我需要找到文件夹中最大的文件。
如何递归扫描文件夹并按大小对内容进行排序?
我尝试过使用ls -R -S
,但这也列出了目录。
我也尝试使用find
.
答案1
您也可以仅使用 来完成此操作du
。为了安全起见,我使用这个版本du
:
$ du --version
du (GNU coreutils) 8.5
该方法:
$ du -ah <some DIR> | grep -v "/$" | sort -rh
方法分解
该命令du -ah DIR
将生成给定目录中所有文件和目录的列表DIR
。这-h
将产生我更喜欢的人类可读的尺寸。如果您不想要它们,请删除该开关。我正在使用head -6
just 来限制输出量!
$ du -ah ~/Downloads/ | head -6
4.4M /home/saml/Downloads/kodak_W820_wireless_frame/W820_W1020_WirelessFrames_exUG_GLB_en.pdf
624K /home/saml/Downloads/kodak_W820_wireless_frame/easyshare_w820.pdf
4.9M /home/saml/Downloads/kodak_W820_wireless_frame/W820_W1020WirelessFrameExUG_GLB_en.pdf
9.8M /home/saml/Downloads/kodak_W820_wireless_frame
8.0K /home/saml/Downloads/bugs.xls
604K /home/saml/Downloads/netgear_gs724t/GS7xxT_HIG_5Jan10.pdf
很容易将其从最小到最大排序:
$ du -ah ~/Downloads/ | sort -h | head -6
0 /home/saml/Downloads/apps_archive/monitoring/nagios/nagios-check_sip-1.3/usr/lib64/nagios/plugins/check_ldaps
0 /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/0/index/write.lock
0 /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/0/translog/translog-1365292480753
0 /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/1/index/write.lock
0 /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/1/translog/translog-1365292480946
0 /home/saml/Downloads/data/elasticsearch/nodes/0/indices/logstash-2013.04.06/2/index/write.lock
将其反转,从最大到最小:
$ du -ah ~/Downloads/ | sort -rh | head -6
10G /home/saml/Downloads/
3.8G /home/saml/Downloads/audible/audio_books
3.8G /home/saml/Downloads/audible
2.3G /home/saml/Downloads/apps_archive
1.5G /home/saml/Downloads/digital_blasphemy/db1440ppng.zip
1.5G /home/saml/Downloads/digital_blasphemy
不要显示目录,只显示文件:
$ du -ah ~/Downloads/ | grep -v "/$" | sort -rh | head -6
3.8G /home/saml/Downloads/audible/audio_books
3.8G /home/saml/Downloads/audible
2.3G /home/saml/Downloads/apps_archive
1.5G /home/saml/Downloads/digital_blasphemy/db1440ppng.zip
1.5G /home/saml/Downloads/digital_blasphemy
835M /home/saml/Downloads/apps_archive/cad_cam_cae/salome/Salome-V6_5_0-LGPL-x86_64.run
如果你想排除所有目录从输出中,您可以使用存在点字符的技巧。这假设您的目录名称不包含点,而您正在查找的文件包含点。然后您可以使用以下命令过滤掉目录grep -v '\s/[^.]*$'
:
$ du -ah ~/Downloads/ | grep -v '\s/[^.]*$' | sort -rh | head -2
1.5G /home/saml/Downloads/digital_blasphemy/db1440ppng.zip
835M /home/saml/Downloads/apps_archive/cad_cam_cae/salome/Salome-V6_5_0-LGPL-x86_64.run
如果您只想要从最小到最大的列表,但需要前 6 个有问题的文件,您可以反转排序开关,删除 ( -r
),然后tail -6
使用head -6
.
$ du -ah ~/Downloads/ | grep -v "/$" | sort -h | tail -6
835M /home/saml/Downloads/apps_archive/cad_cam_cae/salome/Salome-V6_5_0-LGPL-x86_64.run
1.5G /home/saml/Downloads/digital_blasphemy
1.5G /home/saml/Downloads/digital_blasphemy/db1440ppng.zip
2.3G /home/saml/Downloads/apps_archive
3.8G /home/saml/Downloads/audible
3.8G /home/saml/Downloads/audible/audio_books
答案2
如果你想找到当前目录及其子目录中的所有文件并根据它们的大小列出它们(不考虑它们的路径),并且假设所有文件名都不包含换行符,使用 GNU find
,你可以这样做:
find . -type f -printf "%s\t%p\n" | sort -n
来自man find
GNU 系统:
-printf format
True; print format on the standard output,
interpreting `\' escapes and `%' directives.
Field widths and precisions can be specified
as with the `printf' C function. Please note
that many of the fields are printed as %s
rather than %d, and this may mean that flags
don't work as you might expect. This also
means that the `-' flag does work (it forces
fields to be left-aligned). Unlike -print,
-printf does not add a newline at the end of
the string. The escapes and directives are:
%p File's name.
%s File's size in bytes.
从man sort
:
-n, --numeric-sort
compare according to string numerical value
答案3
尝试以下命令:
ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20
它将递归列出当前目录中前 20 个最大的文件。
-h
注意:的选项sort
在 OSX/BSD 上不可用,因此您必须sort
从coreutils
(例如通过brew
)安装并将本地 bin 路径应用于PATH
,例如
export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" # Add a "gnubin" for coreutils.
或者使用:
ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20
对于最大的目录,请使用du
,例如:
du -ah . | sort -rh | head -20
或者:
du -a . | sort -rn | head -20
答案4
适用于 Mac/Linux 的跳过目录的简单解决方案:
find . -type f -exec du -h {} \; | sort -h