DEV Community

Kolawole D Emmanuel
Kolawole D Emmanuel

Posted on

Essential Linux Commands for Storage Monitoring

Introduction

A fundamental aspect of Linux system administration is managing disk space effectively. Understanding how to gauge and monitor available disk space is crucial for maintaining system performance and ensuring the seamless operation of applications.
In this tutorial, we'll delve into four valuable Linux commands designed to facilitate the monitoring of remaining disk space. We'll introduce each command alongside practical examples, simplifying the process of disk space management.
Let's examine the possible uses for these commands in a Linux setting.

df - Get the Big Picture

The df command, short for disk-free, offers a bird's-eye view of a Linux system's disk usage. It provides information about available space on mounted filesystems.
Let's see an example of what the vanilla (no optional flags) command looks like:

df
Enter fullscreen mode Exit fullscreen mode

This command, without any switches, displays information about all mounted filesystems. It shows the filesystem's device, total size, used space, available space, and usage percentage:

df
Filesystem    1K-blocks     Used Available Use% Mounted on
/dev/sda1      20971520  5452595  15414067  26% /
/dev/sdb1       4718592  4299161    397312  91% /data
Enter fullscreen mode Exit fullscreen mode

In this output, we can see details about various filesystems, including their usage percentages, available space, and mount points. But the output from this flagless command is a bit tough to comprehend.

To circumvent this, we can use the -h flag. The -h switch is an alias for --human-readable, which prints the sizes in human-readable format (e.g., 4.5G for 4.5 gigabytes, 388M for 388 megabytes):

df -h
Filesystem  Size  Used  Avail  Use%  Mounted on
/dev/sda1    20G  5.2G  14.7G   26%  /
/dev/sdb1   4.5G  4.1G   388M   91%  /data
Enter fullscreen mode Exit fullscreen mode

To further see the filesystem type as a separate column, we can use the -t or --print-type switch. This switch adds a column to the output, indicating the filesystem type:

df -t
Filesystem   Type  Size  Used  Avail  Use%  Mounted on
/dev/sda1    ext4   20G  5.2G  14.7G   26%  /
/dev/sdb1  tempfs  4.5G  4.1G   388M   91%  /data
Enter fullscreen mode Exit fullscreen mode

From the output above, we see that the /data partition has 91% utilization, therefore it is nearing its limit. We could investigate the contents of this partition to find the reason behind its size.

du - Dive into Directory Usage

Sometimes, we need to drill down into the specifics of a directory's disk usage. This is where the du (disk usage) command comes in handy. We can use the following standard syntax to check the space used by a specific directory:

du /path/to/directory
Enter fullscreen mode Exit fullscreen mode

Running the command without any options will produce a lengthy, unsorted output that lacks clear indications of the problem's location. To address this, we can enhance the output format by adding specific options to the du command:

du -sh /path/to/directory
Enter fullscreen mode Exit fullscreen mode

Here, the -s or --summarize option provides the total disk space used by a particular file or directory. and the -h or --human-readable flag prints the size in human-readable format.
Let's see an example output after running the command on the previously mentioned /data folder:

du -sh /data 
50M    /data/school 
100M   /data/work 
3.9G   /data/misc 
4.1G   /data
Enter fullscreen mode Exit fullscreen mode

From the output, we see that there are three directories inside the /data partition. Out of the three, the misc folder seems to have the biggest size. Maybe it'll be a good idea to delete some files from this directory.
If we want to dive a little bit deeper into the directory structure and only calculate disk usage up to a certain directory level, we can use the --max-depth switch. Moreover, it provides useful information when we want to just limit our focus and not delve into complex directory structures. Let's see an example of using the --max-depth switch for depth equals 1:

# Running in the current directory for depth=1
du -h --max-depth=1
4.1G   /a
1.2G   /b
Enter fullscreen mode Exit fullscreen mode

Now let's run the same command in the same directory, but now for depth equals 2:

 Running in the current directory for depth=2
du -h --max-depth=2
1G     /a/a1
3.1G   /a/a2
4.1G   /a
500M   /b/b1
730M   /b/b2
1.2G   /b
Enter fullscreen mode Exit fullscreen mode

As we can see from the last two example outputs, increasing the depth has provided us with a detailed idea of the directory structure and which folders hold how much space in the filesystem. In this case, changing the maximum depth can therefore enable us to make informed decisions on which directories to investigate for resolving space issues.

ls - Size Insights with File Listings

The ls command is a versatile tool for listing files and directories within a directory. By adding a few options, we can reveal the sizes of these items. We can add the directory we want information about with the ls command:

ls /path/to/directory
Enter fullscreen mode Exit fullscreen mode

Without any options, the ls command only displays the files and directories of the mentioned directory. If we omit the directory path also, then it just shows the files and directories in the current directory.

ls 
file1.txt
file2.txt
folder1
folder2
Enter fullscreen mode Exit fullscreen mode

This information alone is not useful, as we aren't getting any necessary information about storage management. To see the full power of the ls command, we can add additional flags. Let's see an example output after running the command with additional flags in the current directory:

ls -lah /home/user/documents
-rw-r--r--  1 user user  3.5M Oct 10 10:00 file.txt
-rw-r--r--  1 user user  1.2G Oct 10 10:01 bigapp.bin
Enter fullscreen mode Exit fullscreen mode

The -la flag lists the entire contents, along with their size, for the particular directory provided. And again, -h is for the human-readable format.
From the output for running the command for the /home/user/documents directory, we can see that bigapp.bin is indeed taking up a large space (1.2 gigabytes). If the file isn't that important, we could remove it to free up a good chunk of space.
Another very useful switch is the -t, which allows us to identify older or less frequently used files. This sorts the files according to their modification time and helps us recognize files that may be candidates for removal:

$ls -lt /another/directory
-rw-r--r-- 1 user user 2.3M Oct 21 10:00 important_file.txt
drwxr-xr-x 2 user user 4.0K Oct 21 09:30 my_directory
-rw-r--r-- 1 user user 1.5M Feb 20 15:45 old_file.txt
Enter fullscreen mode Exit fullscreen mode

In this example, the old_file.txt seems to have the earliest modification time. Therefore it signals to us that this file hasn't been used for a long time, and therefore we can remove it.

Conclusion

Monitoring disk space regularly in Linux isn't just a good practice; it's a necessity to keep the system running smoothly. Moreover, it helps prevent potential issues such as system slowdowns, data loss, and unexpected interruptions.
In this tutorial, we learned about four useful commands: df, du, ls, and fdisk. These create a complete toolkit to manage storage on Linux machines effectively.
To summarize, we discussed the capabilities of each of the commands, learned about their use cases, some of the necessary options to get the most out of them, and also saw practical examples on how to use them.

Top comments (0)