How to use the command 'df' (with examples)
- Linux
- December 17, 2024
The df
command, found in Unix and Unix-like operating systems, is a handy tool used for displaying the amount of disk space available on file systems. It helps system administrators and users to monitor disk usage efficiently, providing insights into available storage space across different mounting points and filesystem types. This can be particularly useful for managing storage resources on both personal and server environments, ensuring optimal usage and helping in planning for future storage expansions.
Use case 1: Display all filesystems and their disk usage
Code:
df
Motivation:
Using df
without any additional options is a quick and straightforward way to get a snapshot of all mounted filesystems and their current disk usage. This is particularly useful for getting a broad overview of the system’s storage state at a glance, which can help in identifying which filesystems might be nearing capacity.
Explanation:
The command df
without any options will list all mounted filesystems with their respective usage and available storage space. This includes the total size, used space, available space, percentage used, and the mount point.
Example output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 20482876 15093800 4317760 78% /
tmpfs 8165656 0 8165656 0% /dev/shm
/dev/sdb1 50293084 2121044 45654380 5% /backup
Use case 2: Display all filesystems and their disk usage in human-readable form
Code:
df -h
Motivation:
The -h
option, or --human-readable
, is incredibly useful for those who prefer to interpret disk space usage in standard units like MB, GB, or TB rather than in kilobytes, which is the default output. This enhances readability and allows for a more intuitive understanding of space allocation and usage.
Explanation:
The -h
option converts the disk space figures into a human-friendly format by using base-10 or base-2 unit suffixes like M for megabyte or G for gigabyte, making it easier to interpret for regular users.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 20G 15G 4.2G 78% /
tmpfs 7.8G 0 7.8G 0% /dev/shm
/dev/sdb1 48G 2.1G 44G 5% /backup
Use case 3: Display the filesystem and its disk usage containing the given file or directory
Code:
df /home/user/documents
Motivation:
This command comes in handy when you want to check the disk usage of the specific filesystem where a particular file or directory is located. It’s beneficial if you’re managing disk usage for multiple projects stored across different filesystems or when you suspect a specific application or directory is consuming too much space.
Explanation:
By specifying a path (in this case, /home/user/documents
), df
will report the filesystem where this particular file or directory resides. This is particularly useful if your system contains multiple filesystems, helping pinpoint exactly where space is being utilized most.
Example output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 20482876 15093800 4317760 78% /
Use case 4: Include statistics on the number of free inodes
Code:
df -i
Motivation:
Inodes are data structures used to store information about files, such as ownership, permissions, and file type. Even if there’s enough disk space available, running out of inodes can prevent new files from being created. Thus, monitoring inode usage is critical for ensuring that a filesystem can continue to store new files.
Explanation:
The -i
option, or --inodes
, instructs df
to report inode usage. This includes the total number of inodes, how many are used, and how many are left free, alongside the usage percentage of inodes for each filesystem.
Example output:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 1310720 540000 770720 42% /
tmpfs 1049340 14 1049326 1% /dev/shm
/dev/sdb1 3276800 17500 3269300 1% /backup
Use case 5: Display filesystems but exclude the specified types
Code:
df -x squashfs -x tmpfs
Motivation:
Filtering out specific types of filesystems like squashfs
, which is often used for compressed read-only filesystems, or tmpfs
, for temporary files, can make the output of df
more concise and relevant. This is particularly useful in environments with numerous virtual filesystems that might not be pertinent to a disk space audit.
Explanation:
The -x
or --exclude-type
option allows users to exclude certain types of filesystems from the output. In this command, it excludes filesystems of type squashfs
and tmpfs
, which are typically used for specialized purposes and might clutter the output when not needed for disk usage analysis.
Example output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 20482876 15093800 4317760 78% /
/dev/sdb1 50293084 2121044 45654380 5% /backup
Use case 6: Display filesystem types
Code:
df -T
Motivation:
Understanding the types of filesystems used on different partitions can be useful for troubleshooting and system optimization. Different filesystem types (e.g., ext4, xfs, ntfs) offer various features, and knowing the specific types can help in making informed decisions about maintenance and upgrades.
Explanation:
The -T
or --print-type
option extends the output to include the type of each filesystem. This adds an extra column indicating whether a filesystem is, for instance, ext4, tmpfs, or another type.
Example output:
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda1 ext4 20482876 15093800 4317760 78% /
tmpfs tmpfs 8165656 0 8165656 0% /dev/shm
/dev/sdb1 ext4 50293084 2121044 45654380 5% /backup
Conclusion:
The df
command is an essential utility for managing and monitoring disk space usage in any Linux-based environment. Its versatility allows users to gather a variety of detailed information about filesystem usage, potentially preventing storage issues before they arise. By knowing how to use its various options, system administrators and casual users alike can ensure their systems run smoothly and make more informed decisions regarding resource allocation and maintenance.