Sunday, January 15, 2017

How to list all files ordered by size

http://unix.stackexchange.com/questions/53737/how-to-list-all-files-ordered-by-size

Simply use something like:
ls -lS /path/to/folder/
Capital S.
This will sort files in size.
Also see:
-S     sort by file size
If you want to sort in reverse order, just add -r switch.
Update:
To exclude directories:
ls -lS | grep -v '^d' 
Update 2:
I see now how it still shows symbolic links, which could be folders. Symbolic links always start with a letter l, as in link.
Change the command to filter for a -. This should only leave regular files:
ls -lS | grep '^-'
On my system this only shows regular files.
update 3:
To add recursion I would leave the sorting of the lines to the sort command and tell it to use the 5th column to sort on.
ls -lR | grep '^-' | sort -k 5 -rn
-rn means Reverse and numeric to get the biggest files at the top. Down side of this command is that it does not show the full path of the files.
If you do need the full path of the files, use something like this:
find . -type f  | xargs du -h | sort -rn
The find command will recursively list all files in all sub directories of .xargs will use the output of find as an argument to du -h meaning disk usage -humanreadable and then sort the output again.