我想创建一个 bash 脚本来定位目录 foo
中超过 500mb 的目录。
我有这个命令,它按大小升序查找带有 foo 的目录。
du /foo/* | sort -n
问题在于它包括父目录的大小。因此,例如输出将是:
...
442790 /foo/bar/baz/qux
442800 /foo/bar/baz
442880 /foo/bar
我希望输出只显示 /foo/bar/baz/qux
。由于父目录包含 /foo/bar/baz/qux
并有自己的文件大小,但实际上当排除 /foo/bar/baz/qux
时它们是很小的文件夹>.
一些伪代码:
if the current directory is greater than 500mb then
check next directory is parent to current directory (i.e `/foo/bar/baz` is parent of `/foo/bar/baz/qux`) then
takeaway size of parent from current.
if resulting size is greater than 500mb then
return row
else
go to next row
else
go to next row
else
go to next row
最佳答案
使用 GNU find
和 GNU sort
:
find /some/dir -type d -exec du -hS {} + | sort -rh
du -S
打印不包括子目录的目录大小。
关于linux - 如何定位最大的目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31232491/