- Posted on
- • commands
Checking Disk Usage with `df` and `du`
- Author
-
-
- User
- Linux Bash
- Posts by this author
- Posts by this author
-
When managing a Linux or Unix-based system, knowing how to check the available disk space and understand how much space each file and directory is using can be very beneficial. This is particularly important as your system stores more data; keeping an eye on your disk utilization is key to ensuring that your system runs smoothly without running out of disk space unexpectedly. Two powerful, commonly used command-line tools that can help you monitor disk usage are df
and du
.
Understanding df
Command
The df
tool stands for "disk free" and is used to display the amount of available disk space for file systems on which the system has mounted file systems. This tool is very straightforward and provides a snapshot of current disk usage with several useful options. When you run df
, the output typically includes information about the file system type, the total space, the used space, the free space, and the mount point of each file system.
Basic Usage:
Simply type
df
in the terminal to see a list of all mounted file systems along with their size, used space, and available space. It is usually displayed in 1-kilobyte blocks unless specified otherwise.To view the disk space usage in human-readable form, you can use
df -h
(e.g., in GBs and MBs). This makes it much easier to read and understand how much space is available at a glance.
Examples:
$ df
$ df -h
Exploring du
Command
The du
command stands for "disk usage." Unlike df
, which provides the amount of disk space left on your disk, du
is used to check the space used by specific files and directories within the file system. This is particularly useful for finding out which files or directories are using up most of the disk space.
Basic Usage:
To check the disk usage of a particular directory, you can use
du /path/to/directory
. This will show the disk usage for each subdirectory in the specified directory.To get the sum total of a directory's size, you can use
du -sh /path/to/directory
. The-s
flag summarizes the total, and-h
makes the output human-readable.
Examples:
$ du /var
$ du -sh /home/user/Documents
Practical Applications
1. Monitoring overall system space: Use df -h
to continuously monitor the system's disk space, especially on servers and in production environments where disk space is critical.
2. Finding out the largest files/directories: Use du -ah | sort -rh | head
to list the largest items in the current directory. It helps in quickly identifying which files or directories you might want to clean up.
3. Allocating resources: Before installing new applications or when planning backups, use df
and du
to estimate space requirements and ensure that your system has enough space to handle these operations.
Summary
Both df
and du
are essential tools for disk usage analysis in Unix-like operating systems. Whether you're a system administrator monitoring server loads or a regular user trying to clean up your laptop, understanding these commands can greatly help in managing your system's disk space efficiently. Remember, managing disk space effectively is not just about freeing up space but also about optimizing the performance of your system.