Monitoring Disk Usage on a Linux Server with df and du
There are a number of important tasks that are part of the day-to-day running of the server. One of these is monitoring your disk usage and ensuring that you don’t run out of space. Fortunately Linux ships with a couple of helpful tools for just this purpose.
df for Disk Usage Monitoring
The first of these is df, or disk free. This command will show the overall disk usage for the disk partitions on your system. There are a few helpful flags for the command, the main ones being
-h This flag puts the output in a human readable format abbreviating to KB/MB/GB rather than the standard output, which is the number of 1KB blocks on the filesystem.
-i This shows the inode usage rather than byte usage.
Some command examples are:
df -h
df -ih
The first shows the usage of your filesystems in the human readable format. The second shows inode usage in a human readable format. To explain, unfamiliar inodes are what the filesystem uses to keep track of files and directories on the file system. Each file and directory uses an inode up that the filesystem uses to reference where the data is written on the disk, among other information. The filesystem has a limited number of inodes, though this can be extended. The reason for the limit is to reduce the amount of disk space used by the filesystems data tables, so increasing the number of usable inodes decreases potential storage space on the disk. Unless you have a lot of very small files written to your disks you are unlikely to run into the inode limit on a normal system.
Running out of space or inodes on a system can cause some unexpected behavior, so df can be a helpful tool for ensuring you don’t experience this.
df for Disk Space Usage Location and Alert
While this tool is quite helpful it doesn’t let you know where the disk space is being used when you are running low. This is where the du (disk usage) command comes in helpful. This command will list the size of all files and directories in the current directory and all its subdirectories, meaning it can produce some pretty large output. It can also be provided with an alternative directory as an argument which it will then display the information for.
There are a number of flags that can be used to modify the output. Again here are a few of the common ones:
-h As with df, this command sets to use human readable mode.
–inodes This displays the inode usage instead of disk space.
-d x This command displays the usage to a depth of “x” subdirectories below the current. -d 0 will only display the usage of the current directory for example.
-s This is equivalent to using “-d 0”.
–help This provides a help listing with a complete list of flags.
Usage is again fairly straightforward:
du -h
du -hd 1
The first command shows the human readable output of all files and subdirectories from the current directory. The second provides the human readable output for only the files and subdirectories of the current directory. This usage is very helpful if you find yourself running short of disk space, but are unsure where it is being used. The output is short enough to be generally easily readable and makes it simple to see which directories are using the most space, after which you can follow the trail through the directories repeating the command. The output can be piped through other tools such as grep and sort to help make this easier. For example:
du -hd 1 | sort -h
This command sorts the output from the smallest to the largest files, making it really easy to see the directories using the most space. Similarly you can throw in the –inodes flag:
du –inodes -hd 1 | sort -h
This helps identify which directory’s contents is making use of the most inodes on your system, helping you find all those files.