If you're new to the Linux environment, it's common to feel puzzled by certain commands, especially when they serve similar purposes. One such confusion arises between the df
and du
commands, both of which are used for disk space analysis. Don't worry, you're not alone! Understanding df and du commands and their distinct purposes is key to effectively managing your disk space in Linux. In this beginner-friendly guide, we will demystify the df
and du
commands, helping you gain clarity on their functionalities and how they contribute to analyzing disk space in Linux. By the end of this article, you'll feel confident in distinguishing between these commands and utilizing them to make informed decisions about your disk space management.
Table of Contents
Understanding df and du Commands in Linux
The df
and du
commands are both used for disk space analysis in Linux, but they serve different purposes. First, we will discuss about df
command.
1. df Command
The df
command stands for "disk free" and is used to display information about file system disk space usage. It provides an overview of available, used, and total disk space on mounted file systems. It helps you understand how much space is used and how much is available on each partition or file system.
Example:
$ df -h
Explanation:
The command displays disk space usage in a human-readable format (-h
), providing information about file systems, their total size, used space, available space, and utilization percentage.
1.1. df Command Examples
Here are some examples of using the df
command along with their explanations:
1. Basic Disk Space Usage:
$ df
Sample Output:
Filesystem 1K-blocks Used Available Use% Mounted on udev 16191444 0 16191444 0% /dev tmpfs 3248044 1936 3246108 1% /run /dev/nvme0n1p2 478096136 186495704 267241008 42% / tmpfs 16240220 25348 16214872 1% /dev/shm tmpfs 5120 4 5116 1% /run/lock /dev/nvme0n1p1 523248 3484 519764 1% /boot/efi /dev/sda1 976698752 389951104 586747648 40% /media/ostechnix/SK_WD_SSD /dev/fuse 131072 20 131052 1% /etc/pve tmpfs 3248044 1644 3246400 1% /run/user/1000
Explanation:
Running df
without any options displays disk space usage for all mounted file systems. It shows information such as file system, total size, used space, available space, and the mount point.
2. Human-Readable Output:
$ df -h
Sample Output:
Filesystem Size Used Avail Use% Mounted on udev 16G 0 16G 0% /dev tmpfs 3.1G 1.9M 3.1G 1% /run /dev/nvme0n1p2 456G 178G 255G 42% / tmpfs 16G 25M 16G 1% /dev/shm tmpfs 5.0M 4.0K 5.0M 1% /run/lock /dev/nvme0n1p1 511M 3.5M 508M 1% /boot/efi /dev/sda1 932G 372G 560G 40% /media/ostechnix/SK_WD_SSD /dev/fuse 128M 20K 128M 1% /etc/pve tmpfs 3.1G 1.7M 3.1G 1% /run/user/1000
Explanation:
Adding the -h
option makes the output human-readable, displaying sizes in a more understandable format (e.g., KB, MB, GB).
3. Specific File System:
$ df /dev/sda1
Sample Output:
Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda1 976698752 389951104 586747648 40% /media/ostechnix/SK_WD_SSD
Explanation:
Specifying a file system, such as /dev/sda1
, shows disk space usage specifically for that file system.
4. Multiple File Systems:
$ df /dev/sda1 /dev/sdb1
Explanation:
You can provide multiple file system paths as arguments to the df
command to see the disk space usage for each of them.
5. Inode Usage:
$ df -i
Sample Output:
Filesystem Inodes IUsed IFree IUse% Mounted on udev 4047861 617 4047244 1% /dev tmpfs 4060055 1122 4058933 1% /run /dev/nvme0n1p2 30433280 1402561 29030719 5% / tmpfs 4060055 61 4059994 1% /dev/shm tmpfs 4060055 14 4060041 1% /run/lock /dev/nvme0n1p1 0 0 0 - /boot/efi /dev/sda1 0 0 0 - /media/ostechnix/SK_WD_SSD /dev/fuse 262144 37 262107 1% /etc/pve tmpfs 812011 104 811907 1% /run/user/1000
Explanation:
Using the -i
option displays the inode usage, which shows information about the number of used and available inodes on each file system.
6. Display File System Type:
$ df -T
Sample Output:
Filesystem Type 1K-blocks Used Available Use% Mounted on udev devtmpfs 16191444 0 16191444 0% /dev tmpfs tmpfs 3248044 1936 3246108 1% /run /dev/nvme0n1p2 ext4 478096136 186495792 267240920 42% / tmpfs tmpfs 16240220 25348 16214872 1% /dev/shm tmpfs tmpfs 5120 4 5116 1% /run/lock /dev/nvme0n1p1 vfat 523248 3484 519764 1% /boot/efi /dev/sda1 exfat 976698752 389951104 586747648 40% /media/ostechnix/SK_WD_SSD /dev/fuse fuse 131072 20 131052 1% /etc/pve tmpfs tmpfs 3248044 1644 3246400 1% /run/user/1000
Explanation:
Adding the -T
option shows the file system type along with disk space usage information.
7. Exclude Pseudo File Systems:
$ df -x tmpfs -x devtmpfs
Sample Output:
Filesystem 1K-blocks Used Available Use% Mounted on /dev/nvme0n1p2 478096136 186495884 267240828 42% / /dev/nvme0n1p1 523248 3484 519764 1% /boot/efi /dev/sda1 976698752 389951104 586747648 40% /media/ostechnix/SK_WD_SSD /dev/fuse 131072 20 131052 1% /etc/pve
Explanation:
The -x
option followed by one or more file system types excludes them from the output. In this example, we exclude the tmpfs
and devtmpfs
file systems.
8. Display File System inodes and Blocks:
$ df -i -B M
Sample Output:
Filesystem Inodes IUsed IFree IUse% Mounted on udev 4047861 617 4047244 1% /dev tmpfs 4060055 1122 4058933 1% /run /dev/nvme0n1p2 30433280 1402573 29030707 5% / tmpfs 4060055 61 4059994 1% /dev/shm tmpfs 4060055 14 4060041 1% /run/lock /dev/nvme0n1p1 0 0 0 - /boot/efi /dev/sda1 0 0 0 - /media/ostechnix/SK_WD_SSD /dev/fuse 262144 37 262107 1% /etc/pve tmpfs 812011 104 811907 1% /run/user/1000
Explanation:
Combining the -i
option with the -B
option specifies the block size and displays both the inode and block usage.
9. Include File System Type:
$ df -t ext4
Sample Output:
Filesystem 1K-blocks Used Available Use% Mounted on /dev/nvme0n1p2 478096136 186495964 267240748 42% /
Explanation:
The -t
option followed by a file system type shows disk space usage only for that specific file system type. In this example, it displays usage for the ext4
file system.
10. Show Disk Space in 1K Blocks:
$ df -k
Sample Output:
Filesystem 1K-blocks Used Available Use% Mounted on udev 16191444 0 16191444 0% /dev tmpfs 3248044 1936 3246108 1% /run /dev/nvme0n1p2 478096136 186496000 267240712 42% / tmpfs 16240220 25348 16214872 1% /dev/shm tmpfs 5120 4 5116 1% /run/lock /dev/nvme0n1p1 523248 3484 519764 1% /boot/efi /dev/sda1 976698752 389951104 586747648 40% /media/ostechnix/SK_WD_SSD /dev/fuse 131072 20 131052 1% /etc/pve tmpfs 3248044 1644 3246400 1% /run/user/1000
Explanation:
Using the -k
option shows disk space usage in 1K blocks.
These examples illustrate various scenarios where the df
command can be used to obtain disk space usage information in Linux.
Now let us learn about du
command.
2. du Command
The du
command stands for "disk usage" and is used to estimate file and directory disk usage. It helps you determine the space consumed by individual directories or files, making it useful for identifying space-hogging directories or large files.
Example:
$ du -sh /path/to/directory
Explanation:
The command calculates the total disk usage (-s
) of the specified directory and its subdirectories in a human-readable format (-h
). It provides the sum of disk usage by displaying the total size (-h
).
2.1. du Command Examples
Here are some examples of using the du
command along with their explanations:
1. Basic Disk Usage for a Directory:
$ du dir1
Sample Output:
4400 dir1/dir2 50480 dir1
Explanation:
Running du
followed by the path to a directory displays the disk usage of that directory (In this case, dir1) and its subdirectories.
2. Human-Readable Output:
$ du -h dir1
Sample Output:
4.3M dir1/dir2 50M dir1
Explanation:
Adding the -h
option makes the output human-readable, displaying sizes in a more understandable format (e.g., KB, MB, GB).
3. Total Disk Usage of a Directory:
$ du -sh dir1
Sample Output:
50M dir1
Explanation:
The -s
option provides a summary of the total disk usage of the specified directory without displaying individual subdirectory sizes. The -h
option makes the output human-readable.
4. Disk Usage of All Files and Directories in Current Directory:
$ du -h *
Sample Output:
4.3M dir2 5.8M document.pdf 116K image.jpeg 4.0K New Empty File 40M video.webm
Explanation:
Running du
with *
displays the disk usage of all files and directories in the current directory.
5. Disk Usage Sorted by Size:
$ du -sh * | sort -hr
Sample Output:
40M video.webm 5.8M document.pdf 4.3M dir2 116K image.jpeg 4.0K New Empty File
Explanation:
By piping the output of du
to sort
command, you can sort the disk usage in descending order (-r
flag) to see the largest files or directories at the top. The -h
option makes the output human-readable.
6. Disk Usage of Individual Files:
$ du -h dir1/document.pdf
Sample Output:
5.8M dir1/document.pdf
Explanation:
You can use du
to get the disk usage of individual files by providing the path to the file as an argument.
7. Disk Usage Excluding Directories:
$ du -d 1 -h --exclude=dir2 dir1
Sample Output:
45M dir1
Explanation:
The --exclude
option allows you to exclude specific directories from the disk usage calculation. In this example, the disk usage is calculated for the dir1
directory (i.e. parent directory), excluding the specified directory i.e. dir2
. Here, the -d
option specifies the depth or level of the directory hierarchy to be displayed. In this case, -d 1
limits the output to only the current directory and its immediate subdirectories.
8. Disk Usage of Multiple Directories:
$ du -h dir1/ Downloads/
Explanation:
You can provide multiple directory paths as arguments to the du
command to get the disk usage of each directory separately.
9. Disk Usage of Current Directory and Subdirectories:
$ du -h .
Explanation:
Using .
as the argument for du
displays the disk usage of the current directory and all its subdirectories.
10. Disk Usage of the Largest Directories:
$ du -h --max-depth=1 dir1 | sort -hr
Sample Output:
50M dir1 4.3M dir1/dir2
Explanation:
By specifying the --max-depth
option, you can limit the depth of the disk usage calculation. It is equivalent to -d
option. In this example, it displays the disk usage of the top-level directories within the specified directory, sorted in descending order.
These examples demonstrate different ways to use the du
command to analyze disk usage in Linux.
Now that we have explored the df and du commands, including their definitions and examples. I hope you have obtained a fundamental understanding of these two disk space analysis tools. With this knowledge in place, we can now shift our focus towards examining the difference between df
and du
with an example.
Difference between df and du Commands
- Purpose:
df
shows the overall disk space usage for mounted file systems, providing an overview of available and used space on each partition. On the other hand,du
focuses on estimating the disk usage of individual directories or files, helping you identify space-consuming elements. - Scope:
df
covers the entire file system, displaying disk space utilization for each mounted file system, whiledu
allows you to drill down and analyze disk usage at the directory or file level. - Usage:
df
is commonly used to monitor disk space availability and identify potential storage constraints, whiledu
is helpful for identifying directories or files that contribute significantly to disk space usage.
Example Scenario:
Suppose you have a file system with multiple partitions: /dev/sda1
mounted on /
and /dev/sda2
mounted on /home
. Running df -h
will provide information about the disk space utilization for these mounted partitions. In contrast, running du -sh /home
will estimate the disk usage of the /home
directory and its subdirectories, helping you identify space-consuming directories within /home
.
In summary, df
provides an overview of disk space usage at the file system level, while du
helps estimate disk usage at the directory or file level. By using both commands appropriately, you can gain insights into disk space utilization and effectively manage your storage resources in Linux.
Frequently Asked Questions
Here's the FAQ about df and du commands.
A: The df command in Linux is used to display information about file system disk space usage. It provides details such as total, used, and available space on mounted file systems.
A: The du command in Linux is used to estimate disk usage for directories and files. It helps identify the space consumed by individual directories or files.
A: The df command provides an overview of file system disk space usage, giving information about total, used, and available space. On the other hand, the du command estimates disk usage for directories and files, allowing for a more detailed analysis at the directory and file level.
A: Yes, the df and du commands serve different purposes and can be used in conjunction to gain comprehensive insights into disk space utilization. While df provides a high-level view, du allows for a granular examination of disk usage at the directory and file level.
A: You can use the df command followed by the path to the specific file system to view its disk space usage. For example: df /dev/sda1
.
A: To check the disk usage of a specific directory, use the du command followed by the path to the directory. For example: du /path/to/directory
A: Yes, both the df and du commands provide options to exclude specific directories or files from the disk usage calculations. For example, with du, you can use the --exclude
option followed by the directory or file name to exclude it from the calculation.
A: Yes, both commands offer additional options that allow you to customize the output. For example, you can use the -h
option with both commands to display sizes in a human-readable format.
A: With the du command, you can use the -s
option to get a summary of disk usage for a directory without including subdirectories. For example: du -sh /path/to/directory
.
A: Yes, the df and du commands are standard utilities in Linux and are available on most Linux distributions. However, specific options or output formats may vary slightly between distributions.
Conclusion
In this guide, we unraveled the mystery of df and du commands and provided example commands to analyze and understand disk space usage. When it comes to managing disk space in Linux, df
and du
commands play a vital role. For beginners, grasping the distinction between these commands and their practical usage is crucial.
2 comments
Please give actual output of these commands. It becomes easier to try these commands on users terminal. Thanks
Added the outputs for each commands. Hope they are easier to understand.