Check Folder/ Directory Size With SSH + Delete Folder/ Directory With SSH

I backup my files by creating a tar folder and transporting it to a remote location. I use the simple but effective technique documented here.

Recently, I was shocked to find that the tar file had bloated to several GB in size.

I looked in the public_html folder but could not find any suspicious file.

Now the question is how do you determine which folder is the culprit.

The first thing you have to do is check your overall usage with the command:


df –h

The result is this:


[root@vps home]# df –h
Filesystem Size Used Avail Use% Mounted on
/dev/simfs 150G 39G 112G 26% /
none 2.0G 4.0K 2.0G 1% /dev
none 2.0G 0 2.0G 0% /dev/shm
[root@vps home]#

This tells you that out of 150GB available, you have used up 39GB.

Now you have to check the sizes of the various folders / directories with the command:


du -sh /*

This will list out all the folders across the entire VPS with their respective sizes.
To check the sizes of the folders within a directory, use the command:


du -sh *| sort -n

It will list out the names of the folders with the size in GB:


[root@vps home]# du -sh *| sort -n
1.3G abc1
1.4G abc2
2.4M abc3
16K abc4
[root@vps home]#

If you would like the information in MB use the command


du -cms *| sort –nr

If you spot the bloated folder, go inside and run the same command till you isolate the offender.

In may case it was the Maildir folder which had got bloated owing to thousands of bounced messages that had accumulated there. The folder size was an unbelievable 45GB (!).

Now, let’s note the SSH commands for deleting an entire folder/ directory or just the contents of a directory.

If you want to delete an entire folder/ directory use the command:


rm -rf foldername/

If you only want to delete the contents of a folder, while keeping the folder, navigate into the folder and use the command:


rm -rf *

Of course, because this step has lethal consequences and is irretrievable, you must pause for a moment, make sure you are in the right folder, and only then execute the command.

Leave a Reply

Your email address will not be published. Required fields are marked *