How to Find the Largest Disk Space Directories and Files in Linux

In Linux, managing disk space efficiently is essential for optimizing system performance. Identifying the largest files and directories can help free up space and improve storage management. This guide provides various methods using built-in Linux commands and third-party tools.


1. Using Built-in Linux Utilities to Check Disk Usage

Linux provides several built-in utilities to monitor disk usage without requiring additional software.

1.1 Identifying Large Files

To find the 10 largest files in the current directory, use:

du . | sort -nr | head -n10

๐Ÿ”น Explanation:

  • du . โ†’ Displays disk usage in the current directory.
  • sort -nr โ†’ Sorts results in descending order.
  • head -n10 โ†’ Shows the 10 largest files.

1.2 Identifying Large Directories

To list the directories occupying the most space in the current directory:

du -s * | sort -nr | head -n10

๐Ÿ”น Explanation:

  • du -s * โ†’ Summarizes directory sizes.
  • sort -nr โ†’ Sorts the results in descending order.
  • head -n10 โ†’ Displays the 10 largest directories.

1.3 Finding the Largest Files Using GNU Tools

You can also use the find command to identify large files.

Method 1: Finding Files Larger Than 20MB

find ~ -type f -size +20000k -exec ls -lh {} \; 2> /dev/null | awk '{ print $NF ": " $5 }' | sort -hrk 2,2

๐Ÿ”น Explanation:

  • find ~ -type f -size +20000k โ†’ Searches for files larger than 20MB in the home directory.
  • -exec ls -lh {} \; โ†’ Displays detailed file information.
  • awk '{ print $NF ": " $5 }' โ†’ Extracts the file name and its size.
  • sort -hrk 2,2 โ†’ Sorts files in descending order by size.

Method 2: Listing the 20 Largest Files in the Home Directory

find ~ -type f -printf '%b %p\0' | sort -rzn | head -zn 20 | tr '\0' '\n'

๐Ÿ”น Explanation:

  • find ~ -type f -printf '%b %p\0' โ†’ Lists files in the home directory with their size in 512-byte blocks.
  • sort -rzn โ†’ Sorts the files in descending order.
  • head -zn 20 โ†’ Displays the 20 largest files.
  • tr '\0' '\n' โ†’ Converts null characters into new lines for better readability.

To improve readability, you can convert file sizes to human-readable formats with numfmt:

find ~ -type f -printf '%b %p\0' | sort -rzn | head -zn 20 | numfmt -z --from-unit=512 --to=iec | tr '\0' '\n'

1.4 Checking Disk Usage by Directories

To check the storage usage of directories in the root (/) file system:

cd /
du -sh * | grep G

๐Ÿ”น Explanation:

  • du -sh * โ†’ Displays the size of each directory in the root file system.
  • grep G โ†’ Filters directories with sizes measured in gigabytes.

2. Using Third-Party Tools for Disk Usage Analysis

In addition to built-in Linux commands, various third-party tools provide more advanced disk usage analysis and visualization.

2.1 iotop (Monitor Disk I/O in Real-Time)

iotop is useful for identifying processes that consume excessive disk resources.

๐Ÿ”น Installation on Debian/Ubuntu:

sudo apt update
sudo apt install iotop-c

๐Ÿ”น Usage:

iotop -oPa

๐Ÿ“Œ Key Parameters:

  • -o โ†’ Shows only processes with active disk I/O.
  • -P โ†’ Displays process paths.
  • -a โ†’ Shows accumulated disk usage over time.

2.2 gdu (Go Disk Usage Analyzer)

gdu is a fast and efficient tool optimized for both SSD and HDD storage.

๐Ÿ”น Installation:

sudo apt install gdu

๐Ÿ”น Usage:

gdu /

๐Ÿ“Œ Navigate through the directories using arrow keys.


2.3 dua (Disk Usage Analyzer)

dua provides an interactive way to explore and free up disk space.

๐Ÿ”น Installation:

curl -LO https://github.com/Byron/dua-cli/releases/latest/download/dua_linux_amd64
chmod +x dua_linux_amd64
sudo mv dua_linux_amd64 /usr/local/bin/dua

๐Ÿ”น Usage:

dua i  # Launch interactive mode to analyze and delete large files

2.4 ncdu (NCurses Disk Usage Analyzer)

ncdu is a terminal-based tool that offers an interactive way to inspect disk usage.

๐Ÿ”น Installation:

sudo apt update
sudo apt install ncdu

๐Ÿ”น Usage:

ncdu /

๐Ÿ“Œ Provides a text-based interface for analyzing disk usage.


Conclusion

This guide covered various methods to analyze and manage disk usage in Linux, from basic commands to advanced third-party tools.

โœ… Essential Commands:

  • du to check file and directory sizes.
  • find to search for large files.
  • sort and awk to organize and format output.

โœ… Recommended Third-Party Tools:

  • iotop to monitor high disk I/O processes.
  • gdu and ncdu for interactive disk usage analysis.
  • dua for analyzing and freeing up storage space.

By applying these methods, you can efficiently optimize storage usage and improve system performance.

Ediciรณn en Espaรฑol: Cรณmo encontrar los archivos y directorios que mรกs espacio ocupan en Linux

Scroll to Top