How to use the command 'df' (with examples)
- Osx
- December 17, 2024
The df
command in Unix-based systems is a powerful tool used to display information about disk space usage on mounted filesystems. It provides key details about available and used space, allowing users to effectively monitor and manage disk storage. This command is essential for system administrators and users who want to gain insight into how their storage resources are being utilized, ensuring optimal performance and capacity planning.
Use case 1: Display all filesystems and their disk usage using 512-byte units
Code:
df
Motivation:
Using the df
command without any options is the most straightforward way to get an overall picture of disk usage on a system. This default command displays the disk space usage for all filesystems in a 512-byte block format, which is useful when monitoring systems where this block size is standard.
Explanation:
- The command
df
on its own lists each mounted filesystem and provides columns with their filesystem name, 512-byte block size capacity, used and available space, usage percentage, and mount point.
Example output:
Filesystem 512-blocks Used Available Capacity Mounted on
/dev/disk1s1 976490432 243458432 700041000 26% /
devfs 1024 1024 0 100% /dev
Use case 2: Use human-readable units (based on powers of 1024) and display a grand total
Code:
df -h -c
Motivation:
The -h
flag is particularly useful for making disk space figures easier to comprehend by displaying them in human-readable formats, which translate bytes into Kilobytes (K), Megabytes (M), and Gigabytes (G). Adding the -c
option provides a cumulative total of all filesystems, helping users quickly understand overall disk usage.
Explanation:
-h
: Converts block sizes to human-readable form using powers of 1024.-c
: Adds a line at the end of the output with the total disk usage across all filesystems.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/disk1s1 466G 232G 426G 36% /
devfs 1.0K 1.0K 0 100% /dev
total 467G 233G 427G 36%
Use case 3: Use human-readable units (based on powers of 1000)
Code:
df --si
Motivation:
Users who prefer disk size representations based on the metric system (powers of 1000) might find the --si
flag more intuitive, showing sizes in Kilobytes (KB), Megabytes (MB), and Gigabytes (GB), which align with many standard industry metrics.
Explanation:
--si
: Converts block sizes to human-readable form using powers of 1000, rather than 1024, which is the traditional standard in some contexts.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/disk1s1 500G 249G 451G 36% /
devfs 1.1k 1.1k 0 100% /dev
Use case 4: Display the filesystem and its disk usage containing the given file or directory
Code:
df path/to/file_or_directory
Motivation:
When issues arise pertaining to space on specific directories or files, this version of the command becomes invaluable. It helps pinpoint which filesystem contains the directory or file and provides crucial information about its available and used space.
Explanation:
- By specifying a path, the command only shows information for the filesystem that contains the given file or directory, allowing focused analysis.
Example output:
Filesystem 512-blocks Used Available Capacity Mounted on
/dev/disk1s1 976490432 243458432 700041000 26% /
Use case 5: Include statistics on the number of free and used inodes including the filesystem types
Code:
df -iY
Motivation:
Inodes represent filesystem objects such as files and directories. Systems with a large number of small files might run out of inodes before disk space, leading to issues creating new files. This command helps users monitor these resources and also displays the type of filesystem.
Explanation:
-i
: Shows the number of inodes used and available.-Y
: Provides extra details about the filesystem types, aiding in recognizing the structure and characteristics of the system.
Example output:
Filesystem Type Inodes IUsed IFree IUse% Mounted on
/dev/disk1s1 apfs 4882452 1538694 3343758 32% /
devfs devfs 600 600 0 100% /dev
Use case 6: Use 1024-byte units when writing space figures
Code:
df -k
Motivation:
Displaying sizes in 1024-byte blocks is preferred by those who favor traditional binary subdivisions over the default 512-byte units. It provides clarity when calculating space in the more commonly used kilobyte (binary) terms.
Explanation:
-k
: Adjusts the display to show block sizes in 1024-byte units, aligning with kilobyte increments.
Example output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/disk1s1 488245216 121729216 350020500 26% /
devfs 512 512 0 100% /dev
Use case 7: Display information in a Portable way
Code:
df -P
Motivation:
The -P
option ensures the output is formatted consistently, regardless of the operating system or language settings. This is particularly useful in scripting or when data needs to remain understandable across different systems and locales.
Explanation:
-P
: Produces information in a standardized format, optimizing scripts and output parsing for consistency.
Example output:
Filesystem 1024-blocks Used Available Capacity Mounted on
/dev/disk1s1 488245216 591729216 350020500 26% /
devfs 512 512 0 100% /dev
Conclusion
The df
command provides essential information for understanding disk space usage and filesystem status in Unix-based systems. From monitoring available space to ensuring enough inodes are available, the various options of df
enable users to maintain smooth operation of their systems. By using these commands strategically, you can effectively manage your disk space and anticipate potential issues before they become critical.