Linux df Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the df command in Linux to understand the purpose and syntax of the command, explore disk usage, and customize the output. The df command is a utility that displays information about the file system, including the total size, used space, and available space of each file system. You will also learn how to use various options to customize the output, such as displaying the file system information in a more human-readable format. Additionally, you will explore disk usage by creating sample files and using the df command to check the disk usage of the directory where the files are located.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicFileOperationsGroup(["`Basic File Operations`"]) linux(("`Linux`")) -.-> linux/SystemInformationandMonitoringGroup(["`System Information and Monitoring`"]) linux/BasicFileOperationsGroup -.-> linux/ls("`Content Listing`") linux/SystemInformationandMonitoringGroup -.-> linux/free("`Memory Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/df("`Disk Space Reporting`") linux/SystemInformationandMonitoringGroup -.-> linux/du("`File Space Estimating`") subgraph Lab Skills linux/ls -.-> lab-422632{{"`Linux df Command with Practical Examples`"}} linux/free -.-> lab-422632{{"`Linux df Command with Practical Examples`"}} linux/df -.-> lab-422632{{"`Linux df Command with Practical Examples`"}} linux/du -.-> lab-422632{{"`Linux df Command with Practical Examples`"}} end

Understand the Purpose and Syntax of the df Command

In this step, you will learn about the purpose and syntax of the df command in Linux. The df command, short for "disk free", is a utility that displays information about the file system, including the total size, used space, and available space of each file system.

To use the df command, open a terminal and type the following command:

df

This will display the file system information for all mounted file systems on your system. The output will look similar to this:

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

This output shows the file system, total size, used space, available space, and the mount point for each file system.

You can also use the df command with various options to customize the output. For example, to display the file system information in a more human-readable format, you can use the -h (human-readable) option:

df -h

This will display the file system information with the sizes shown in a more readable format, such as megabytes and gigabytes.

Explore Disk Usage with the df Command

In this step, you will learn how to use the df command to explore the disk usage on your system.

First, let's create a new directory and some sample files to test the df command:

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

Now, let's use the df command to check the disk usage of the /home directory, where the sample_files directory is located:

df -h /home

This will display the file system information for the /home directory, including the total size, used space, and available space.

Example output:

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

You can also use the df command to display the disk usage of a specific file or directory. For example, to check 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 be used to display the disk usage of all file systems on your system. To do this, simply run the df command without any arguments:

df -h

This will display the disk usage information for all mounted file systems on your system.

Customize the df Command Output

In this step, you will learn how to customize the output of the df command to display the information you need.

The df command has several options that allow you to customize the output. Here are a few examples:

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

This will display the file system information with the sizes shown in a more readable format, such as megabytes and gigabytes.

  1. Display the file system type:
df -T

This will display the file system type for each file system.

  1. Display the inodes information:
df -i

This will display the total number of inodes, used inodes, and available inodes for each file system.

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

This will display the file system information in a specific format, including the file system source, type, size, used space, available space, percentage used, and mount point.

You can also combine these options to customize the output even further. For example:

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

This will display the file system information in a human-readable format with the specific fields you want to see.

Summary

In this lab, you learned about the purpose and syntax of the df command in Linux, which displays information about the file system, including the total size, used space, and available space of each file system. You also explored how to use the df command to check the disk usage on your system, including how to customize the output using various options such as the -h (human-readable) option. Finally, you learned how to use the df command to explore the disk usage of specific directories on your system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like