Get a human-readable list of file sizes in the current directory
- Written by Dave on Friday 1 July 2011
On occasion I have need in the unix shell to display all files in a folder without directories. Sometimes it's useful to show their filesizes too, sorted descending by size. This is convenient for instance in a large folder of images, where you want to see which ones are diskspace-hogs.
I'll build it up step by step, and explain each part so you understand the three core methods used here.
ls -laSh
You know what ls does. For a rundown of the parameters I'll refer you to man ls which explains it much better than I do. Suffice to say that it generates a list of all files and folders in the active directory (including names starting with .), sorted descending by filesize in human-readable notation.
ls -laSh | grep -v ^d
The piped grep does an inverse selection (-v) of all results of ls -l starting with "d". So we effectively end up with all files, excluding the directories.
ls -laSh | grep -v ^d | awk '{print $5 "\t" $9}'
We already have our desired result at this point, but we want to cut away the information we don't want to see. What the piped awk basicly does is "show me column 5 and 9 of the output, separated by a tab".