...

How to use df command in Linux(Unix): An easy and simple Guide in 2 Minutes

Rate this post

Introduction

The df command in Linux is essential for checking disk space usage across mounted filesystems, helping users and administrators manage storage efficiently. Here’s a comprehensive guide covering the usage, options, and practical examples of the df command in Linux.

Prerequisites

1. What is the df Command?

The df command displays information about the disk space usage of file systems. By default, it shows information such as the filesystem name, total size, used space, available space, and the mount point. It’s particularly useful for quickly checking if you’re running low on storage.

2. Basic Syntax of df command in Linux

The syntax for the df command is simple:

df [options] [file]
  • options: Various options to customize the output (e.g., showing in human-readable format or displaying inode usage).
  • file: Optional; provides information on the specific filesystem where the file is located.
df command in linux
df command in Linux

3. Common Options with df command

Below are commonly used options with df that enhance its usability:

  • -h (Human-readable format): Converts sizes to more readable formats (KB, MB, GB).df -h
  • -a (All filesystems): Shows all filesystems, including those with 0 size.df -a
  • -T (File system type): Displays the filesystem type for each entry.df -T
  • -i (Inodes usage): Shows inode information instead of block usage, useful for managing file-intensive directories.df -i
  • -x (Exclude specific file systems): Excludes certain file systems from the output, often used to filter out temporary filesystems like tmpfs.df -x tmpfs

Please go through the below table to understand the different options available with the df command

OptionDescriptionExample Usage
-a, --allIncludes dummy file systems in the output (e.g., those with zero blocks).df -a
-h, --human-readableDisplays sizes in a human-readable format (e.g., 1K, 234M, 2G).df -h
-HLike -h, but uses powers of 1000 instead of 1024 (e.g., 1K = 1000 bytes).df -H
-kDisplays sizes in kilobytes (default behavior in many systems).df -k
-mDisplays sizes in megabytes.df -m
-T, --print-typeShows the file system type.df -T
-l, --localLimits the listing to local file systems only.df -l
-t, --type=[TYPE]Displays only file systems of the specified type.df -t ext4
-x, --exclude-type=[TYPE]Excludes file systems of the specified type from the output.df -x tmpfs
-i, --inodesDisplays inode information instead of block usage.df -i
--totalProduces a grand total at the end of the output.df --total
--syncInvokes sync before getting usage info (ensures data is up-to-date).df --sync
--no-syncDoes not invoke sync before getting usage info (default behavior).df --no-sync
--output=[FIELD_LIST]Uses the output format defined by the field list.df --output=source,size,used,avail
-P, --portabilityUses the POSIX output format (ensures compatibility).df -P
-B [SIZE]Scales sizes by SIZE before printing them (e.g., -B MB for megabytes).df -B MB
--helpDisplays help information about the df command.df --help
--versionShows version information of the df command.df --version
df command options

4. Understanding the df Command Output

When you run df (typically with -h for readability), you’ll see columns with information about each filesystem:

  • Filesystem: The name of the filesystem.
  • Size: The total storage capacity of the filesystem.
  • Used: The amount of storage currently used.
  • Avail: Available free storage on the filesystem.
  • Use%: Percentage of the filesystem’s used space.
  • Mounted on: The directory where the filesystem is mounted.

Here’s a sample output:

everythingcoding@ec:~$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/root        9065864 5712388   3337092  64% /
tmpfs             490752       0    490752   0% /dev/shm
tmpfs             196304     984    195320   1% /run
tmpfs               5120       0      5120   0% /run/lock
efivarfs              56      24        27  48% /sys/firmware/efi/efivars
/dev/sda16        901520  112480    725912  14% /boot
/dev/sda15        106832    6246    100586   6% /boot/efi
tmpfs              98148      12     98136   1% /run/user/1001

5. Practical Examples

Here are some real-world uses of the df command:

Example 1: Checking Disk Space in Human-Readable Format

df -h

This command is commonly used to view disk usage in a more intuitive format (MB, GB), making it easy to interpret.

Example 2: Displaying Filesystem Type

To view disk usage along with the filesystem type (e.g., ext4, tmpfs):

df -T

Example 3: Exclude Temporary Filesystems

Exclude temporary filesystems, such as tmpfs or devtmpfs, to focus on actual disk storage:

df -h -x tmpfs

Example 4: Viewing Disk Usage for a Specific Directory

Specify a directory to view the filesystem where it resides:

df -h /home

Example 5: Display Inode Information

Inode usage is important for filesystems with many small files, as they can run out of inodes before running out of disk space:

df -i
find inode usage with the df command

6. Advanced Usage with Scripting

For administrators, automating disk usage checks can be useful for system monitoring. Here’s an example script that triggers an alert if a filesystem exceeds 80% usage:

#!/bin/bash
THRESHOLD=80
df -h | awk '{ if($5+0 > THRESHOLD) print "Warning: Disk space on "$6" at "$5 }'

7. Troubleshooting Common Issues with df command

Problem: Disk Full but Not Showing in df

Sometimes, a disk appears full even if df shows available space. This could be due to:

  • Open files: Files deleted but still held by processes.
  • Hidden files: Temporary files in hidden directories.
  • Inodes exhaustion: Running out of inodes despite free space.

Solution:

Use tools like lsof +L1 to identify open files or df -i to check inode usage.

8. Combining df with Other Commands

Combine df with commands like grep or sort to streamline output:

  • Filter specific filesystems: df -h | grep '/dev/sd'
  • Sort filesystems by usage: df -h | sort -nk5

9. Differences Between df and du Commands

While df provides a summary of filesystem usage, the du command reports directory- or file-specific disk usage. du is useful for understanding which directories or files consume space within a filesystem, whereas df gives an overview of total filesystem capacity.

10. Summary and Best Practices

The df command is an essential tool for monitoring disk space on Linux. With options for human-readable output, inode tracking, and filesystem filtering, administrators can stay on top of disk usage and proactively avoid storage issues.

Best Practices:

  • Regularly monitor disk usage with df -h to prevent unexpected space shortages.
  • Use -i to monitor inode usage, especially if you manage directories with large numbers of files.
  • Automate disk monitoring with scripts to catch usage spikes early.

Conclusion

The df command in Linux is simple yet powerful, providing crucial insights into disk space management. By mastering its options and applications, Linux users and administrators can maintain an efficient, well-monitored storage environment.

Spread the love

Leave a Comment

Seraphinite AcceleratorOptimized by Seraphinite Accelerator
Turns on site high speed to be attractive for people and search engines.