Home Beginner's Guide Mastering disk space with the ‘df’ command in Linux

Mastering disk space with the ‘df’ command in Linux

The 'df' command is a cornerstone for managing disk space in Linux. Learn how to use it effectively with our practical guide, featuring real-world examples for better comprehension.

by Divya Kiran Kumar
df command linux

Among the plethora of commands available, df stands out as a crucial command for managing and understanding disk space on your Linux system. In this comprehensive guide, we will delve into the nuances of the df command, exploring its syntax, various options, and real-world applications.

Whether you are a seasoned system administrator, a budding developer, or simply a curious Linux enthusiast, understanding how to effectively use the df command can greatly enhance your ability to monitor and manage your system’s resources. So, let’s embark on this journey to uncover the capabilities of the df command, armed with examples, FAQs, and personal insights that will demystify this powerful Linux tool.

What is the df command?

The df command in Linux stands for “disk free.” It’s a simple yet powerful command used to display the amount of available and used disk space on all mounted filesystems. Whether you’re a system admin, a developer, or just a Linux enthusiast, knowing how to use df is essential.

Basic syntax of df

The basic syntax of the df command is quite straightforward:

df [options] [file...]

Here, [options] are the flags you can use to modify the output, and [file...] refers to a specific file or directory you want to check the disk space for.

Understanding df output

When you run df, the output typically shows several columns:

  • Filesystem: The name of the filesystem.
  • 1K-blocks (or Size): Total size of the filesystem.
  • Used: Amount of space used.
  • Available: Amount of space available.
  • Use%: Percentage of the filesystem that is used.
  • Mounted on: Directory on which the filesystem is mounted.

Let’s run a basic df command and see what the output looks like:

$ df

You might see something like this:

Filesystem     1K-blocks    Used Available Use% Mounted on
udev             2048060       0   2048060   0% /dev
tmpfs             413500    1340    412160   1% /run
/dev/sda1      102535924 9053964  88226340  10% /
tmpfs            2067480   24684   2042796   2% /dev/shm
tmpfs               5120       4      5116   1% /run/lock
tmpfs            2067480       0   2067480   0% /sys/fs/cgroup
/dev/sda6      366284644 8761234 339151072   3% /home
tmpfs             413496      52    413444   1% /run/user/1000

Examples

Let’s walk through some practical examples of how df can be used:

1. Checking disk space in human-readable format

Personally, I find the default output a bit hard to digest due to its use of 1K-blocks. To make it more readable, use the -h option:

$ df -h

Filesystem Size Used Avail Use% Mounted on
udev 2.0G 0 2.0G 0% /dev
tmpfs 404M 1.4M 403M 1% /run
/dev/sda1 98G 8.6G 84G 10% /
tmpfs 2.0G 24M 1.9G 2% /dev/shm
/dev/sda6 350G 8.4G 323G 3% /home

This command will display the disk space in MB, GB, etc., which is much easier to understand.

2. Displaying the filesystem type

Sometimes, I need to know the type of filesystem I’m dealing with. The -T option comes in handy:

$ df -T

Filesystem Type 1K-blocks Used Available Use% Mounted on
udev devtmpfs 2048060 0 2048060 0% /dev
/dev/sda1 ext4 102535924 9053964 88226340 10% /
/dev/sda6 ext4 366284644 8761234 339151072 3% /home

3. Including and excluding certain filesystem types

If I want to include or exclude specific filesystem types, I use the -t and -x options, respectively. For example, to only show ext4 filesystems:

$ df -t ext4

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda1 102535924 9053964 88226340 10% /
/dev/sda6 366284644 8761234 339151072 3% /home

And to exclude tmpfs filesystems:

$ df -x tmpfs

Filesystem 1K-blocks Used Available Use% Mounted on
udev 2048060 0 2048060 0% /dev
/dev/sda1 102535924 9053964 88226340 10% /
/dev/sda6 366284644 8761234 339151072 3% /home

4. Checking disk space for a specific directory

At times, I only need to check the disk space related to a specific directory. This can be done by:

$ df /path/to/directory
$ df /home

Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda6 366284644 8761234 339151072 3% /home

5. Including all filesystems (-a)

This option includes dummy filesystems and those with 0-block sizes that are usually omitted from the standard df output.

$ df -a

Filesystem     1K-blocks    Used Available Use% Mounted on
sysfs                  0       0         0    - /sys
proc                   0       0         0    - /proc
udev             2048060       0   2048060   0% /dev
/dev/sda1      102535924 9053964  88226340  10% /
...

6. Showing inode information (-i)

Instead of showing block usage, this displays the inode information, which is useful when you’re dealing with a large number of small files.

