Let’s explore the differences between the df and du commands in Linux, as they both play crucial roles in managing disk space. These commands may seem similar, but they serve distinct purposes that are important to understand. Let’s break it down:
df command
df Command (Disk Free): The “df” command, short for “disk free”, provides details on the disk space usage of a file system. It provided File System, type, total size, usage, available and mount points.
Below is an example. Here -h is used for human-readable format. Notice, the counters are in K (KB), M (MB) or G (GB) etc.
Note, df command provides these details irrespective of location fromt it’s being executed. In below example, df command is run from user home location.
rakesh@jumpserver:~$ pwd
/home/rakesh
rakesh@jumpserver:~$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 1.2G 1.9M 1.2G 1% /run
/dev/sda3 98G 78G 15G 85% /
tmpfs 5.9G 0 5.9G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 5.9G 0 5.9G 0% /run/qemu
/dev/sda2 512M 5.3M 507M 2% /boot/efi
tmpfs 1.2G 144K 1.2G 1% /run/user/1000
rakesh@jumpserver:~$
Use “-T” option for filesystem type.
rakesh@jumpserver:~$ df -hT
Filesystem Type Size Used Avail Use% Mounted on
tmpfs tmpfs 1.2G 1.9M 1.2G 1% /run
/dev/sda3 ext4 98G 78G 15G 85% /
tmpfs tmpfs 5.9G 0 5.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 5.9G 0 5.9G 0% /run/qemu
/dev/sda2 vfat 512M 5.3M 507M 2% /boot/efi
tmpfs tmpfs 1.2G 144K 1.2G 1% /run/user/1000
rakesh@jumpserver:~$
You can also check specifically for a particular filesystem.
rakesh@jumpserver:~$ df -h /dev/sda2
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 512M 5.3M 507M 2% /boot/efi
rakesh@jumpserver:~$
To check Inode information, use -i option.
rakesh@jumpserver:~$ df -i /
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda3 6520832 363467 6157365 6% /
rakesh@jumpserver:~$
Overall, df command give you high level view of disk.
du command
du command – short for “disk usage” – a valuable tool for managing individual files and directories. It displays the amount of disk space used by a designated file or directory, along with its subdirectories.
Note, du command unlike df command give results based on current working directory, unless specific path is provided.
Here are various options.
- -h : human-readable format
- -s : summarize
- -a : Also includes files, not just directories
- –max-depth=N : print the total for a directory. max-depth=0 is equivalent to the to -s (summarize)
- –exclude : exclude files that match PATTERN
Now let’s look for output using these options.
rakesh@jumpserver:/home$ pwd
/home
rakesh@jumpserver:/home$ sudo du -sh < sudo is required if user not having permissions for all files/directories.
25G . << This is total size of directory.
rakesh@jumpserver:/home$
rakesh@jumpserver:/home$ sudo du -h --max-depth=0 << equivalent to -s
25G .
rakesh@jumpserver:/home$
To check usage of the sub-folders. you can use -a option if there are some files in current directory and you want size of those also listed.
rakesh@jumpserver:/home$ sudo du -h --max-depth=1
5.4G ./admin
12M ./admin1
20G ./rakesh
25G . <<<<< This is total
rakesh@jumpserver:/home$
You can pipe the output to “sort -h” command to have output listed in order of ascending sizes.
du -h --max-depth=1 | sort -h
Use “-a” option to have all files listed as well.
du -ah --max-depth=1 | sort -h
To learn more about du and df commands, checkout Linux man page
du man page – https://linux.die.net/man/1/du
df man page – https://linux.die.net/man/1/df
Crisp and very informative. Kudos to author.
Looking forward to more enlightening insights in the future!
Thank you Pradeep !
Rakesh Ji Thanks it’s very helpful
Thank you Yogesh Ji !!!