df Command in Linux

Introduction

This lab provides a comprehensive guide to using the df command within a Linux environment. You will gain practical knowledge of the command's function and structure, learn how to analyze disk utilization, and tailor the output to your specific needs. The df command is an essential utility for system administrators and users alike, providing insights into file system capacity, used space, and available storage. Understanding its capabilities is crucial for effective system management. We will cover how to present information in a more easily understandable format and examine disk usage by creating test files and then leveraging the df command to assess the directory's disk footprint.

Understand the Purpose and Syntax of the df Command

This section focuses on the fundamental purpose and syntax of the df command in Linux. The df command, an abbreviation for "disk free," is a key tool in the systemadmin's arsenal. It delivers crucial data about file systems, including their overall capacity, the amount of space currently utilized, and the remaining free space.

To execute the df command, simply open your terminal and enter the following:

df

This command will generate a report detailing the file system information for all mounted file systems present on your system. The output will resemble the example shown below:

Filesystem     1K-blocks     Used Available Use% Mounted on
udev             2000144        0   2000144   0% /dev
tmpfs             403044     1072    401972   1% /run
/dev/sda1       50331648  7195380  40953268  15% /
tmpfs            2015220        0   2015220   0% /dev/shm
tmpfs               5120        0       5120   0% /run/lock
tmpfs            2015220        0   2015220   0% /sys/fs/cgroup
/dev/sda2       97656732 16703420  75958312  18% /home

The resulting table displays essential details for each file system: its name, total size, amount of space in use, available space, and the corresponding mount point.

The df command's versatility is further enhanced through the use of options that allow for customization of the output. To present the file system information in a more user-friendly, human-readable format, incorporate the -h option, as illustrated below:

df -h

Executing this modified command will result in file system sizes being displayed in a format more readily understood, such as megabytes (MB) and gigabytes (GB).

Explore Disk Usage with the df Command

In this section, you'll delve into utilizing the df command to gain insights into disk utilization on your system.

Begin by creating a new directory and generating some sample files. This will allow us to effectively test the df command:

mkdir ~/project/sample_files
cd ~/project/sample_files
touch file1.txt file2.txt file3.txt

Next, we will leverage the df command to assess disk usage within the /home directory, where the newly created sample_files directory resides:

df -h /home

This command will output the file system information associated with the /home directory, including overall capacity, utilized space, and available space.

Example output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       96G   16G   76G  18% /home

Furthermore, the df command can be employed to display the disk usage associated with a specific file or directory. To examine the disk usage of the sample_files directory:

df -h ~/project/sample_files

Example output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda2       96G   16G   76G  18% /home

The df command can also generate a comprehensive report detailing the disk usage of all file systems on your system. This is achieved by executing the df command without any command-line arguments:

df -h

This command will generate a disk usage report encompassing all mounted file systems present on your system.

Customize the df Command Output

This section focuses on tailoring the output of the df command to display the specific information you need for your systemadmin tasks.

The df command provides various options for customizing the output. Below are several examples:

  1. Present the file system information in a more human-readable format:
df -h

This option displays file system sizes in a format that is easier to understand, using units like megabytes (MB) and gigabytes (GB).

  1. Display the file system type:
df -T

This option reveals the file system type (e.g., ext4, xfs) for each file system.

  1. Display the inodes information:
df -i

This option presents the total number of inodes, the number of inodes currently in use, and the number of available inodes for each file system. Inodes are a critical concept in Linux file systems.

  1. Display the file system information in a specific format:
df --output=source,fstype,size,used,avail,pcent,target

This powerful option enables you to specify the precise fields to be included in the output, such as the file system source, type, size, used space, available space, percentage used, and mount point. This is invaluable for scripting and automating systemadmin tasks.

You can combine these options to achieve even greater customization of the output. For example:

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

This command combines human-readable formatting with the selection of specific fields, presenting the information in a clear and concise manner.

Summary

This lab has provided a thorough exploration of the df command in Linux. You have learned its purpose and syntax, which is to display information about file systems, including total size, used space, and available space. You also explored how to use the df command to check disk usage on your system and learned how to customize the output using options such as -h. Finally, you have learned how to leverage the df command to examine the disk usage of specific directories within your Linux environment, a critical skill for any systemadmin or user.

400+ Linux Commands