df Command in Linux 1

How to Use df Command in Linux

Disk management was and is still a very important part of owning and using a computer. Although the time of expensive and small-capacity storage is over, a robust disk management skill can still make your computing experience better through efficiency. One of the most reliable disk monitoring tools in a Linux environment is the df command.

What is the df Command?

df is a command that provides all kinds of data regarding file disk space usage. It works by implementing statfs and statvfs system calls (basically commands that retrieve statistics for mounted filesystems. Users need to have at least read access to use this command.

Disk space was a very precious resource during the early days of computing. Every byte was accounted for and was frequently monitored to plan document storage efficiently. The df command was released in 1971 when 80k floppy disks were used on mainframes.

Users then and now use df to monitor the remaining disk capacity to prevent their device from running out of usable space unexpectedly. Having to clean up your disk with unnecessary files while in the middle of doing something important can become a very time-consuming nuisance.

How to Use df?

You don’t need to add anything else to use the primary functionality. However, the available parameters are very useful in customizing your command.

$ df <option> <parameter>

In the example below, you can get all the used and available storage space along with the directory it is mounted on.

$ df
usage of df command in Linux

Casual users who just want to check their disk usage might not be proficient enough in crunching command-line results yet. The remedy to this is to use df arguments.

Relevant Options for df command in Linux

Users can display df output in a simpler way through several options. You can even exclude filesystems based on type, or display them all in a detailed list regardless. Here are the most commonly-used df parameters and their functions.

-a: List all disk usage including pseudo, duplicate, and remote file systems.

-B: Shows results with user-defined block size.

-h, -m, -k: Sets output results into human-readable units

-l: Only shows local system files in the output.

-t: Limits results to preferred file system type

-i: Shows inode information for the list (instead of block usage)

Example Usages of df Command in Linux

There are a handful of df usages that Linux users often master to make their disk management experience easier. df’s core functionality is to display disk space usage for a specific mount point. Its other usages are focused on organizing and displaying data. It can:

  • Show the result in KB, MB, and GB (or human-readable format)
  • Filter out and display disk space usage according to the filesystem type
  • Exclude partitions according to the filesystem type
  • Shows Inodes (or a data structure that stores metadata from files )  
  • Display usage based on the current directory

Studying df can help a lot when you’re a beginner trying to understand outputs that command spit out in the command line. Students can also learn how to use options and parameters effectively. It is a great exercise for command line beginners out there.

Without further ado, let’s hop in with some of the examples!

Checking Total Disk Usage from all Mount Points

As mentioned above, the df command alone can display all the necessary information on disk usages. Unfortunately, it doesn’t include pseudo filesystems, duplicates, and inaccessible ones.

To measure disk usages in these devices, the user must also add the -a option in their prompt.

$ df -a
df command showing all mounted disks

Checking Disk Space by Filesystem Type

For those who have multiple partitions who want to work with one filesystem type at a time, the -t and -x option is for you. -t filters out all the partitions by type based on the parameter that the user set up.

In the example below, -t is followed by the ext4 filesystem type, so the result only shows all ext4 partitions.

$ df -t ext4
df command type

Newer Linux distros default ext4 during installations. However, you would likely have an ext3 filesystem if your system is older.  Replace the parameters accordingly.

Alternatively, you can remove any filesystem types that you don’t want to appear in your df result by using the -x option. It will still work as a normal df, but it won’t skip filesystems as described in your prompt. Here’s an example below:

$df -x ext4
df exclude type

Checking Disk Space from Specific Directory / File

What if you only want to check the disk usage of the directory that you’re currently working at? Checking disk usage from the current directory can be useful for people who are working on multiple partitions or disks for one project.

Use df followed by the pathname of the directory that you want to check. Alternatively, you can use df .. which means to check the current directory. As follows:

$ df /pathname/file
df command directory

Displaying Inodes Instead of Block Usage

Inodes contain metadata and address information for each file in a computer. If a program is looking for a certain file, it will pull the corresponding inode needed to use it.

All metadata of a file (size, permission, access control, location, etc) is contained in a corresponding inode.

If you want to get the inodes for a specific disk, the df option for you is df -i.

$ df -i
df inodes

Displaying Results in Human-readable Format

You can also set up the default unit to make the result comprehensible and not just random numbers. So instead of measuring your storage space per 1K blocks, you can make it show conventional units like GB (-h) and MB (-m).

$df -h
df human-readable

Final Thoughts

Linux disk management software with UIs that can do the similar tasks mentioned above are already available to download. But there is still a good reason why the df command is still worth learning: it helps you understand more about the command line. Check out our other blogs to know more about other Linux commands.

If this guide helped you, please share it. ?

Leave a Reply
Related Posts