df Command in Linux



The df command in Linux displays disk space usage on file systems. It provides a quick summary of disk space usage and offers customizable output options, making it highly useful for scripting tasks.

The df command can be useful for identifying file system types, finding the mount points, and monitoring disk usage.

Table of Contents

Here is a comprehensive guide to the options available with the df command −

Syntax of df Command

The general syntax for using the df command in Linux is as follows −

df [options] [filesystem] 

Here, the [options] signifies the df command options that include displaying disk usage in different formats. While [filesystem] indicates the device’s name on which the file system is present. If no file system is mentioned, the df command displays information on all currently mounted file systems.

df Command Options

Commonly used options for df command options are listed below −

Option Description
-a (--all) It displays all the file systems that include inaccessible, duplicate, or pseudo file systems
-B (--block-size)=SIZE It scales sizes by the specified size before printing
-h (--human-readable) It prints the information in human-readable format
-H (--si) It prints the information in powers of 1000
-i (--inodes) It prints inode information rather than block usage
--output=FIELD It prints the output of specified fields only
-P (--portability) It prints the output in POSIX format
--total It prints the total of disk usage
-t (--type)=TYPE It prints the file system of the specified TYPE
-T (--print-type) It prints file system type
-x (--exclude-type)=TYPE It does not print the file system of the specified TYPE

The SIZE arguments that can be used with the -B flag or --block-size option are listed below −

SIZE (Power of 1024) SIZE (Power of 1000) Description
K KB Kilobyte; default
M MB Megabyte
G GB Gigabyte
T TB Terabyte
P PB Petabyte
E EB Exabyte
Z ZB Zettabyte
Y YB Yottabyte
R RB Ronnabyte
Q QB Quettabyte

To list one or more fields with the df command output, the FIELD arguments are used with the --output option.

FIELD Description
source Displays file system source
fstype Displays the file system type
itotal Displays total inodes on each file system
iused Displays used inodes
iavail Displays free inodes
ipcent Displays the currently used inodes
size Displays the size of the file system
used Displays space occupied by the file system
avail Displays free space on a file system
file It refers to a specific file
pcent Displays usage in percentage
target It refers to a specific mount point

Examples of df Command in Linux

This section demonstrates various examples of using the Linux df command −

  • Displaying Disk Usage
  • Displaying Disk Usage of All File Systems
  • Displaying Disk Usage in Specific Block Size
  • Displaying Inode Information
  • Displaying Total Available Space
  • Displaying Disk Space of a Mount Point
  • Displaying Disk Space with File System Type
  • Displaying Disk Space Excluding File System Type
  • Getting Customized Output

Displaying Disk Usage

To display the disk usage of all mounted filesystems, use the df command without any options or arguments −

Displaying Disk Usage df command 1

By default, the output is in 1KB blocks, which makes it a bit harder to read.

To get details in human-readable format, use the -h flag.

Displaying Disk Usage df command 2

The -h or --human-readable option automatically selects the appropriate unit for easier readability of the output. It's important to note that the output is in powers of 1024.

To display output in powers of 1000, use -H or --si.

Displaying Disk Usage df command 3

Note − The powers of 1000 and 1024 play a crucial role in computing. The SI officially uses powers of 1000 while the IEC uses powers of 1024 as standard.

Displaying Disk Usage of All File Systems

To get the disk usage of all the file systems, the -a flag is used. It displays information on all the file systems whether they are pseudo, duplicate, or inaccessible.

df -ah
Displaying Disk Usage of All File Systems

You may need sudo privileges to access any inaccessible file system.

Displaying Disk Usage in Specific Block Size

By default, the df command prints the output in 1 kilobyte blocks. Use the -B flag or --block-size option to modify it. For example, to set the block size in 1M blocks, use −

df -B 1M
Displaying Disk Usage in Specific Block Size 1

The above command gives output using powers of 1024. To get it using the powers of 1000, replace the M with MB.

df -B 1MB
Displaying Disk Usage in Specific Block Size 2

In powers of 1000, 1 KB = 1000 bytes and 1 MB = 1000 Kilobytes, and so on while in powers of 1024 1 KB = 1024 bytes and 1 MB = 1024 Kilobytes.

Displaying Inode Information

The index nodes or inodes are a data structure that stores metadata of files or directories. Each file system has a finite number of inodes which also depends upon the file system type.

To get information on how inodes are utilized by each file system, use the -i or --inodes option.

Displaying Inode Information df command

The output displays various fields such as Filesystem, Inodes, IUsed, IFree, IUse%, and Mounted On.

Displaying Total Available Space

To print the total of all the file systems mounted on the same disk, use the --total option.

Displaying Total Available Space df 1

To print only the total parameter, pipe the output to the grep command −

Displaying Total Available Space df 2

Displaying Disk Space of a Mount Point

To get the disk space usage of a specific mount point, simply mention the mount point after the df command.

For instance, to get the information on the root mount point, use the following option −

df -h /

To get information on /boot mount point −

df -h /boot
Displaying Disk Space of Mount Point df

Displaying Disk Space with File System Type

By default, the df command does not display the file system types. Using the -t flag or --type option with the df command inserts a Type field into the output, indicating the filesystem type for each mounted filesystem.

Displaying Disk Space with File System Type 1

To limit the output to a specific file system, mention the type such as ext4, xfs, vfat, exfat, or ntfs.

Displaying Disk Space with File System Type 2

Displaying Disk Space Excluding File System Type

To exclude a file system by type, use the -x flag or the --exclude-type option. For instance, to exclude, tmpfs type file system −

df -x tmpfs
Displaying Disk Space Excluding File System Type

Getting Customized Output

The df command output can be customized using the --output option. For example, to display only Source, Used and Available fields, use the following option −

df -h --output=source,used,avail
Getting Customized Output df Command

Note that there should be no space after commas.

Using the df Command in Bash

The df command can be used for various purposes in bash scripting. Such as monitoring the storage by logging it or identifying the total space for critical tasks.

The following bash script checks the disk usage. If it is greater than 80%, then it will warn to remove the unnecessary content.

#!/bin/bash

disk=$(df --output=pcent / | tail -n 1 | tr -d '%')

echo "Disk usage: $disk%"

if [ "$disk" -gt 80 ]; then
   echo "Remove unnecessary content, storage is filling up"
else
   echo "Storage is in good health"
fi

Here is a sample output

Using the df Command in Bash

Conclusion

The df command is a powerful command line utility in Unix and Unix-like operating systems. It is primarily used to check the disk usage on a file system. The df command has various options to display the output in different formats.

The df command can be a handy tool for system administrators to monitor disk space and incorporate it into scripting.

Advertisements