Essential Commands to Monitor Disk Usage in Linux

Monitoring disk usage is crucial for maintaining a healthy Linux system. By regularly checking disk usage, you can prevent disk space issues, identify large files and directories, and manage storage effectively. This article covers essential commands to monitor disk usage in Linux.

1. 'df'

The df (disk free) command displays the amount of disk space available on the file system:

df -h

The -h option provides human-readable output, showing sizes in KB, MB, or GB.

2. 'du'

The du (disk usage) command estimates file space usage. To check the disk usage of a directory and its contents:

du -sh /path/to/directory

The -s option summarizes the total size, and -h makes the output human-readable.

3. 'ncdu'

ncdu (NCurses Disk Usage) is an interactive tool that provides a visual representation of disk usage:

ncdu

If not installed, you can add it using your package manager (e.g., sudo apt install ncdu on Debian-based systems).

4. 'ls'

The ls command with the -lh option lists files and directories along with their sizes in a human-readable format:

ls -lh

5. 'find'

The find command can locate files based on size. For example, to find files larger than 100MB:

find /path/to/search -type f -size +100M

6. 'lsof'

The lsof (list open files) command lists open files and the processes that opened them. This can help identify which files are consuming disk space:

lsof +L1

This command lists open files that have been deleted but are still consuming disk space.

7. 'stat'

The stat command displays detailed information about a file or file system, including its size and storage location:

stat filename

8. inode Usage

The df -i command shows inode usage, which is essential for systems with many small files:

df -i

Inodes are data structures that store information about files, and running out of inodes can prevent new files from being created even if there is free disk space.

9. 'btrfs filesystem df'

If you are using a Btrfs file system, the btrfs filesystem df command provides detailed information about disk usage, including data, metadata, and system space:

btrfs filesystem df /mountpoint

10. Journaling File System Logs

For systems using journaling file systems like ext4, monitoring log sizes can be crucial. The journalctl --disk-usage command shows the size of the systemd journal logs:

journalctl --disk-usage

Conclusion

Monitoring disk usage is vital for maintaining a well-functioning Linux system. By using these commands, you can effectively manage your storage, identify potential issues, and ensure that your system runs smoothly. Whether you prefer simple commands like df and du or more advanced tools like ncdu and lsof, Linux offers a variety of options to suit your monitoring needs.