Linux df Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the df command in Linux. The df (disk free) command is an essential utility for displaying information about total, used, and available space on your system's filesystems. You will start with the basic command, learn how to make the output more readable, check usage for specific paths, and finally, customize the output to show only the information you need. By the end of this lab, you will be comfortable using df to monitor disk space on a Linux system.

Linux Commands Cheat Sheet

Basic Disk Usage Reporting

First, you will learn the basic usage of the df command and how to format its output for better readability. All commands in this lab will be executed in the terminal.

Open your terminal and run the df command without any options:

df

You will see a report of all mounted filesystems. The output will look similar to this:

Filesystem     1K-blocks     Used Available Use% Mounted on
overlay         20971520   128764  20842756   1% /
tmpfs              65536        0     65536   0% /dev
tmpfs            8052892        0   8052892   0% /sys/fs/cgroup
shm                65536        0     65536   0% /dev/shm
/dev/nvme1n1   104806400 20643324  84163076  20% /etc/hosts
tmpfs            8052892        0   8052892   0% /proc/acpi
tmpfs            8052892        0   8052892   0% /proc/scsi
tmpfs            8052892        0   8052892   0% /sys/firmware

By default, the disk space is shown in 1-kilobyte blocks, which can be difficult to interpret. To make the output more user-friendly, use the -h (human-readable) option. This option prints sizes in powers of 1024 (e.g., M for megabytes, G for gigabytes).

Run the df command with the -h option:

df -h

The output is now much easier to understand:

Filesystem      Size  Used Avail Use% Mounted on
overlay          20G  126M   20G   1% /
tmpfs            64M     0   64M   0% /dev
tmpfs           7.7G     0  7.7G   0% /sys/fs/cgroup
shm              64M     0   64M   0% /dev/shm
/dev/nvme1n1    100G   20G   81G  20% /etc/hosts
tmpfs           7.7G     0  7.7G   0% /proc/acpi
tmpfs           7.7G     0  7.7G   0% /proc/scsi
tmpfs           7.7G     0  7.7G   0% /sys/firmware

Here is a breakdown of the columns:

  • Filesystem: The name of the storage device.
  • Size: The total size of the filesystem.
  • Used: The amount of space currently used.
  • Avail: The amount of space available for use.
  • Use%: The percentage of the disk space that is used.
  • Mounted on: The directory where the filesystem is mounted.

Checking a Specific Path

The df command can also report on the filesystem that contains a specific file or directory. This is useful when you want to know how much space is left on the partition where a particular directory resides. It's important to remember that df reports on the entire filesystem, not the size of the directory itself.

First, check the disk usage for the root directory, represented by /:

df -h /

The output will show the details for the filesystem mounted at the root of your system:

Filesystem      Size  Used Avail Use% Mounted on
overlay          20G  126M   20G   1% /

Next, check the disk usage for your current working directory. The default path for your terminal is ~/project, which is located inside your home directory. You can use a dot . to represent the current directory.

df -h .

The output will show the disk usage for the filesystem where your ~/project directory is located. In this environment, it is the same as the root filesystem:

Filesystem      Size  Used Avail Use% Mounted on
overlay          20G  126M   20G   1% /

This confirms that your project directory is on the main filesystem and shows you the total available space on that partition.

Displaying Filesystem Type and Inode Information

Beyond disk space, df can provide other useful details about your filesystems, such as their type and inode usage.

To display the filesystem type, use the -T (print-type) option. This adds a Type column to the output, showing formats like overlay, tmpfs, or xfs.

Run the following command to see the filesystem types in a human-readable format:

df -hT

The output will now include the Type column:

Filesystem     Type     Size  Used Avail Use% Mounted on
overlay        overlay   20G  126M   20G   1% /
tmpfs          tmpfs     64M     0   64M   0% /dev
tmpfs          tmpfs    7.7G     0  7.7G   0% /sys/fs/cgroup
shm            tmpfs     64M     0   64M   0% /dev/shm
/dev/nvme1n1   xfs      100G   20G   81G  20% /etc/hosts
tmpfs          tmpfs    7.7G     0  7.7G   0% /proc/acpi
tmpfs          tmpfs    7.7G     0  7.7G   0% /proc/scsi
tmpfs          tmpfs    7.7G     0  7.7G   0% /sys/firmware

You can also check inode usage. An inode is a data structure on a filesystem that stores all the information about a file or directory except its name and actual data. A filesystem can run out of inodes even if there is still disk space available. To check inode usage, use the -i option.

df -i

This command displays the total number of inodes and how many are used and free:

Filesystem       Inodes  IUsed    IFree IUse% Mounted on
overlay        52428800 657612 51771188    2% /
tmpfs           2013223     16  2013207    1% /dev
tmpfs           2013223     17  2013206    1% /sys/fs/cgroup
shm             2013223      1  2013222    1% /dev/shm
/dev/nvme1n1   52428800 657612 51771188    2% /etc/hosts
tmpfs           2013223      1  2013222    1% /proc/acpi
tmpfs           2013223      1  2013222    1% /proc/scsi
tmpfs           2013223      1  2013222    1% /sys/firmware

Customizing the Output

For scripting or specific reporting needs, you might want to display only certain columns. The df command provides the --output option to customize the fields shown in the report.

You can specify a list of fields you want to see. Common fields include source (the filesystem), fstype (filesystem type), size, used, avail, pcent (use percentage), and target (mount point).

Let's create a custom report that shows the filesystem source, size, used space, available space, and mount point, all in a human-readable format.

df -h --output=source,size,used,avail,target

This command produces a clean, customized table with only the columns you requested:

Filesystem      Size  Used Avail Mounted on
overlay          20G  126M   20G /
tmpfs            64M     0   64M /dev
tmpfs           7.7G     0  7.7G /sys/fs/cgroup
shm              64M     0   64M /dev/shm
/dev/nvme1n1    100G   20G   81G /etc/hosts
tmpfs           7.7G     0  7.7G /proc/acpi
tmpfs           7.7G     0  7.7G /proc/scsi
tmpfs           7.7G     0  7.7G /sys/firmware

This feature is powerful for generating concise reports or for feeding data into other scripts, as it allows you to control the output format precisely.

Summary

In this lab, you have learned how to use the df command to monitor filesystem disk space in Linux. You started with the basic command and then used the -h option to get human-readable output. You also practiced checking the disk space for specific paths and learned the important distinction that df reports on the entire filesystem. Finally, you explored advanced options like -T to view filesystem types, -i to check inode usage, and --output to create custom reports. These skills are fundamental for any Linux user or system administrator.

Linux Commands Cheat Sheet