Understanding the 'df' Command (with examples)
- Netbsd
- December 17, 2024
The df
command, short for “disk free,” is a vital utility in Unix-based systems used to display the available and used disk space on mounted filesystems. It’s an essential tool for system administrators and users who need to monitor disk usage, manage storage, and ensure that their filesystem operations proceed smoothly without running into storage limitations. Available across numerous Unix-like operating systems, df
provides a diverse set of options to tailor the information displayed, making it adaptable to various needs and use cases.
Display all filesystems and their disk usage using 512-byte units
Code:
df
Motivation:
Running the command df
without any arguments provides a quick overview of all mounted filesystems and their respective disk usages in 512-byte units. This is particularly useful for administrators or users who need a rapid audit of storage across all mounted resources. By default, most systems opt for 512-byte units as the standard block size, making this a straightforward choice when precision isn’t required and speed is of the essence.
Explanation:
df
: Invokes the disk free utility to scan and report on disk usage across all mounted filesystems.
Example Output:
Filesystem 512-blocks Used Available Capacity Mounted on
/dev/disk1s1 1953456784 128934008 1393467984 10% /
devfs 1232 1232 0 100% /dev
Use human-readable units (based on powers of 1024)
Code:
df -h
Motivation:
Deploying df -h
enhances readability by converting the output into human-friendly sizes (e.g., KB, MB, GB). This is indispensable when sharing information with individuals who prefer more intuitive formats rather than deciphering raw byte or block counts. If a user wants a quick glance at disk space without detailed calculations, this option presents data in a universally understandable format.
Explanation:
-h
: Stands for “human-readable”, which organizes the disk usage data into adjustable size units that are easy to interpret.
Example Output:
Filesystem Size Used Avail Capacity Mounted on
/dev/disk1s1 932Gi 616Gi 667Gi 48% /
devfs 612Ki 612Ki 0Bi 100% /dev
Display all the fields of the structure(s) returned by statvfs
Code:
df -G
Motivation:
Utilizing df -G
allows advanced users or developers to see the full range of fields provided by the statvfs
system call, which are not usually visible through standard output options. These fields, including block sizes and flags, serve as a foundation for deeper insights into system file operations and potentially aid in debugging or advanced filesystem diagnostics.
Explanation:
-G
: Enables an expanded view of disk and filesystem statistics as provided by the underlyingstatvfs
system structure, showing additional technical details.
Example Output:
(Note: Exact fields can vary based on system and configuration)
Filesystem BlockSize Blocks Free Inodes Flags
/dev/disk1s1 4096 1953453996 1727386797 977038848 1024 flags
Display the filesystem and its disk usage containing the given file or directory
Code:
df path/to/file_or_directory
Motivation:
This specific command usage comes handy for users who need to quickly identify and understand storage utilization associated with a single file or directory. Whether investigating storage issues or planning resource allocation, it’s crucial to isolate precise locations in the filesystem directly linked to a particular directory or file.
Explanation:
path/to/file_or_directory
: Pointsdf
to report relevant filesystem statistics for the particular file or directory specified.
Example Output:
Assuming a file at /usr/local/bin/script.sh
Filesystem 512-blocks Used Available Capacity Mounted on
/dev/disk1s1 1953456784 616038008 1393467984 30% /
Include statistics on the number of free and used inodes
Code:
df -i
Motivation:
By invoking df -i
, users can delve into inode statistics, which are pivotal in understanding filesystem capacity limitations beyond just disk space. Inodes represent filesystem indexing entries, and their exhaustion indicates filesystem maximum capacity even if plenty of disk space remains. System administrators monitoring filesystem health and integrity will find this feature immensely beneficial.
Explanation:
-i
: Adds inode usage statistics to the standard output, showcasing used and available inodes for each filesystem.
Example Output:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/disk1s1 48833825 150000 48683825 1% /
devfs 1232 1232 0 100% /dev
Use 1024-byte units when writing space figures
Code:
df -k
Motivation:
The df -k
option becomes particularly useful when raw byte precision is necessary, but users prefer a convenient 1024-byte-based accounting style. This command suits scenarios involving memory management, storage auditing, or when consistency with other metrics (such as RAM) is needed.
Explanation:
-k
: Configures the output to display disk space figures in 1024-byte units, diverging from the default 512-byte blocks.
Example Output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/disk1s1 953675392 308019004 696733992 31% /
devfs 612 612 0 100% /dev
Display information in a Portable way
Code:
df -P
Motivation:
The df -P
format provides output that remains consistent across a wide range of systems, meeting the Portable Operating System Interface (POSIX) standards. This ensures that scripts and tools can reliably parse the result across different platforms, a necessity for those writing cross-platform utilities or scripts reliant on standardized input.
Explanation:
-P
: Alters the format to comply with POSIX, ensuring portable and script-friendly outputs.
Example Output:
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/disk1s1 953675392 308019004 696733992 31% /
devfs 612 612 0 100% /dev
Conclusion:
The df
command is a versatile and widely utilized tool that offers comprehensive insights into disk usage across filesystems. Its variety of options allows tailoring to specific needs, ranging from simple disk space checks to more intricate evaluations involving inodes or POSIX compliance. For users and administrators alike, mastering the df
command ensures informed disk management and optimizes the performance and reliability of computing environments.