Guide to Monitoring Disk Usage on Linux


Introduction

It is essential for Linux users to monitor our system's disc usage to make sure we have enough storage for our programmes and files. Monitoring disc consumption can also aid in problem detection and performance enhancement for our system. In this article, we will look at four techniques for tracking Linux disc utilisation. We will go over the df tool to quickly see how much disc space is available, the du programme to keep track of how much disc space is being used by particular directories or files, the ncdu function to see a more interactive graphical representation of disc utilization, and the iostat command to get disc I/O statistics. These techniques allow us to monitor the disc consumption on our system and guarantee that we have enough disc space for our need.

Using the ‘df’ Command to Monitor Disk Usage

The “df” command is a simple yet effective tool for monitoring disc usage in Linux. It allows us to view the available disc space and utilisation information for each file system that is mounted on our system. We can rapidly identify any potential disc space concerns and take the appropriate procedures to free up space or assign additional storage by using the df program.

To use the “df” command,we have to open and enter the following command in our terminal −

$ df -h

The “-h” option converts the output to human-readable text. This will provide a table with the file system, total size, used space, available space, and percentage of available space. This data can assist us in determining which file systems are taking up the most space and require attention.

Filesystem      Size  Used Avail Use% Mounted on
udev            1.9G     0  1.9G   0% /dev
tmpfs           389M  1.8M  388M   1% /run
/dev/sda5        20G   13G  5.5G  70% /
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/loop0      128K  128K     0 100% /snap/bare/5

The “df” command on Linux is a simple yet effective way to monitor disc usage. We can ensure that we always have enough disc space for our applications and files by including this tool into our normal system maintenance plan.

Using the ‘du’ Command to Monitor Disk Usage

Another useful tool for measuring disc usage in Linux is the du command. Unlike the df function, which reports on overall disc usage, the du program lets us examine the disc usage of specific directories or files. This is useful for determining which directories or files are taking up the most space on our system.

To use the du command, start a terminal and navigate to the directory where we want to examine the disc usage. Then we can type the following command −

$ du -h

This will display a human-readable list of all the directories and files in the current directory, along with their sizes. This information can be used to determine which directories or files are using up the most space on our system.

4.0K	./Public
…Many more files..
60K	./.cache/fontconfig
7.4M	./.cache/tracker

We may also combine the du -h command with other Linux commands to obtain more detailed information on disc usage. We can use “sort” command to sort the result by size −

$ du -h | sort -h

This will sort the directories or files from smallest to largest on our system.

 .0K	./.aws
4.0K	./.cache/evolution/addressbook/trash
……Many more files….
8.0K	./.local/share/Trash/info
12K	./.cache/vmware

So this command is a powerful tool for monitoring disk usage on Linux.

Using the “ncdu” Command to Monitor Disk Usage

The "ncdu" command provides a more interactive way to monitor disc usage. It shows a graphical respresentation of the disc usage of each file and directory on our system.

We must first install the ncdu command using our distribution's package management.

$ sudo apt-get install ncdu

Once “ncdu” is installed on our system, we can run this command in our terminal −

$ ncdu /

The command will result in a graphical interface with information on the disc usage of each file and directory on your system. The arrow keys are used to traverse through the directories, while the d key is used to delete files or directories.

ncdu 1.14.1 ~ Use the arrow keys to navigate, press ? for help                  
--- / --------------------------------------------------------------------------
  5.2 GiB [##########] /usr                                                   
  923.3 MiB [#         ]  swapfile
. 169.9 MiB [          ] /boot
Total disk usage:  16.2 GiB  Apparent size:  16.6 GiB  Items: 641978

Using the “find” Command to Monitor Disk Usage

On Linux, the “find” command can also be used to monitor disc usage. We may use the “find” command to look for files depending on their size, modification time, and other characteristics.

To use the df command,we have to open and enter the following command in our terminal −

$ find . -type f -exec ls -lh {} \;

This command will search for and display the sizes of all regular files in the current directory and its subdirectories. The -type f argument directs find to exclusively search for normal files, while the -exec option directs find to execute the ls -lh command for each file discovered.

-rw-r--r-- 1 papan papan 807 Feb 17 20:53 ./.profile
-rw------- 1 papan papan 0 Feb 25 01:13 ./.aws/config

Conclusion

In conclusion, monitoring disc usage on Linux is critical for managing our system's health and performance. We can easily identify and manage disc space usage on our Linux operating system using the methods explained in this article, including the df, du,nsdu, and find command.

Updated on: 08-May-2023

413 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements