How to get file sizes recursively in a directory and list them in ascending order

When cleaning up unused files from my linux computer, I find it useful to know which files use most space so that I can easily determine which ones to remove.

The most basic command to find file size of a given file is:
du -sm myfile.txt

If you are in a folder with multiple files and want to get file sizes of all the files, the command is:
du -sm *

The above command does not get file sizes recursively. However, if you are happy with it and want to sort the file sizes in ascending order:
du -sm * | sort -n -k 1

Now, this is little useful as it does not do recursive file size listing. To do that:
find . -type f -exec du -sm {} \; | sort -n -k 1

If you are looking for a particular type of file (eg. jar files):
find . -name "*.jar" -type f -exec du -sm {} \; | sort -n -k 1

Enjoy!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.