Articles on: Configuring, Customizing & Troubleshooting

How To Check Disk Space In SSH

The Linux command line can leave you feeling as if you don't have all the information at your fingertips. In a traditional GUI, you can just peek at the bottom of the window and see how much space a folder is using. Or you can hover over a file or folder and get its size. You even have graphical indications of how much space you have left on your hard disk.

However, just because this information is not instantly visible on a command line interface like CentOS, doesn't mean it's not easily available. With the right commands, you can get a lot more information out of the command line, and that includes knowing how much free space you have. In this tutorial, I'll show you how to check free space, and get the storage consumption of files and folders in MB.

But First: Space Permissions with Sudo



Many files and folders are restricted to admins only. Additionally, commands like fdisk require sudo permissions to run. In these examples, I'm not logged into root, so I'll sometimes be using sudo. Here's a tutorial on how to give users sudo permissions.

Checking Free Space



To get the free space available on all your disks, type the following command:

sudo fdisk -l


The volumes on CentOS are labeled like this:

/dev/vda /dev/vdb etc...


In addition, you'll sometimes see a number after these disks - these indicate partitions. In the screenshot below, I don't have any partitions yet:



I have two volumes, but only the first one is of any significant size - 26.8 GB.

So that's how you check free space on Linux. Now let's see how we get the sizes of files an folders.

Getting Folder Space Allocation



We use the "du" command in any given directory for a breakdown of the sizes of the individual folders. For example, here I'm in the "usr" directory when I use the following command:

du | more


The "more" command is so that everything doesn't just scroll up too fast for me to see. Instead, it'll present me with information a little bit at a time.



Here you can see that in my current "usr" directory, I have large folders like "bin", "sbin" etc. I haven't sorted these in any particular order yet. Note that the size is displayed in bytes. If we want, we can change this to a more convenient human-readable format by adding the -h parameter:

du -h


This gives us the following:



Much better! It's much more intuitive to get the sizes in MB. However, if we want to find out which folders are taking up the most space, we need to sort this list in descending order. We do this by piping the output of du to the "sort" command with the "-hr" parameters like this:

du -h | sort -hr | more


Which gives us:



This shows us that there were much bigger folders further down, which are now revealed due to the descending sort. Useful right?

Now that we have the sizes of folders, let's get the file sizes as well.

Checking the File Sizes



To get the files as well as the folders in our net, we just need to use the "-a" parameter with du. So the above command becomes:

du -ah | sort -rh | more


And now all the files sizes are visible in descending order:



You can even pass a specific folder to "du" and get the sizes of files and folders inside it. You don't have to be physically present inside it. So we could have gotten the results of /usr by typing:

du /usr


from anywhere else. These commands should allow you to get an idea of what's going on with your system, which files are using the maximum space, and which folders are the biggest!!

Updated on: 11/10/2024

Was this article helpful?

Share your feedback

Cancel

Thank you!