How to Find Most Used Disk Space Directories and Files in Linux

To Find Most Used Disk Space Directories or To find which directories and files are using the most disk space in Linux, you can use the du (disk usage) command along with some other tools. Here are a few ways to locate the largest directories and files:

Below is some useful tips to Find Most Used Disk Space Directories

1. Finding the Largest Directories

The du command can help you display the disk usage of directories.

Example Command:

du -ah / | sort -rh | head -n 10

Explanation:

  • du -ah /: Lists the disk usage of all files and directories under the root (/).
    • -a: Displays sizes of both files and directories.
    • -h: Human-readable format (e.g., KB, MB, GB).
  • sort -rh: Sorts the output in reverse order by size (r for reverse, h for handling human-readable numbers).
  • head -n 10: Shows the top 10 largest directories or files.

You can replace / with a specific directory path if you want to target a particular directory.

2. Finding the Largest Directories (Summarized)

If you want to find the largest directories but skip the individual files:

du -sh /* 2>/dev/null | sort -rh

Explanation:

  • du -sh /*: Summarizes disk usage for each top-level directory in the root directory (/).
    • -s: Summarizes the size of each directory.
  • 2>/dev/null: Suppresses any error messages (e.g., permission denied).
  • sort -rh: Sorts by size in reverse order.

3. Finding the Largest Files

To search for the largest files specifically, use the find command in combination with du:

Example Command:

find / -type f -exec du -h {} + | sort -rh | head -n 10

Explanation:

  • find / -type f: Finds all files (-type f) under the root directory (/).
  • -exec du -h {} +: Runs du on each file found, displaying the size in human-readable format.
  • sort -rh: Sorts the output by size in reverse order.
  • head -n 10: Displays the top 10 largest files.

If you want to search within a specific directory, replace / with the directory path.

4. Using ncdu for Interactive View

For a more user-friendly, interactive way to view disk usage, you can install and use ncdu (NCurses Disk Usage):

Install ncdu:

sudo apt install ncdu # On Debian/Ubuntu-based systems
sudo yum install ncdu # On CentOS/RHEL-based systems

Run ncdu:

ncdu /

This will give you an interactive, navigable view of disk usage, making it easy to find large files and directories.

5. Using ls to Find the Largest Files in a Directory

To find the largest files within a specific directory:

ls -lhS /path/to/directory | head -n 10

Explanation:

  • ls -lhS: Lists files in the directory, sorted by size (-S) in human-readable format (-h).
  • head -n 10: Shows the top 10 largest files.

Summary of Common Commands

  • Top disk usage directories (detailed): du -ah / | sort -rh | head -n 10
  • Top disk usage directories (summary): du -sh /* 2>/dev/null | sort -rh
  • Top largest files: find / -type f -exec du -h {} + | sort -rh | head -n 10
  • Interactive tool: ncdu /

That’s all for now. you will Find Most Used Disk Space Directories using the above article. Even a novice administrator can easily find them and will work on it.

If you needs to delete All files before a certain date in linux click here.

By vpsadmn