根据大小递归排序文件

use*_*293 104 command-line ls find

我需要在文件夹中找到最大的文件。
如何递归扫描文件夹并按大小对内容进行排序?

我试过使用ls -R -S,但这也列出了目录。
我也尝试使用find.

slm*_*slm 125

您也可以仅使用du. 为了安全起见,我正在使用以下版本du:

$ du --version
du (GNU coreutils) 8.5
Run Code Online (Sandbox Code Playgroud)

该方法:

$ du -ah ..DIR.. | grep -v "/$" | sort -rh
Run Code Online (Sandbox Code Playgroud)

方法分解

该命令du -ah DIR将生成给定目录中所有文件和目录的列表DIR。该-h会产生人类可读的大小,我喜欢。如果你不想要它们,那就放下那个开关。我正在使用head -6just 来限制输出量!

$ 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
Run Code Online (Sandbox Code Playgroud)

很容易将其从小到大排序:

$ 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
Run Code Online (Sandbox Code Playgroud)

反转它,从最大到最小:

$ 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
Run Code Online (Sandbox Code Playgroud)

不要显示目录,只显示文件:

$ 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
Run Code Online (Sandbox Code Playgroud)

如果要从输出中排除所有目录,可以使用带有点字符的技巧。这假设您的目录名称不包含点,并且您要查找的文件包含点。然后你可以过滤掉目录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
Run Code Online (Sandbox Code Playgroud)

如果你只是想的最小到最大的名单,但前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
Run Code Online (Sandbox Code Playgroud)

  • `grep -v "/$"` 部分似乎没有按照您的预期运行,因为目录没有附加斜杠。有谁知道如何从结果中排除目录? (19认同)
  • 基于该解决方案以及这篇文章提供的解决方案:https://unix.stackexchange.com/questions/22432/getting-size-with-du-of-files-only,我能够用文件产生结果仅使用以下命令:`find . -type f -exec du -ah {} + | grep -v“/$”| 排序-rh` (5认同)
  • 这不仅列出文件,还列出目录:( (3认同)
  • 这也发现目录 (2认同)

ter*_*don 30

如果您想查找当前目录及其子目录中的所有文件并根据它们的大小(不考虑它们的路径)列出它们,并且假设没有一个文件名包含换行符,那么使用 GNU find,您可以这样做:

find . -type f -printf "%s\t%p\n" | sort -n
Run Code Online (Sandbox Code Playgroud)

从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.
Run Code Online (Sandbox Code Playgroud)

来自man sort:

   -n, --numeric-sort
          compare according to string numerical value
Run Code Online (Sandbox Code Playgroud)

  • 不幸的是,在 Mac 上不起作用,显示:find: -printf: 未知的主要或运算符 (2认同)
  • @RomanGaufman 是的,这就是答案指定 ***GNU*** find 的原因。如果您在 Mac 上安装了 GNU 工具,它也可以在那里工作。 (2认同)

ken*_*orb 12

尝试以下命令:

ls -1Rhs | sed -e "s/^ *//" | grep "^[0-9]" | sort -hr | head -n20
Run Code Online (Sandbox Code Playgroud)

它将递归列出当前目录中的前 20 个最大文件。

注意:OSX/BSD 上不提供-hfor选项sort,因此您必须sort从coreutils(例如 via brew)安装并将本地 bin 路径应用到PATH,例如

export PATH="/usr/local/opt/coreutils/libexec/gnubin:$PATH" # Add a "gnubin" for coreutils.
Run Code Online (Sandbox Code Playgroud)

或者使用:

ls -1Rs | sed -e "s/^ *//" | grep "^[0-9]" | sort -nr | head -n20
Run Code Online (Sandbox Code Playgroud)

对于最大的目录使用du,例如:

du -ah . | sort -rh | head -20
Run Code Online (Sandbox Code Playgroud)

或者:

du -a . | sort -rn | head -20
Run Code Online (Sandbox Code Playgroud)

  • 完美,这是第一个适用于 Mac 且不显示目录的解决方案 :) - 谢谢! (3认同)

Bra*_*rks 7

这将递归地查找所有文件,并按大小对它们进行排序。它以 kb 为单位打印出所有文件大小,并向下取整,因此您可能会看到 0 KB 文件,但它对我的使用来说已经足够接近了,并且可以在 OSX 上运行。

find . -type f -print0 | xargs -0 ls -la | awk '{print int($5/1000) " KB\t" $9}' | sort -n -r -k1


小智 7

Mac/Linux 跳过目录的简单解决方案:

find . -type f -exec du -h {} \; | sort -h
Run Code Online (Sandbox Code Playgroud)


Sté*_*las 5

使用zsh,您会找到最大的文件(就表观大小而言,如ls -l输出中的大小列,而不是磁盘使用情况):

ls -ld -- **/*(DOL[1])
Run Code Online (Sandbox Code Playgroud)

对于 6 个最大的:

ls -ld -- **/*(DOL[1,6])
Run Code Online (Sandbox Code Playgroud)

要按文件大小对它们进行排序,您可以使用ls's-S选项。一些ls实现还可以-U选择ls不对列表进行排序(因为这里已经按大小排序了zsh)。