As a server administrator, you need to constantly be on the lookout for ways to conserve disk space. The first step to that is monitoring what’s being used on your system. Is outdated software swallowing up precious GB? Did you forget to delete the source files after compiling them? Are old software versions still clogging up your directories? If you find yourself running low on disk space, the first step is to conduct a broad audit.
If you’re used to working with a Windows GUI, you’ll know that there are lots of utilities we can use to obtain sophisticated graphs and charts of disk usage across folders and files. Moreover, you can simply list the contents of a directory and sort by disk space to find out which directories have the most storage. Of course in a Linux CLI environment, these conveniences are out of reach for us. However, that doesn’t mean we can’t do an effective job at managing disk space.
Here are some easy ways to check disk space usage and get a handle on managing your storage.
Getting the Partition list and Block Size
Just like you have partitions in Windows, Linux has can have partitions as well. However, they’re not represented as different letters of the alphabet, but look more like files instead. To find out what partitions are available on your Linux set up, type the following command:
This will give an output like the following:
In the above screenshot, we have a disk called “/dev/vda”. This disk is divided into two partitions with numbers 1 and 15 as highlighted further below. This means, we can refer to each partition by appending the number to the disk as such:
Each partition is made up of “blocks” that vary in size between systems. To find out the size of a block on a given partition, we use the “blockdev” command like this, using the specific partition as a parameter:
blockdev --getbsz /dev/vda1
This gives us:
So we know that the partition “/dev/vda1” has a block size of 4KB.
Getting the Disk Usage of Folders
If we’re currently in a folder at any given moment, we can get the disk usage in block size for all the subdirectories by using the command “du” like this:
This command will go recursively through all the subdirectories of the current folder and tell you how many blocks is used by each. While this gives us the data we need, it’s not very informative. Ideally we’d like to know not just the block size, but also the exact space used in terms of MB or GB. Moreover, we’d like to sort them by date. To do this, we use the “-h” parameter and pipe the output through the sort command using the following:
du -h | sort -h
This gives us a list of directories sorted in ascending order by the amount of space used in terms of “K”, or “M” like this:
You can specify a directory as a parameter for the “du” command instead of just searching the current directory. You can even get an overview of the most disk expensive folders by just passing “/” like this:
du / -h | sort -hr
This will give you a list in descending order in terms of which folders are the most expensive. Note that this will be a long list and you won’t be able to scroll up to see the first entries. We can correct this using the “| less” pipe. So the above command will become:
du / -h | sort -hr | less
When you’re done, simply type “q” to interrupt the command execution and exit.
Get File Usage
Once you’ve narrowed down your search to a specific folder, you can use the “-a” switch to get the file sizes within a particular directory. Like this:
du -ah | sort -h
This will generate the following output:
So using a combination of the above commands, you can drill down to the specific subfolder and file you want, to see what’s eating up all that disk space on your system. It might not be as convenient as clicking a tab and ordering a folder like we do with a GUI, but it gets the job done without all that graphical overhead to hamper your server performance!
Is your website slow?
Enter its URL below to find out now:
- PREVIOUS ENTRY: How Does a Game Server Compensate for Lag?
- NEXT ENTRY: How to Exclude Specific Categories from the Meta Info in WordPress