du vs df commands in linux

Pranav Bakare - Oct 2 - - Dev Community

The du and df commands in Linux both deal with disk usage, but they serve different purposes and provide different kinds of information:

The du command reports the disk space used by files and directories. It calculates the actual space occupied by files within a specified directory or file path.

The df command reports the available disk space on the entire filesystem, including mounted drives. It shows how much space is free, used, and available on the disk partitions, not for specific files or directories.

  1. du (Disk Usage)

The du command reports the disk space used by files and directories. It calculates the actual space occupied by files within a specified directory or file path.

Key Points:

Shows the disk usage of files and directories.

Displays the size of each directory and subdirectory.

Provides an estimate of how much space a directory or file occupies.

Useful for understanding how space is being consumed within a particular directory or file structure.

Common Syntax:

du [options] [directory/file]

Example:

du -h /home/user

-h: Displays the sizes in human-readable format (KB, MB, GB).

This command shows how much space each file and directory under /home/user is using.

Sample Output:

4.0K /home/user/Documents/notes
12M /home/user/Documents/photos
64M /home/user

This output shows the disk usage for each subdirectory under /home/user.

  1. df (Disk Free)

The df command reports the available disk space on the entire filesystem, including mounted drives. It shows how much space is free, used, and available on the disk partitions, not for specific files or directories.

Key Points:

Shows overall disk usage per file system or partition.

Displays the amount of space used and available for mounted filesystems.

Useful for seeing the total disk space usage and available storage on the system.

Common Syntax:

df [options] [filesystem]

Example:

df -h

-h: Displays the sizes in human-readable format (KB, MB, GB).

This command shows the total disk space usage for all mounted filesystems.

Sample Output:

Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 30G 18G 63% /
tmpfs 2G 100M 1.9G 5% /tmp
/dev/sdb1 200G 150G 40G 80% /mnt/storage

This output shows the total space, used space, and available space for each mounted filesystem.

Key Differences:

Example Use Case:

du is useful when you need to know which files or directories are using the most space within a folder or across your file system.

df is useful when you want a high-level overview of disk space usage on your entire system, or to see which partitions are running out of space.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player