Use du -h and sort -h
find /your/dir -type f -size +5M -exec du -h '{}' + | sort -hr
Explanations:
du -h file1 file2 ... prints d isk u sage in h readable XML format for these files.sort -hr sorts h readable numbers in r order (first numbers).- the
+ find -exec option will reduce the number of calls to the du command and therefore speed up execution. Here + can be replaced by ';' .
You can remove the -r option of the sort command if you want larger files to be printed at the end. You can even use the simpler following command, but the buffer of the terminal buffer can be filled!
find /your/dir -type f -exec du -h '{}' + | sort -h
Or, if you want only a dozen large files:
find /your/dir -type f -exec du -h '{}' + | sort -hr | head
Note: the -h option from sort was introduced around 2009, so this option may not be available in the old distribution (like Red Hat 5). In addition, the + find -exec option is not available on the older distribution (like Red Hat 4).
In the old distribution, you can use xargs instead of the + find -exec option. The ls can also be used to print sorted files. But to guarantee sorting by size, xargs should only call ls once. xargs can call ls only once if the number of files is acceptable: it depends on the length of the text passed to the ls argument (the sum of all the lengths of the file names).
find /your/dir -type f -size +5M -print0 | xargs -0 ls -1Ssh
(with a little inspiration borrowed from MichaelKrelin-hacker ).
Explanations:
ls -1 displays one file per linels -S sorted by file sizels -S print file sizels -h prints sizes in human readable format
The fastest command can use the above ls -1Ssh with the + find -exec option, but as mentioned above, the number of files should be acceptable for calling ls only once to ensure sorting by size (the + of find -exec option works in much the same way xargs ).
find /your/dir -type f -size +5M -exec ls -1Ssh '{}' +
To reduce the number of files found, you can increase the threshold size: replace +5M with +100M , for example.
source share