You need to traverse the whole directory tree and check the size of each file in order to find the largest one.
In zsh, there's an easy way to sort files by size, thanks to the o
glob qualifier:
print -rl -- **/*(D.oL)
To see just the largest files:
echo **/*(D.oL[-1])
To see the 10 largest files:
print -rl -- **/*(D.oL[-10,-1])
You can also use ls -S
to sort the files by size. For example, this shows the top 10 largest files. In bash, you need to run shopt -s globstar
first to enable recursive globbing with **
; in ksh93, run set -o globstar
first, and in zsh this works out of the box. This only works if there aren't so many files that the combined length of their names goes over the command line limit.
ls -Sd **/* | head -n 10
If there are lots of large files, collecting the information can take a very long time, and you should traverse the filesystem only once and save the output to a text file. Since you're interested in individual files, use the -S
option of GNU du
in addition to -a
; this way, the display for directories doesn't include the size files in subdirectories, only files directly in that directory, which reduces the noise.
du -Sak >du
sort -k1n du | head -n 2
If you only want the size of files, you can use GNU find's -printf
action.
find -type f -printf '%s\t%P\n' | sort -k1n >file-sizes.txt
tail file-sizes.txt
Note that if you have file names that contain newlines, this will mess up automated processing. Most GNU utilities have a way to use null bytes (which cannot appear in file names) instead, e.g. du -0
, sort -z
, \0
instead of \n
, etc.
du -h | sort -h
or the SI prefixed variant preferred by me over the binary prefixed one:du --si | sort -h
. – erik May 18 '14 at 19:42-h
here as you lose precision. You could end up with 20 files with since 200M not knowing which one is the largest and thus returning the wrong result. You want to convert to "human readable" after you've sorted your list. – Stéphane Chazelas May 19 '14 at 09:33numfmt
included, as I have a version lower than coreutils-8.21 which introduces this command line util. – erik May 19 '14 at 23:29