Understanding 'df' Command (with Examples)
- Openbsd
- December 17, 2024
The df
(disk free) command is a pivotal utility in Unix-like operating systems. It provides a synopsis of the filesystem’s disk space utilization, assisting administrators and users in effectively monitoring and managing disk space. By leveraging various options and arguments, df
can deliver diverse insights into disk space usage, tailored to specific requirements. This article delves into numerous practical use cases of the df
command, elucidating its utility in distinct scenarios.
Use Case 1: Display All Filesystems and Their Disk Usage Using 512-Byte Units
Code:
df
Motivation:
Using the df
command without any arguments is an efficient way to garner a general overview of the disk usage across all mounted filesystems. This is particularly beneficial for system administrators or users who need a quick snapshot of how disk resources are being allocated or need to troubleshoot disk space issues.
Explanation:
When executed without options, df
reports the amount of disk space used and available on all current filesystems in terms of 512-byte blocks, which is a traditional Unix standard format.
Example Output:
Filesystem 512-blocks Used Avail Capacity Mounted on
/dev/sda1 10240000 4000000 6052000 40% /
tmpfs 2048000 0 2048000 0% /dev/shm
/dev/sda2 5120000 2560000 2560000 50% /home
Use Case 2: Display All Filesystems and Their Disk Usage in Human-Readable Form
Code:
df -h
Motivation:
This variant is invaluable when quickly assessing disk usage in a manner that’s intuitive and easy to understand. It automatically converts the output into human-readable values, displaying it in terms of MB, GB, etc., thus providing a user-friendly overview of disk consumption.
Explanation:
The -h
flag stands for “human-readable”, formatting the output by converting block sizes to more easily comprehended units such as kilobytes (K), megabytes (M), and gigabytes (G).
Example Output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 10G 3.8G 5.8G 40% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
/dev/sda2 5.0G 2.5G 2.5G 50% /home
Use Case 3: Display the Filesystem Containing a Specific File or Directory
Code:
df path/to/file_or_directory
Motivation:
This approach is essential when wanting to understand the disk usage related to a specific file or directory. It helps determine where and how storage resources are allocated for specific sets of files, assisting in managing or optimizing space usage.
Explanation:
The specified path/to/file_or_directory
argument directs df
to display disk usage information solely for the filesystem containing the given path, thereby zeroing in on a particular section of storage and its resources.
Example Output:
For a file located at /home/user/example.txt
:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 5120000 2560000 2560000 50% /home
Use Case 4: Include Statistics on Free and Used Inodes
Code:
df -i
Motivation:
Inodes are data structures that store information about files and directories. Viewing inode statistics is crucial when diagnosing filesystem issues not related to disk block availability but rather to inode exhaustion, such as when a filesystem has free space but cannot create new files due to a lack of available inodes.
Explanation:
The -i
option extends df
’s capabilities by including inode usage statistics alongside block usage. This adds an extra tier of detail for evaluating the state of a filesystem.
Example Output:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 256000 80000 176000 31% /
tmpfs 100000 1 99999 1% /dev/shm
/dev/sda2 128000 60000 68000 47% /home
Use Case 5: Use 1024-Byte Units When Displaying Space Figures
Code:
df -k
Motivation:
This use case, which specifies the block size explicitly, is often preferred for compatibility with other utilities or scripts that expect input in 1024-byte (1K) units, aligning the output more closely with the commonly used binary system for memory units.
Explanation:
The -k
switch directs df
to convert its output to 1024-byte blocks, ensuring consistency with applications and scripts that expect disk usage data in these units.
Example Output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 10000000 4000000 6000000 40% /
tmpfs 2000000 0 2000000 0% /dev/shm
/dev/sda2 5000000 2500000 2500000 50% /home
Use Case 6: Display Information in a Portable Way
Code:
df -P
Motivation:
Portability is key when sharing disk usage reports or integrating them into cross-platform applications or documentation. The -P
option ensures that the output is presented in a standardized format that is consistent across different environments.
Explanation:
The -P
flag guarantees that df
’s output adheres to the POSIX standard, which strips away complications arising from variations in display formatting across Unix-like systems, allowing for straightforward parsing.
Example Output:
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/sda1 10240000 4000000 6000000 40% /
tmpfs 2048000 0 2048000 0% /dev/shm
/dev/sda2 5120000 2560000 2560000 50% /home
Conclusion
The df
command, with its rich array of options, presents itself as an indispensable tool for those mindful of disk space management. Each use case, ranging from high-level overviews to targeted inspections of file-specific utilization, equips users with the flexibility needed for effective disk space analysis. Embracing these variations allows system administrators and savvy users alike to maintain optimal storage conditions on their systems.