Linux如何查找大文件或目录总结

原帖地址


在Linux系统中,如何去搜索一些比较大的文件呢?下面我整理了一下在Linux系统中如何查找大文件或文件夹的方法。

1 如何查找大文件

其实很多时候,你需要了解 “/” 下有哪些大的数据文件,比如文件大小超过100M或1G(阀值视具体情况而定)。那么如何把这些大文件搜索出来呢?

1.1搜索指定目录下超过指定大小的文件

例如我要搜索 / 下,超过500M大小的文件

find / -type f -size +500M

 

如上命令所示,我们仅仅能看到超过500M大小的文件的文件名称,但是对文件的信息(例如,文件大小、文件属性)一无所知,那么能否更详细显示一些文件属性或信息呢,当然可以

1.2搜索指定目录下超过指定大小的文件(显示文件用户、属组)

 find / -type f -size +500M -print0 | xargs -0 ls –l

 

1.3搜索指定目录下超过指定大小的文件(显示文件详细size)

当我们只需要查找超过500M大小文件,并显示查找出来文件的具体大小,可以使用下面命令

 find / -type f -size +500M -print0 | xargs -0 du –h

1.4搜索指定目录下超过指定大小的文件(按大小排序,结果有出入)

如果你还需要对查找结果按照文件大小做一个排序,那么可以使用下面命令

find / -type f -size +500M -print0 | xargs -0 du -h | sort -nr
711M /u01/app/oracle/oradata/prod/system01.dbf

521M /u01/app/oracle/oradata/prod/sysaux01.dbf

1.5搜索指定目录下超过指定大小的文件(按大小排序,严格的)

不过如上截图所示,有时候排列的顺序并不完全是按大小一致,这个是因为du命令的参数h所致,你可以统一使用使用MB来显示,这样就能解决这个问题

 find / -type f -size +500M -print0 | xargs -0 du -hm | sort –n
521 /u01/app/oracle/oradata/prod/sysaux01.dbf

711 /u01/app/oracle/oradata/prod/system01.dbf

1.6 搜索指定目录下超过指定大小的文件(详细显示文件的属主、属组、文件大小(M为单位))

 find / -type f -size +500M -print0 | xargs -0 ls -lh | sort -nr
rw-r----- 1 oracle oinstall 711M 11-20 09:06 /u01/app/oracle/oradata/prod/system01.dbf
-rw-r----- 1 oracle oinstall 521M 11-20 09:06 /u01/app/oracle/oradata/prod/sysaux01.dbf

2 如何查找Linux下的大目录

譬如有时候磁盘空间告警了,而你平时又疏于管理、监控文件的增长,那么我需要快速的了解哪些目录变得比较大,那么此时我们可以借助du命令来帮我们解决这个问题

2.1查找指定目录下的大目录

 du -h /u01 --max-depth=1
5.7G /u01/app

16K /u01/lost+found

5.7G /u01

[oracle@Oracle11g ~]$ du -h /u01 --max-depth=2

2.6M /u01/app/oraInventory

5.7G /u01/app/oracle

5.7G /u01/app

16K /u01/lost+found

5.7G /u01
 du -h /u01 --max-depth=3
16K /u01/app/oraInventory/ContentsXML

8.0K /u01/app/oraInventory/oui

2.6M /u01/app/oraInventory/logs

2.6M /u01/app/oraInventory

1.7G /u01/app/oracle/oradata

4.0K /u01/app/oracle/checkpoints

4.0G /u01/app/oracle/product

716K /u01/app/oracle/admin

232K /u01/app/oracle/cfgtoollogs

6.8M /u01/app/oracle/diag

5.7G /u01/app/oracle

5.7G /u01/app

16K /u01/lost+found

5.7G /u01

如果你想知道/u01目录下面有哪些大文件夹,那么可以将参数max-depth=2 ,如果你想对搜索出来的结果进行排序,那么可以借助于sort命令。如下所示

du -h /u01 --max-depth=2 |sort -n
2.6M /u01/app/oraInventory

5.7G /u01

5.7G /u01/app

5.7G /u01/app/oracle

16K /u01/lost+found

[oracle@Oracle11g ~]$ du -h /u01 --max-depth=3 |sort -n

1.7G /u01/app/oracle/oradata

2.6M /u01/app/oraInventory

2.6M /u01/app/oraInventory/logs

4.0G /u01/app/oracle/product

4.0K /u01/app/oracle/checkpoints

5.7G /u01

5.7G /u01/app

5.7G /u01/app/oracle

6.8M /u01/app/oracle/diag

8.0K /u01/app/oraInventory/oui

16K /u01/app/oraInventory/ContentsXML

16K /u01/lost+found

232K /u01/app/oracle/cfgtoollogs

716K /u01/app/oracle/admin

有时候搜索出来的结果太多了(譬如,我从根目录开始搜索),一直在刷屏,如果我只想查出最大的5个文件夹,怎么办呢?此时就要借助head命令来显示了

 du -hm /u01/app/oracle/ --max-depth=2 | sort -nr | head -5
5741 /u01/app/oracle/

4057 /u01/app/oracle/product/11.2.0

4057 /u01/app/oracle/product

1677 /u01/app/oracle/oradata/prod

1677 /u01/app/oracle/oradata

3.自己整理的(实用的)

3.1 查找系统中的大目录(从大到小排序,取前5个)

du -hm / --max-depth=1 | sort -nr | head -5
9456 /
5744 /u01
2964 /usr
260 /dev
234 /lib

3.2 查找上面目录(/u01)中的大文件

find /u01/app/oracle/ -type f -size +500M -print0 | xargs -0 ls -lh | sort -nr
-rw-r----- 1 oracle oinstall 711M 11-20 09:41 /u01/app/oracle/oradata/prod/system01.dbf
-rw-r----- 1 oracle oinstall 521M 11-20 09:36 /u01/app/oracle/oradata/prod/sysaux01.dbf

哈哈,挺实用的

 


END

本文来自网络,不代表本站立场,转载请注明出处:万道一,Wonder One » Linux如何查找大文件或目录总结
莫要搞事情哦
你喜欢的人刚好也未喜欢你
张学友刘德华邓紫琪已关注
赞(0) 打赏

赏点小费吧客倌

微信扫一扫打赏