Cloud Service >> Knowledgebase >> General >> A Complete Guide: How to Use the df Command in Linux/Unix
submit query

Cut Hosting Costs! Submit Query Today!

A Complete Guide: How to Use the df Command in Linux/Unix

The df (Disk Filesystem) command in Linux/Unix is used to check the disk space usage of file systems. It provides details about total disk space, used space, available space, and usage percentage.

This guide covers everything you need to know about using the df command effectively, along with examples.

Syntax of df Command

The basic syntax of the df command is:

df [OPTION]... [FILE]...

If run without any options, df displays disk space usage in blocks for all mounted file systems.

Understanding the Default df Output

When you execute df without any options, you get an output similar to this:

Filesystem

1K-blocks

Used

Available

Use%

Mounted on

udev

4,049,376

0

4,049,376

0%

/dev

tmpfs

814,400

1,832

812,568

1%

/run

/dev/sda1

50,000,000

1,234,567

48,765,433

3%

/

tmpfs

4,071,984

0

4,071,984

0%

/dev/shm

tmpfs

512,000

0

512,000

0%

/run/user/1000

Explanation of Columns:

Column

Description

Filesystem

The name of the disk partition

1K-blocks

The total size of the file system in 1K blocks

Used

Space already used

Available

Free space available

Use%

Percentage of disk space used

Mounted on

Mount point (the location where the file system is attached)

Commonly Used df Options with Examples

1. Display Disk Space in Human-Readable Format

To display disk space usage in a more readable format (MB, GB, TB), use the -h option:

df -h

Example Output:

Filesystem      Size  Used  Avail  Use%   Mounted on

/dev/sda1        50G   1.2G   48G    3%           /

2. Display Disk Space in Specific Units

You can use -k, -m, or -h to specify the units:

  • -k for Kilobytes (default)

  • -m for Megabytes

  • -h for Human-readable format

Example (Display in MB):

df -m

3. Display File System Types

To check the type of each file system, use the -T option:

df -T

Example Output:

Filesystem     Type  1K-blocks    Used     Available    Use%  Mounted on

/dev/sda1       ext4   50000000   1234567  48765433   3%        /

4. Display Information for a Specific File System

To check the disk usage of a specific file system or directory, specify its path:

df -h /home

5. Display Inodes Usage Instead of Blocks

Use the -i option to show inode information instead of disk usage:

df -i

Example Output:

Filesystem     Inodes    Used     Free      Use% Mounted on

/dev/sda1      3276800  54321 3222479    2%         /

6. Exclude Specific File System Types

To exclude specific file system types (e.g., tmpfs), use -x:

df -h -x tmpfs

7. Display All File Systems (Including Unmounted Ones)

Use df -a to show all file systems, including those with 0 blocks:

df -a

8. Check Disk Usage in a Script-Friendly Format

For script automation, use --output to specify columns:

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

Example Output:

Filesystem     Type   Size  Used Avail Use% Mounted on

/dev/sda1     ext4    50G   1.2G   48G   3%         /

Practical Use Cases of df Command

1. Monitor Disk Space Regularly

To check disk usage every 5 seconds:

watch -n 5 df -h

2. Find Low Space on Disk

To quickly check partitions with low space:

df -h | awk '$5 > 80 {print "Warning: Disk Usage High on", $6}'

3. Send Alerts for Low Disk Space

You can use the following script to notify when disk usage exceeds 90%:

#!/bin/bash

THRESHOLD=90

 

df -h | awk '{if ($5+0 > THRESHOLD) print "Warning: High Disk Usage on", $6}'

Conclusion

The df command is an essential tool for managing disk space in Linux/Unix systems. By using different options, you can monitor disk usage efficiently and prevent storage-related issues. Whether you're a beginner or a system administrator, mastering df will help you maintain a healthy filesystem.

Cut Hosting Costs! Submit Query Today!

Grow With Us

Let’s talk about the future, and make it happen!