DF vs DU Command in Linux: Key Differences

You’ve probably heard of the df and du commands if you’re a Linux user. They are both used to check disk usage, but they work in slightly different ways. This article will explore the key differences between these two commands, including their syntax, output, and usage.

What is the df Command?

The df command stands for “disk free” and displays information about the file system’s disk space usage. The command displays the total amount of disk space available, the amount of disk space used, and the amount of disk space available for the root (the system administrator). The df command also displays the file system type, the number of blocks used and available, and the file system mount point.

How to Use the df Command

To use the df command, open your terminal and type:

df

This will display the disk usage information for all mounted file systems. You can also specify a specific file system to check its disk usage. For example, to check the disk usage of the root file system, you can use the following command:

df /

df Command Options

The df command has several options that can be used to customize the output. Some of the most commonly used options include:

  • -h: displays the output in a human-readable format
  • -T: displays the file system type
  • -i: displays the inode information

Examples of Using the df Command

Here are some examples of using the df command with different options:

df -hT /

This will display the disk usage information for the root file system in a human-readable format and include the file system type.

df -i /

This will display the inode information for the root file system.

df -h --total

This will display the disk usage information for all mounted file systems in a human-readable format and a total for all file systems.

df -i -t ext4

This will display the inode information for all ext4 file systems.

What is the du Command?

The du command stands for “disk usage” and displays the disk space a file or directory uses. The command displays the total amount of disk space used by the specified file or directory and its subdirectories.

How to Use the du Command

To use the du command, open your terminal and type:

du filename

This will display the total amount of disk space the specified file or directory uses. You can also specify the -h option to display the output in a human-readable format.

du Command Options

The du command has several options that can be used to customize the output. Some of the most commonly used options include:

  • -h: displays the output in a human-readable format
  • -s: displays only the total disk space used

Examples of Using the du Command

Here are some examples of using the du command with different options:

du -h filename

This will display the total amount of disk space used by the specified file or directory in a human-readable format.

du -sh directory/

This will display the total amount of disk space used by the specified directory and its subdirectories in a human-readable format.

du -h --max-depth=1 directory/

This will display the total amount of disk space used by the specified directory and its immediate subdirectories in a human-readable format.

du -h --exclude="*.mp3" directory/

This will display the total amount of disk space used by the specified directory and its subdirectories in a human-readable format, excluding any files with the .mp3 extension.

du -h --time directory/

This will display the total amount of disk space used by the specified directory and its subdirectories in a human-readable format, along with the last modification time of each file or directory.

Differences Between df and du Commands

While both df and du commands are used to check disk usage, they have several key differences.

Output Format

The output format of the df command is a table that displays the file system type, the number of blocks used and available, and the file system’s mount point. In contrast, the output format of the du command is a simple list that displays the total amount of disk space used by a file or directory and its subdirectories.

Displayed Information

The df command displays information about the entire file system, including information about the root file system and any other mounted file systems. The du command, on the other hand, displays information only about the specified file or directory and its subdirectories.

Performance and Resource Usage

The df command is generally faster and uses fewer system resources than the du command because it only needs to read the file system’s superblock to get the disk usage information. In contrast, the du command needs to scan all the directories and files in the specified file or directory and its subdirectories, which can take a significant amount of time and use many system resources.

Conclusion

In summary, the df and du commands are useful tools for checking disk usage in Linux, but they work slightly differently. The df command displays information about the entire file system, while the du command displays information only about the specified file or directory and its subdirectories. Additionally, the df command is generally faster and uses fewer system resources than the du command.

Share to...