df command in Linux


Want to get an insight into the storage space (free and used) of your Linux system? Don’t worry we are here with the df command.

The df “disk free” command represents the free and used space on a Linux system. You get quick stats of your mounted file systems. Today, we will list the working and functionality of the df command in Linux.

Outline:

df Command in Linux

The df command provides the overview of the storage occupied by each file system. The stats include the free disk space, total disk space, mount details, etc. However, you can get the output as per your requirements by using different options of the df command.

Let’s first understand the syntax and basics of df command:

Syntax of the df Command

The df command has the following syntax:

df [Options] [File]

The [Options] and [File] are the optional parameters. If these are not provided, the “df” command automatically prints the details of all the mounted filesystems.

Options Supported By the df Command

The options of the “df” command enhance its overall functionality. Let’s have a look at the supported options before you dig into examples:

Option Description
-a or –all Prints info of all types of filesystems (even inaccessible)
-B or –block-size=<size> Get the total capacity of the filesystems in custom block size, i.e., 1k=1024 bytes, 2K=2048 bytes, and so on
–total Prints the total of all the columns at the end
-h or –human-readable Prints the values in the human-readable format, i.e., mbs, kbs
-H or –si An extension of the human-readable format but uses the power of 1000.
-i or –inodes Prints the inode information, i.e., index node stores the information such as file type, size, permissions, ownership, etc
-l or –local Show only the local file systems.
-t or –type=<FS-Type> Prints only the specific file types’ information
-T or –print-type Prints file system type

For more options, you can use the “-help” flag of the df command:

Let’s dig into the use cases of the “df” command.

Example 1: Get the Free, Used, or Total Disk Space of a Linux System

The “df” command prints the statistics of all the mounted file systems on your Linux system. However, the output can be configured by using various options of the command. This example provides the basic output and configuring the output with various options.

Example 1.1: Basic Usage of the df

Look at the below command (being executed without options) to have basic usage of the “df” command:

df

The output of the command contains five columns, i.e., “Filesystem”, “1K-blocks”, “Used”, “Available”, “Use%”, and “Mounted on”.

  • Filesystem: The name of the mounted file system, i.e., “/dev/sda2” refers to the physical storage attached, and the “tmpfs” is the temporary storage (RAM).
  • 1K-blocks: Total capacity of each filesystem in 1K (1024 bytes)-blocks.
  • Used: The storage occupied by each filesystem.
  • Available: Free space on that specific filesystem
  • Use%: The used percentage of each filesystem.
  • Mounted on: The location where the filesystem is mounted.

With the df command, you not only get the used or free space but a complete view of the filesystems.

Example 1.2: Get the Custom Block Size

The “df” denotes the block size in 1K unit. However, you can specify as per your requirement. For instance, the below command segregates the total capacity in 3072 Bytes (3K) Blocks:

df -B 3072

The output has converted the 3072 Bytes into 3K, as can be seen in the output.

Example 1.3: Get the Human Readable Output

By default, the output shows the measuring unit 1K-blocks. You can get a more understandable output using the “-h” flag of the “df” command (which actually converts the measuring unit to KBs, MBs, and GBs)

df -h

The “-h” flag uses the power of 1024 for each measuring unit.

If you see the output, the “blocks” column has been replaced with the “Size” and the measuring unit is more human-readable than its default behavior (blocks-based).

Note: You can use the “-H” flag but it gives the measuring unit in power of “1000”.

Example 2: Analyze the Disk Usage With Respect to File System Type

The “df” command offers dedicated options to deal with the file systems type. You can select or exclude a specific file system type to analyze the disk usage. Let’s see how to deal with file system type while working with the “df” command:

Example 2.1: Analyze All File Systems Associated With All the System Processes

The “df” command can be used to get the information of all the file systems, i.e., “proc” and “sysfs” to store the system processes and information:

sudo df -a

Here, you must have the root privileges or use sudo because few locations require the root privileges to fetch the information:

The output is much more detailed as compared to the general output of the “df” command.

Example 2.2: Get the File System Types of the File Systems

You can use the “-T” option with the “df” command to get the type of each filesystem. Here’s an example of such a scenario:

df -T

In the output, the “Type” column is added which shows the type of all the mounted filesystems.

Example 2.3: Get Storage Info About Specific File System

If you use the small “-t” and provide the FS (filesystem) type, you will get the information about that specific FS type. Here is the command to get only “ext4” File System types:

df -t ext4

The output is more concise containing only the selected FS.

Example 2.4: Exclude the Specific File System Type

The standard output of the “df” command prints all the filesystems which include the temporary (tmpfs) and the primary (ext4 in our case). If you want to exclude any file system type from the output, you can do it using the “-x” flag with the file system type.

For instance, if the output is messy, you can exclude the temporary filesystems (tmpfs), as we did here:

df -x tmpfs

These are the most used functionality of the “df” command.

Note: The operability of the options is the same. However, each option serves a different purpose. So, get into the options section to get more support options for the “df” command.

Tip: Alternatives of the df Command

The purpose of the “df” command is to tell you the free space and other parameters about the storage of the file systems. Linux well-known for its effective command line support, offers some alternatives to the “df” command. Let’s have a quick overview of these commands:

pydf Command

This command is an extension of the “df” command written in Python. It shows a colored output of the “df” command. The “pydf” command is not available by default. First, install it using the command as per your Linux distribution:

sudo apt install pydf #Debian and its Derivatives
sudo pacman -S pydf #Arch Based Distros
sudo zypper install pydf #SUSE Linux

Example of the pydf Command in Linux

Let’s run the command in the terminal:

pydf

The output is the same as the df command, just with an interactive interface.

For more detailed output/information, use the options supported by the “pydf” command:

pydf --help

duf Command

The “duf” command is another enhanced utility to provide a comprehensive overview of disk usage. It has the combined effect of “df” and “du” commands. The “duf” utility needs to be installed as it is not available by default. Here are the commands:

sudo apt install duf #Debian/Ubuntu Based Distributions
sudo pacman -S duf #Arch-Based Distributions
sudo zypper install duf #SUSE-Based Distributions

Example of the duf Command

Let’s exercise duf command:

duf

Here, you will get the categories-wise output, i.e., local devices refer to the physical devices attached, and the special devices include the virtual storage devices.

Likewise, each command discussed above, the “duf” command also offers various options that can be used to filter the output. Here they are:

duf --help

This is all about the “df” and its alternatives on Linux.

Bottom Line

Primarily, the “df” command is used to check the free disk space on your file systems. However, it also shows the total and available disk space alongside short info about the mounted file systems. So, the “df” is a potential utility, if you want to get the details about the storage devices and the associated file systems.

This post has briefly discussed the syntax, options, and examples of the “df” command. Moreover, you have also learned two best alternatives to the “df” command.

Categories