FUNDAMENTALS A Complete Guide for Beginners
The df command in Linux displays available and used space in file systems. As you know, no machine has unlimited disk space and checking available space is necessary sometimes. The df command in Linux is mainly run by the system administrator but doesn’t require special permission. So, anyone can use the df command. In this article, you will learn everything you need of the df command with tons of examples.
A. Description
The “df” comes from “disk free” and does what it sounds like. Moreover, it can take arguments and comes with multiple options. So, you can customize your output according to your preference.
B. Syntax
The syntax of the df command in Linux is simple. The syntax is df, followed by one or multiple options and one and multiple files or directories.
df [OPTION]... [FILE]...
C. Options
There are many options available for the df command in Linux. You can check those yourself by going to the manual page. Type the following command-
man df
Useful Options
- -a, –all (displays all file systems including pseudo, duplicate, inaccessible)
- -h, –human-readable (shows sizes in powers of 1024)
- -H, –si (shows sizes in powers of 1000)
- –output (prints output in a customised See the manual page for details)
- –total (ignores insignificant entries to available space and shows a grand total)
- -t, –type (only prints a specific type of file system. See the manual page for details)
- -x, –exclude-type (prints file system excluding specific file system type. See the manual page for details)
- -T, –print-type (prints file system type)
- -i, –inodes (lists inodes information)
Practical Examples of the “df” Command in Linux
Now, let’s see some practical examples of the df command in Linux.
Example 1: Display the Disk Space Usage Using the “df” Command in Linux
It is the most common use of the df command. To view your disk space usage, follow the steps below:
-
At first open the Ubuntu Terminal.
-
Type the following command in the command prompt:
df
-
Now, press the ENTER button.
You will see multiple columns with a title at the first line like below.
- Filesystem (A logical collection of files or directories on a disk drive or partition)
- 1K-blocks ( Using 1KB as a unit)
- Used (Used space)
- Available (Available Space)
- Use% (Used space in percentage)
- Mounted on (The location of the Linux directory tree where a storage device is associated)
Similar Readings
- The “apt-get” Command in Linux [10 Practical Examples]
- The “firewall-cmd” Command in Linux [7 Practical Examples]
- The “finger” Command in Linux [6 Practical Examples]
- The “env ” Command in Linux [9 Practical Examples]
- The “enable” Command in Linux [6 Practical Examples]
Example 2: Displaying the Disk Space Usage in Human-Readable Form
To make the output more understandable for a human you can follow the steps below.
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df -h
- Now, press the ENTER button.
As you can see, our output is more readable now.
Example 3: Displaying Used and Available Space for a Directory
You can also give a directory as an argument to view the used and available space for the directory. Let’s say I have a directory named mydirectory and I want to view available space for the directory. I need to follow the steps below:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df mydirectory
- Now, press the ENTER button.
The output will be like this:
Similar Readings
- The “time” Command in Linux [4 Practical Examples]
- The “tty” Command in Linux [4 Practical Examples]
- The “uptime” Command in Linux [5 Practical Examples]
- The “vmstat” Command in Linux [6 Practical Examples]
- The “uname” Command in Linux [11 Practical Examples]
Example 4: Show Grand Total Available Space Using the “df” Command in Linux
You can also view the grand total available space of your machine. Follow the steps below to do the same:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df --total
- Now, press the ENTER button.
You will see at the end of your output there is a new line at the end starting with the word total.
Example 5: Display File System Types Using the “df” Command in Linux
To view file systems with their types, follow the steps below:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df -T
- Now, press the ENTER button.
As you can see, there is an extra column with the heading Type.
Similar Readings
- The “free” Command in Linux [8 Practical Examples]
- The “getent” Command in Linux [11 Practical Examples]
- The “groupadd” Command in Linux [7 Practical Examples]
- The “addgroup” Command in Linux [7 Practical Examples]
- The “groups” Command in Linux [6 Practical Examples]
Example 6: Display Specific File System Type Using the “df” Command in Linux
You can print file systems with a specific type using the df command in Linux. For example, if I only want to see ext4 type file system, I need to follow the steps below:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df -T -t ext4
OR,
df -Tt ext4
Note: Using option “-T” isn’t mandatory. I used it so that you can have a clear understanding. Also, putting option “-t” before “-T” would generate an error as the option “-t” needs an argument.
- Now, press the ENTER button.
As you can see, there is only ext4 type file system.
Example 7: Display File System Excluding Specific File System Type
You can also exclude a specific file system type. For demonstration purposes, I will exclude the tmpfs type file system. Now follow the steps below:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df -T -x tmpfs
OR,
df -Tx tmpfs
Note: Using option “-T” isn’t mandatory. I used it so that you can have a clear understanding. Also, putting option “-x” before “-T” would generate an error as the option “-t” needs an argument.
- Now, press the ENTER button.
There is no tmpfs type file system in the output.
Similar Readings
- The “groupmod” Command in Linux [5+ Practical Examples]
- The “id” Command in Linux [7+ Practical Examples]
- The “service” Command in Linux [6 Practical Examples]
- The “sestatus” Command in Linux [4 Practical Examples]
- The “shutdown” Command in Linux [7 Practical Examples]
Example 8: Customize Output of the “df” Command
You can customize the output of the df command in Linux. You need to use the double dash option (–), followed by an equal (=) sign and finally a comma-separated list of columns of our interest (‘source‘, ‘fstype‘, ‘itotal‘, ‘iused‘, ‘iavail‘, ‘ipcent‘, ‘size‘, ‘used‘, ‘avail‘, ‘pcent‘, ‘file‘ and ‘target‘ etc). As I want only want to print Filesystem, Avail and Use%, I will use ‘source’,’avail’ and ‘pcent’. Now follow the steps below to do the same:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df --output=source,avail,pcent
- Now, press the ENTER button.
Now you can see our customized output
Example 9: Displaying Inode Information Using the “df” Command in Linux
Every file and directory in a filesystem has an Inode number. This number keeps track of all files and directories within a Linux or Unix-based filesystem. You can print Inode information by following the steps below:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df -i
- Now, press the ENTER button.
Now you will get your output in Inode number.
Similar Readings
- The “dmesg” Command in Linux [7+ Practical Examples]
- The “install” Command in Linux [6+ Practical Examples]
- The “reboot” Command in Linux [3 Practical Examples]
- The “sync” Command in Linux [8 Practical Examples]
- The “chage” Command in Linux [7 Practical Examples]
Example 10: Show Disk Space Available for a Specific Mount
To print disk space info for a specific mount, you need to provide the name of that mount as an argument. Let’s say I want to find information of the mount “/”. I will follow the steps below:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df /
- Now, press the ENTER button.
See, I am only getting the information of mount “/”, nothing else.
Example 11: Display Only Grand Total in Human Readable Form
Now, here is another example of combining multiple options of the df command in Linux. I also piped the output of the df command into another command. Let’s say I am only interested in displaying the grand total in a human-readable format. First, I used options “–total” and “-h” and piped the output into the command grep. grep is used to search text based on a pattern. I provided the grep command pattern “total”. So it is only showing the line containing the word total. You can do it yourself by following the steps below:
- At first open the Ubuntu Terminal.
- Type the following command in the command prompt:
df --total -h | grep total
- Now, press the ENTER button.
Now, you will see the output like this:
Conclusion
Hopefully, you have tried all the examples above, and I am pretty sure you have a good grip on the df command in Linux. Eventually, you will be spontaneous and efficient in using the command.
Similar Readings