I need to find the largest files in a folder.
How do I scan a folder recursively and sort the contents by size?
I have tried using ls -R -S
, but this lists the directories as well.
I also tried using find
.
command linefindls
I need to find the largest files in a folder.
How do I scan a folder recursively and sort the contents by size?
I have tried using ls -R -S
, but this lists the directories as well.
I also tried using find
.
Best Answer
You can also do this with just
du
. Just to be on the safe side I'm using this version ofdu
:The approach:
Breakdown of approach
The command
du -ah DIR
will produce a list of all the files and directories in a given directoryDIR
. The-h
will produce human readable sizes which I prefer. If you don't want them then drop that switch. I'm using thehead -6
just to limit the amount of output!Easy enough to sort it smallest to biggest:
Reverse it, biggest to smallest:
Don't show me the directory, just the files:
If you want to exclude all directories from the output, you can use a trick with the presence of a dot character. This assumes that your directory names do not contain dots, and that the files you are looking for do. Then you can filter out the directories with
grep -v '\s/[^.]*$'
:If you just want the list of smallest to biggest, but the top 6 offending files you can reverse the sort switch, drop (
-r
), and usetail -6
instead of thehead -6
.