Managing your server storage is a critical to ensuring that you get the most value out of your package. Inefficient usage of disk space can lead to increased storage costs and make managing your server difficult. This is as true for small personal servers as it is enterprise infrastructure, get it right now and you’ll have less to worry about in the future.
Overall monitoring directory size and overall disk usage helps prevent performance bottlenecks, help detect malware and can even help diagnose site errors via runaway log files. In this guide we’ll touch on how to use the du and df commands to monitor directory size and overall disk usage.
Using the du (disk usage) command check the size of directories and any subdirectories. It provides you with a lot of different options to ensure you get the output you need.
1. You can start getting a feel for du by using it on its own – This shows the size of a specific directory and its subdirectories in KB.
du /path/to/directory
2. To improve this further you can use the -h flag to show the output in a more “human-readable” format converting KB into MB, GB or TB as required.
du -h /path/to/directory
3. Moving on, you might want to limit the scan depth of the du command to do this you can amend your command with –max-depth and the directory path, this is a more custom scan and is great for high-level directories.
du -h --max-depth=1 /home/directory
5. After that, you can make understanding the output even easier by incorporating sort. The command below will sort items in that directory by largest – smallest.
du -h --max-depth=1 /home/directory | sort -rh
Using the df (disk free) command get view disk space usage across all mounted file systems on your server. Similar to du above, we’ll go over some basic df commands and flags.
1. First off, you can run the df command on its own, this will show the disk usage across all partitions on the server and includes the total size, space used, available space, and the usage percentage.
df
2. Moving on, similar to du you can add the -h flag to specify that you want the results in a human readable format.
df -h
3. To focus down your search further you can target specific mount points or partitions allowing you to zero in disk usage across your server.
df -h /dev/sda2
df -i
This shows the number of inodes which store metadata for files, these are critical. If your system runs out of inodes, new files can’t be created even if space is available.
df -T
The -T flag identifies the format of each mounted file system whether that is ext4, xfs or tmpfs.