Finding the Largest Files on Your System with the du Command

Maret 12, 2025

When your Linux system is running out of space, identifying the largest files and directories can help you quickly free up space. In this article, we'll explore a powerful command combination that makes this task straightforward.

The Command

sudo du -ah / | sort -rh | head -20

Understanding the Components

The du Command

The du (disk usage) command estimates file space usage. Let's break down the options used:

  • -a: Shows all files, not just directories
  • -h: Human-readable output (e.g., 1K, 234M, 2G)
  • /: Starts from the root directory

The sort Command

The output is piped to sort with these options:

  • -r: Reverse order (largest first)
  • -h: Human-readable numbers (compares 2K and 1M correctly)

The head Command

head -20 restricts output to the first 20 lines, giving you the 20 largest items.

Practical Considerations

Why use sudo?

Running with sudo ensures you have permission to read all files on the system. Without it, you'll get "Permission denied" errors for protected directories.

Performance Impact

This command can be resource-intensive when run on the entire filesystem. It will:

  • Generate significant disk I/O
  • Take a long time on large systems
  • Potentially impact system performance temporarily

Better Alternatives for Large Systems

For production systems, consider these refinements:

sudo du -h --max-depth=2 / | sort -rh | head -20

This limits the depth of recursion, making the command run faster while still identifying large directories.

Safety Precautions

  • Run during low-traffic periods if on a production server
  • Consider using nice to reduce CPU priority: nice -n 19 sudo du -ah / | sort -rh | head -20
  • Avoid deleting files you don't understand - especially in system directories

Practical Examples

Sample output might look like this:

16G    /
14G    /home
10G    /home/username
8.5G   /var
7.2G   /var/lib
5.4G   /var/lib/docker
3.2G   /usr
2.8G   /usr/lib
2.1G   /opt
...

This quickly reveals where your disk space is being consumed, allowing you to take targeted action to reclaim space.

#Linux #CommandLine #DiskUsage #SysAdmin #Tutorial