How to use the command "df" (with examples)
The “df” command provides an overview of the filesystem disk space usage on a Linux system. It is a useful command for monitoring disk usage and identifying any potential storage issues. This article will illustrate several use cases of the “df” command and provide explanations for each argument used.
Use case 1: Display all filesystems and their disk usage
Code:
df
Motivation: Running the “df” command without any arguments will display information about all mounted filesystems and their respective disk space usage. This is useful for getting a general overview of the disk usage on the system and identifying if any filesystems are running out of space.
Explanation: In this use case, no arguments are provided to the “df” command. It will automatically display the filesystem, total size, used space, available space, percentage of usage, and mount point for each mounted filesystem.
Example output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 1024000 316428 707572 31% /
tmpfs 409600 4 409596 1% /dev/shm
/dev/sdb1 2048000 1648032 399968 81% /mnt/data
Use case 2: Display all filesystems and their disk usage in human-readable form
Code:
df -h
Motivation: The “-h” option is used to display the disk usage in a human-readable form, making it easier to interpret the sizes. Instead of displaying the usage in kilobytes (KB), it will display in megabytes (MB) or gigabytes (GB) depending on the size.
Explanation: In this use case, the “-h” option is added to the “df” command. This option modifies the output to display the disk usage in a human-readable format. For example, sizes will be displayed as “100M” for 100 megabytes or “2G” for 2 gigabytes.
Example output:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 1000M 309M 691M 31% /
tmpfs 400M 4K 400M 1% /dev/shm
/dev/sdb1 2000M 1610M 390M 81% /mnt/data
Use case 3: Display the filesystem and its disk usage containing the given file or directory
Code:
df path/to/file_or_directory
Motivation: This use case is useful when you want to determine the filesystem where a particular file or directory resides. By specifying the path to the file or directory, the “df” command will display the filesystem and its associated disk usage.
Explanation: In this use case, the “df” command is used with the path argument to a specific file or directory. The command will display the filesystem on which the file or directory is located and provide the disk usage information for that particular filesystem.
Example output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 1024000 316428 707572 31% /
Use case 4: Display statistics on the number of free inodes
Code:
df -i
Motivation: Inodes are data structures used to store metadata for files and directories on a filesystem. The “-i” option in the “df” command allows you to display statistics on the number of free inodes available on each filesystem. This is useful for monitoring the number of available inodes and determining if a filesystem is running out.
Explanation: In this use case, the “-i” option is used with the “df” command. This option modifies the output to display statistics on the number of free inodes available on each filesystem. It will show the total number of inodes, the number of used inodes, the number of free inodes, and the percentage of inodes used.
Example output:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 65536 16384 49152 25% /
tmpfs 102400 4 102396 1% /dev/shm
/dev/sdb1 131072 52480 78592 40% /mnt/data
Use case 5: Display filesystems but exclude the specified types
Code:
df -x squashfs -x tmpfs
Motivation: Sometimes you may want to exclude certain types of filesystems from the output of the “df” command. This use case demonstrates how to exclude “squashfs” and “tmpfs” filesystems from the output.
Explanation: In this use case, the “-x” option is used twice with the “df” command, followed by the filesystem types to exclude. “squashfs” and “tmpfs” filesystems will be excluded from the output. This can be useful if you want to focus only on specific types of filesystems or exclude certain types from the output.
Example output:
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 1024000 316428 707572 31% /
/dev/sdb1 2048000 1648032 399968 81% /mnt/data
Conclusion:
The “df” command is a versatile tool for displaying filesystem disk space usage on a Linux system. It provides valuable information about mounted filesystems, their disk usage, and the number of free inodes. By utilizing the different options and arguments available with the “df” command, you can effectively monitor disk usage and identify any potential storage issues.