$ df -i

Filesystem     Inodes   IUsed   IFree IUse% Mounted on
udev           511515     402  511113    1% /dev
/dev/sda1     6553600  480234 6073366    8% /
/dev/sda6    24419000  345678 24063322   2% /home

7. Displaying the disk usage in 1024-byte blocks (-k)

This is the default setting, but can be explicitly set using -k. It displays disk space in 1024-byte (1K) blocks.

$ df -k

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1      102535924 9053964  88226340  10% /
/dev/sda6      366284644 8761234 339151072   3% /home

8. Limiting the output to local filesystems (-l)

This is useful when you want to ignore remote filesystems and focus only on the local ones.

$ df -l

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1      102535924 9053964  88226340  10% /
/dev/sda6      366284644 8761234 339151072   3% /home

9. Producing a grand total (--total)

This option is particularly useful when you want a summary of the total disk space used and available across all filesystems. It appends a grand total at the end of the output.

$ df --total

Filesystem     1K-blocks    Used Available Use% Mounted on
udev             2048060       0   2048060   0% /dev
/dev/sda1      102535924 9053964  88226340  10% /
/dev/sda6      366284644 8761234 339151072   3% /home
...
total          470916628 17815632 442534372   4%

10. Ensuring data is up to date (--sync)

By using the --sync option, df performs a sync before getting the usage data. This ensures that the information provided is up to date, reflecting any recent changes to the filesystem.

$ df --sync

Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1      102535924 9053964  88226340  10% /
/dev/sda6      366284644 8761234 339151072   3% /home
...

The output looks similar to the standard df output, but with the assurance that all recent changes to the disk are accounted for.

Useful df command options

To further enhance your understanding and usage of the df command, here’s a table summarizing some of the most useful options. This table is a handy reference that I often turn to when using df in various scenarios.

Option Description
-h Displays the disk space in a human-readable format (e.g., KB, MB, GB).
-a Includes all filesystems, including those with 0 blocks, which are usually omitted.
-T Shows the type of each filesystem.
-t [type] Displays filesystems of a specific type (e.g., df -t ext4 for ext4 filesystems).
-x [type] Excludes filesystems of a specific type (e.g., df -x tmpfs to exclude tmpfs filesystems).
--total Produces a grand total at the end of the output, which is helpful in summarizing.
-i Shows inode information instead of block usage, useful for inode-based monitoring.
-k Displays the disk usage in 1024-byte blocks, the default setting.
-l Limits the output to local filesystems only, omitting remote ones.
--sync Ensures the data is up to date by invoking a sync before getting the usage data.
--help Displays help and exit.
--version Outputs version information and exit.

Frequently Asked Questions about the df Command

What does the df command do in Linux?

The df command in Linux is used to display the amount of available and used disk space on all mounted filesystems. It shows details like total space, space used, space available, and the mount point of each filesystem.

How do I see disk space in GB in Linux?

To view disk space in GB (gigabytes) or other human-readable formats like MB (megabytes), use the -h option with the df command:

$ df -h

This will display the disk space in a format that’s easier to read and understand.

Can df show the type of filesystem?

Yes, the -T option with the df command displays the type of each filesystem:

$ df -T

How do I exclude a specific filesystem type in the df output?

To exclude a specific filesystem type, use the -x option followed by the filesystem type. For example, to exclude tmpfs filesystems, use:

$ df -x tmpfs

Is there a way to see inode information instead of disk usage with df?

Yes, the -i option will display inode information instead of block usage:

$ df -i

This is particularly useful when you need to monitor inode usage instead of disk space.

How can I get a summary of total disk space with the df command?

You can get a summary of total disk space used and available by using the --total option:

$ df --total

This will append a grand total line at the end of the output.

Does df provide real-time information?

To ensure that df provides the most current information, you can use the --sync option, which will perform a sync before fetching the data:

$ df --sync

This ensures the information reflects all recent changes to the filesystem.

Can I check disk space for a specific directory with df?

Yes, simply append the directory path to the df command:

$ df /path/to/directory

This will show the disk space usage for the filesystem where the specified directory is located.

Conclusion

The df command in Linux is an incredibly versatile and essential tool for managing and understanding disk space usage. As we explored, it provides a wide range of functionalities, from displaying disk space in a human-readable format to showing detailed filesystem types and inode information. The real-world examples and FAQs should equip you with the knowledge to effectively utilize df in various scenarios, whether you’re a system administrator, a developer, or a Linux enthusiast. Remember, the power of Linux lies in its flexibility and the depth of its command-line tools, and df is a shining example of this, offering insights into your system’s storage with simple yet powerful commands.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.