Linux如何查找大文件或目录总结
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux如何查找大文件或目录总结相关的知识,希望对你有一定的参考价值。
1: 如何查找大文件?
搜索当前目录下,超过100M大小的文件:
[[email protected] u03]# find . -type f -size +100M ./usr/local/jdk-7u67-linux-x64.tar.gz ./data/log/charge-service/test-access.log.2016-08-08.log ./data/log/aaa_service/test-access.log.2016-08-09.log ./home/deploy/logs/testmqlogs/otherdays/testmq_client.1.log ./home/deploy/logs/testmqlogs/otherdays/testmq_client.2.log ./opt/backend/charge-service/1.2/shared/console.log ./opt/backend/express_service/0.0.3.tar.gz ./opt/backend/aaa-service/1.2/shared/console.log
更详细显示一些文件属性或信息:
[[email protected] u03]# find . -type f -size +100M -print0 | xargs -0 ls -l -rw-rw-r-- 1 deploy deploy 542795006 8月 30 15:14 ./data/log/charge-service/test-access.log.2016-08-08.log -rw-rw-r-- 1 deploy deploy 225658142 8月 25 16:28 ./data/log/aaa_service/test-access.log.2016-08-09.log -rw-rw-r-- 1 deploy deploy 253623621 8月 30 15:15 ./home/deploy/logs/testmqlogs/otherdays/testmq_client.1.log -rw-rw-r-- 1 deploy deploy 373547598 8月 25 16:28 ./home/deploy/logs/testmqlogs/otherdays/testmq_client.2.log -rw-rw-r-- 1 deploy deploy 645631934 8月 30 15:15 ./opt/backend/charge-service/1.2/shared/console.log -rw-rw-r-- 1 deploy deploy 209314900 8月 30 15:15 ./opt/backend/aaa-service/1.2/shared/console.log -rw-r--r-- 1 root root 315334884 12月 21 2015 ./opt/backend/express_service/0.0.3.tar.gz -rw-r--r-- 1 root root 142376665 6月 11 2015 ./usr/local/jdk-7u67-linux-x64.tar.gz -rw-rw-r-- 1 deploy deploy 105085974 8月 30 15:15 ./usr/local/push-tomcat/logs/push/info.log6829699375703641.tmp
当我们只需要查找超过100M大小文件,并显示查找出来文件的具体大小,并排序(下)
[[email protected] u03]# find . -type f -size +100M -print0 | xargs -0 du -h 102M ./usr/local/push-tomcat/logs/push/info.log6829699375703641.tmp 136M ./usr/local/jdk-7u67-linux-x64.tar.gz 518M ./data/log/charge-service/test-access.log.2016-08-08.log 216M ./data/log/aaa_service/test-access.log.2016-08-09.log 242M ./home/deploy/logs/testmqlogs/otherdays/testmq_client.1.log 357M ./home/deploy/logs/testmqlogs/otherdays/testmq_client.2.log 616M ./opt/backend/charge-service/1.2/shared/console.log 301M ./opt/backend/express_service/0.0.3.tar.gz 201M ./opt/backend/aaa-service/1.2/shared/console.log
[[email protected] u03]# find . -type f -size +100M -print0 | xargs -0 du -h | sort -nr 616M ./opt/backend/charge-service/1.2/shared/console.log 518M ./data/log/charge-service/test-access.log.2016-08-08.log 357M ./home/deploy/logs/testmqlogs/otherdays/testmq_client.2.log 301M ./opt/backend/express_service/0.0.3.tar.gz 243M ./home/deploy/logs/testmqlogs/otherdays/testmq_client.1.log 216M ./data/log/aaa_service/test-access.log.2016-08-09.log 201M ./opt/backend/aaa-service/1.2/shared/console.log
不过如上截图所示,有时候排列的顺序并不完全是按大小一致,这个是因为du命令的参数h所致,你可以统一使用使用MB来显示,这样就能解决这个问题。到这里,这个在Linux系统查找大文件的命令已经非常完美了,当然如果你还有很多的需求,那么可以在这个命令上做修改、调整.
2: 如何查找Linux下的大目录
譬如有时候磁盘空间告警了,而你平时又疏于管理、监控文件的增长,那么我需要快速的了解哪些目录变得比较大,那么此时我们可以借助du命令来帮我们解决这个问题。
[[email protected] u03]# du -h --max-depth=1 16K ./lost+found 33G ./flash_recovery_area 37G ./oradata 70G . 如果你想知道flash_recovery_area目录下面有哪些大文件夹,那么可以将参数max-depth=2 ,如果你想对搜索出来的结果进行排序,那么可以借助于sort命令。如下所示
[[email protected] u03]# du -h --max-depth=2 | sort -n 3.5G ./flash_recovery_area/EPPS 16K ./lost+found 29G ./flash_recovery_area/backup 33G ./flash_recovery_area 37G ./oradata 37G ./oradata/epps 70G . [[email protected] u03]# du -hm --max-depth=2 | sort -n 1 ./lost+found 3527 ./flash_recovery_area/EPPS 29544 ./flash_recovery_area/backup 33070 ./flash_recovery_area 37705 ./oradata 37705 ./oradata/epps 70775 .
有时候搜索出来的结果太多了(譬如,我从根目录开始搜索),一直在刷屏,如果我只想查出最大的12个文件夹,怎么办呢?此时就要借助head命令来显示了
[[email protected] /]# du -hm --max-depth=2 | sort -nr | head -12 407480 . 167880 ./u04 158685 ./u02/oradata 158685 ./u02 152118 ./u04/oradata 70775 ./u03 37705 ./u03/oradata 33070 ./u03/flash_recovery_area 5995 ./u01/app 5995 ./u01 3551 ./usr 1558 ./usr/share
参考资料:
http://linuxandfriends.com/how-to-find-large-files-in-linux-using-command-line/
http://www.docin.com/p-563963500.html
本文出自 “IT·奋斗” 博客,请务必保留此出处http://qiangsh.blog.51cto.com/3510397/1844338
以上是关于Linux如何查找大文件或目录总结的主要内容,如果未能解决你的问题,请参考以下文章