Key Takeaway: The df command in Linux is a crucial tool for managing disk space, providing essential information about total, used, and available space on mounted file systems. By mastering the usage of df, users can efficiently monitor disk space utilization and ensure optimal system performance.
Whether you’re managing a system as an administrator or simply using a desktop, it’s crucial to stay informed about your available disk space. While this can be easily done using a regular file manager on a Linux desktop, understanding the command line can significantly enhance your experience.
Linux, known for its command-line friendliness, offers a handy utility called df for precisely this purpose. This command reveals the available disk space on the file system containing each specified file name argument.
This article delves into the comprehensive use of the df command in Linux, providing practical examples to help you grasp its functionality and importance in managing disk space effectively.
So, without any further ado, let’s explore how you can leverage this command-line utility in Linux environments.
What is the df command?
The df command, short for disk free, is a powerful tool for retrieving information about disk space utilization in a Linux system. It provides details on both total and available space within a file system.
The FileSystem parameter allows you to specify the name of the device hosting the file system, the directory where it’s mounted, or the relative path of a file system. Meanwhile, the ‘File’ parameter enables you to focus on specific files or directories rather than entire file systems. When no parameters are specified, the df command displays information for all currently mounted file systems.
By default, the df command presents file system statistics in units of 512-byte blocks. It obtains these statistics primarily from the statfs system call. However, appending the ‘-s’ flag allows you to retrieve statistics from the virtual file system (VFS) specific file system helper.
In cases where no arguments accompany the ‘-s’ flag and the helper fails to fetch statistics, df resorts to using the statfs system call data. It’s worth noting that during certain exceptional circumstances, such as file system modifications while df is executing, the displayed statistics may not be entirely accurate.
Note – It’s important to note that certain remote file systems, such as the Network File System (NFS), may not furnish all the required information for the df command. In such cases, df will print blank spaces for any statistics that the server fails to provide. This can occur due to limitations or configurations specific to the remote file system.
Purpose
The df command in Linux is widely used by system administrators to display various types of storage information on a file system, including but not limited to –
- Viewing used space.
- Checking available disk space.
- Checking how many filesystems have been mounted.
- Listing the number of available inodes for a Linux or Unix box.
- Understanding if the disk capacity has been fully utilized on a partition or connected devices.
- Checking if sufficient space is available on a partition before installing or upgrading apps.
- Configuring a shell script or a Perl script to trigger alerts when your storage space runs out of space.
Syntax of the df command in Linux
The fundamental syntax of the df command is as follows –
df [options] [filesystems]
Here’s a breakdown of the components:
- options: These are optional flags that can be used to customize the output of the command. I have listed the available flags in detail below.
- filesystems: You have the option to specify particular filesystems (mount points) to check their usage instead of obtaining information for all mounted drives.
Note: If no file name is provided, the df command will display the space available on all currently mounted file systems.
Consider the following, for example:
df
Executing this command will provide information about all mounted Filesystems, including details such as 1K-blocks, used space, available space, usage percentage, and the mount point.
The output will be presented in a table format with columns for:
- Filesystem: Indicates the name of the mounted storage device (e.g., /dev/sda4).
- Size: Represents the total size of the filesystem in bytes.
- Used: Shows the amount of space currently occupied by data in bytes.
- Available: Displays the amount of free space available in bytes.
- Use%: Indicates the percentage of the filesystem used.
- Mounted on: Specifies the directory where the filesystem is mounted (e.g., /, /home).
If you specify a particular file, the command will show the mount information specific to that file.
For example:
df asktheneek.pdf
Here, you can replace the “asktheneek.pdf” with any other file of your choice.
Options/Flags Available in the df Command
There are various flags available in the df command to help configure this utility for desired results.
Here are some of the most common ones –
-a, –all | Includes pseudo, duplicate, and remote file systems. |
-B, –block-size=SIZE | Scales sizes by SIZE before printing them. For example, using ‘-BM’ prints sizes in units of 1,048,576 bytes. |
-h, –human-readable | Displays sizes in powers of 1024 (e.g., 1023M). |
-H, –si | Shows sizes in powers of 1000 (e.g., 1.1G). |
-i, –inodes | Lists inode information instead of block usage. |
-l, –local | Limits the listing to local file systems. |
–no-sync | Does not invoke sync before retrieving usage info (default behavior). |
–output[=FIELD_LIST] | Uses the output format defined by FIELD_LIST or prints all fields if FIELD_LIST is omitted. |
-P, –portability | Uses the POSIX output format. |
–total | Excludes all entries insignificant to available space and produces a total. |
-t, –type=TYPE | Limits the listing to file systems of type TYPE. |
-T, –print-type | Displays the file system type. |
-x, –exclude-type=TYPE | Limits the listing to file systems not of type TYPE. |
–help | Displays a brief manual containing information about the supported options. |
–version | Displays version information of the df command. |
Practical Examples of the df Command in Linux
Now that you have learned all the basics of the df command, let me walk you through the best practical examples of it –
Check Linux Disk Space Usage
The df command can be used to check the disk space usage in Linux.
When you execute this command without any options or parameters, it provides information about device names, total blocks, total disk space, used disk space, available disk space, and mount points on a file system.
To view this information, simply run:
sudo df
Here’s a breakdown of the columns displayed in the output:
- Filesystem: Indicates the mount point name.
- 1K-blocks: Represents available total space counted in 1 kilobyte (1000 bytes).
- Used: Indicates the used block size.
- Available: Displays the free block size.
- Use%: Shows the usage percentage.
- Mounted on: Specifies the path of the mounted point.
Check Information on Linux Disk Space Usage
The df -a
command is used to retrieve comprehensive information about all mounted filesystems’ disk space usage. This includes details such as total space, used space, available space, memory utilization, and the percentage of space used for each filesystem.
Executing it provides a comprehensive overview, encompassing pseudo filesystems with 0 blocks, which are typically not directly associated with a physical device and are ignored by default.
df -a
Note: The output of the df -a command mirrors that of the previous command but with the inclusion of all pseudo filesystems alongside real ones. Linux dynamically creates and populates filesystems based on system functionalities, often referred to as pseudo filesystems. These pseudo filesystems generate fake or non-real files according to the current system’s operations and are typically not present in the rootfs filesystem of the distribution image. Examples include proc, sysfs, and others.
Show Disk Space Usage in Human Readable Format
Have you noticed that the commands above display information in bytes? While technically accurate, this format isn’t very user-friendly because most of us are accustomed to reading sizes in megabytes, gigabytes, and so on, which are easier to understand and remember.
For this, you can use df -h
command where -h stands for “human-readable.” This allows you to view disk space usage in a format that’s much more intuitive. It presents information about all mounted filesystems on your system, expressing sizes in units like kilobytes (KB), megabytes (MB), gigabytes (GB), and so forth, making it easier for humans to grasp.
df –h
See, running df -h displays all outputs in a human-readable format. Compare this to the same command without the -h option, and you’ll notice a significant difference in readability. Sizes measured in GB are much simpler to comprehend than those in bytes or kilobytes.
df -H
Moreover, there’s also the df -H option. This variant lists sizes in powers of 1000 rather than 1024, resulting in slightly different disk space measurements compared to the df -h.
Check Disk Space Usage of /home Directory
If you want to check the disk space usage of the home directory, you can execute the df -hT /home command.
This command displays the disk space utilization of the /home directory or partition in a format that is easy for humans to understand, thanks to the -h option. Additionally, the -T option provides details about the filesystem type, such as ext4, along with other pertinent information.
df -hT /home
Sum up the Total Disk Space Usage
By employing the configured command df -h –total, you can aggregate the total utilized and available disk space, providing valuable insight into whether storage upgrades are necessary or if unneeded files should be removed.
df -h --total
Check Disk Space Usage in Kilobytes
To display comprehensive information about all mounted filesystems and their usage in 1024-byte blocks, you can employ the option -k (e.g., –block-size=1K). This option presents details about each filesystem on your system, displaying sizes in kilobytes (KB).
df -k
Check Disk Space Usage in Megabytes
Like KB, you can also get details about all file system usage in megabytes (MB). For this, you can utilize the option -m with the df command.
This option presents sizes in megabytes (MB).
df -m
Check Disk Space Usage in Gigabyte
Lastly, there is also a way to get details about file system usage in gigabytes (GB).
For this, you can simply execute the command df -h. As we already know, this command displays disk space information in a human-readable format, presenting sizes such as kilobytes (KB), megabytes (MB), gigabytes (GB), and beyond.
df -h
List the Inodes Information for All File Systems
Executing the command df -i offers a detailed breakdown of the number of inodes used and available on every mounted filesystem within a Linux system. Inodes serve as data structures storing essential information pertaining to files and directories, encompassing details like ownership, permissions, and timestamps.
df -i
Note: If there is a shortage of inodes in your machine, it becomes important to increase the inode count on your Linux filesystem. If you require further clarification on what inodes entail, you can access additional details by clicking here.
List Only the Local File System
To list only the local filesystems, you can use the -l option with the df command. This option limits the listing to local filesystems only.
df -l
Check Linux File System Type
Observing the output of the aforementioned commands, you might have noticed the absence of Linux filesystem types in the results.
To get details about the file system types on your system, you can employ the option -T with the df command. This presents the file system type alongside other relevant information.
df -T
Note: Utilizing this option provides a comprehensive list of file system types. In the resulting output, you can identify the file system types under the TYPE column, such as ext4, tmpfs, or vfat, as illustrated in the examples above.
Check the Disk Space Details of a Specific File System Type
To view disk space usage exclusively for a specific file system type, you can use the -t option with the df command.
For example, executing the df -t ext4
will specifically display information regarding filesystems of type ext4.
df -t ext4
Note: By employing this option, you are filtering the output to focus solely on a particular file system type. In the provided example, ext4 is specified, but you can adjust the file system type as per your requirements.
Exclude Certain File System Type
To display disk space usage for all mounted filesystems, excluding those of type ext4, you can use the option -x with the df command.
For instance, running sudo df -x ext4
will print information about non-ext4 filesystems only.
df -x ext4
Note: By employing the -x option, you are excluding specific file system types from the output list. In this example, the ext4 file system type is excluded. You can compare this output with the previous example, which includes all filesystem types, including ext4.
Customize the Output
You further get the ability to customize the output of the df command in Linux by utilizing the double dash option (–) followed by an equal (=) sign and a comma-separated list of columns you’re interested in.
For instance, if you wish to display only the Filesystem, Available space, and Usage percentage, you can use the columns ‘source’, ‘avail’, and ‘pcent’ respectively.
df --output=source,avail,pcent
Understand Different Use Cases
Although I have covered most of the common use cases of the df command, this command is highly versatile.
To learn what else you can do with this command, you can execute the df –help in the terminal. This command serves as a valuable resource for understanding the various use cases and functionalities of the df command, enabling you to become more proficient in its usage.
df --help
Alternatives to df Command in Linux
Although the df command is an effective tool for disk space management in Linux, there exist alternative commands and techniques that offer distinct approaches. Among these alternatives are the du command and the ncdu tool, each boasting unique features and use cases.
Checking Disk Space with DU Command
The du command, an abbreviation for disk usage, serves as a valuable tool for examining the sizes of directories and files in Linux. Differing from the df command, which reports disk usage of filesystems, du calculates the space usage of individual files and directories.
Here’s an example illustrating the usage of the du command:
du -sh /home/user
In this example, the command du -sh /home/user estimates the total disk space consumed by the /home/user directory. The -s flag summarizes the result, while the -h flag renders the size in a human-readable format.
Using NCDU for Disk Usage Analysis
An alternative yet robust tool for disk usage analysis is ncdu, which stands for NCurses Disk Usage. It’s a ncurses-based utility that offers a swift method to visualize disk space allocation across directories.
To utilize ncdu, you’ll need to install it first.
Below is the command to install ncdu on Ubuntu:
sudo apt-get install ncdu
Once installed, you can leverage ncdu to delve into your disk usage patterns. Note that the installation process could vary depending on the Linux distro you have on your machine.
Here’s an example:
ncdu /home/user
In this example, the command ncdu /home/user scrutinizes the /home/user directory and presents the disk usage in an intuitive format, conducive to user-friendly analysis.
While the df command remains a potent tool for disk space management in Linux, it’s worth exploring or at least knowing other commands and utilities like du and ncdu for their alternative approaches.
Challenges with the df Command
As with any tool, employing the ‘df’ command in Linux may occasionally pose challenges.
Here, I have addressed the most common issues you may face while using the df command, offering solutions and tips to effectively navigate through these obstacles.
Disk Usage Doesn’t Match Expectations
Occasionally, you might encounter discrepancies between the disk usage reported by the df command and your expectations. Such discrepancies could arise from deleted files still in use by a process or due to reserved disk space in Linux filesystems.
Reserved space, by default, is allocated in Linux filesystems, typically constituting 5% of the total filesystem capacity. This space is reserved for the root user and system services, serving as a buffer to prevent system service disruptions stemming from insufficient disk space.
Inconsistent Results between DF and DU Commands
Another prevalent issue involves discrepancies in results between the df and du commands. While the df command presents disk space usage of filesystems, the du command estimates the usage of files and directories. Because of their distinct methodologies, they may occasionally display varying results.
Such differences can occur from several factors, including deleted files that remain in use, discrepancies in block size calculations, or variations in filesystem metadata.
DF Command Shows No Space When There Is
Occasionally, the df command may indicate a lack of available disk space even when space is indeed present. This issue often arises from inode exhaustion.
In a Linux filesystem, each file or directory necessitates an inode. When all available inodes are consumed, no additional files or directories can be created, regardless of the presence of free space.
Frequently Asked Questions
What is the df command used for?
The df command in Linux serves to exhibit details concerning file system disk space usage. It is used to display information such as the total, used, and available space on mounted file systems.
How do I use the df command to display disk space information for a specific file system?
To showcase disk space details for a particular file system, you can employ the -h option to render human-readable output and designate the file system path as an argument. For instance: df -h /dev/sda1
How do the df and du commands differ in their purpose?
The df command offers a broad view of file system disk space usage, furnishing details on total, used, and available space. Conversely, the du command estimates disk usage for directories and files, enabling a more granular analysis at the directory and file level.
Can the df and du commands be used together?
Indeed, both the df and du commands serve distinct purposes and can complement each other to provide comprehensive insights into disk space utilization. While df offers a broad overview, du facilitates a detailed examination of disk usage at the directory and file level.
Conclusion
There you have it – a detailed article covering information on the df command in Linux.
The df command stands as a standard tool for accessing disk space information in Linux-like systems.
With this command, you can retrieve details regarding currently mounted file systems, total space, human-readable format, disk space in specified units, and the disk space of specific file systems. This article offers insights into the syntax and provides numerous examples of utilizing the df command.