Finding all file sizes

Finding all file sizes with the “du” command

The following command will display all files and folders sorted by MegaBytes.

du --max-depth=1|sort -n|awk 'BEGIN {OFMT = "%.0f"} {print $1/1024,"MB", $2}'

You should see an output of all your folders with the size of them in megabytes similar to the following:

0 MB ./.trash
0 MB ./.cpanel
0 MB ./mail
4 MB ./tmp
1173 MB ./public_html
1224 MB .

This output shows that all files over 1 MB are in the /public_html. You can run this command on a directory basis to find folders that are large. Also, this command can be customized to refine your search.

Finding specific file sizes using the “find” command

Below is the “find“command that looks for specific files in the “home” directory. The following script is finding files that are 99k or larger

find /home -type f -size +99k -exec ls -lh {} \; | awk '{ print $9 ": " $5 }'

The previous command will output a similar result like the following.

/home/username/public_html/error_log: 51M
/home/username/backup-username.tar.gz: 73M

Here you can see that there is a backup that is 73MB large. You can find your backup files that are no longer needed and remove them to free up space.