How to find Directories & Files taking up the most Disk Space on Linux

Estimated reading: 3 minutes 317 views

As a Linux administrator, you must periodically check which files and folders are consuming more disk space. It is very necessary to find unnecessary junk and free up them from your hard disk.

In this Linux quick tip we will show you how find large files and directories.

Method 1. Find the largest files & directories using ” du ” command

1. Find Directories Using Most Disk Space

The du command allows you to check disk usage from the command line. Used alone it will recursively go through each child directory and show it’s size. However, if you pair it with a few other commands you can narrow down your search. In the example below we use the du -h command to list the directories in human readable fromat. Then we pipe the du output to sort. This allows us to do a human readable numeric sort in reverse order -hr. This will place the largest directories at the top of the list

# du -h | sort -hr | head -10

A good way to narrow down the search is to limit the du command to only the top directories (max directory depth). This allows you to discover which top level directory is taking up the most space. Then you can change directory into it and do another run. Here we tell the du command to only go one directory deep with the -d 1 option.

# du -d 1 | sort -n -r

Now we know that the “cache” folder is storing the most data. We can cd in that folder and run to see which sub -folder is using the most capacity.

This method is especially helpful when you have large amounts of directories.

2. Find Largest Files & Directories

By default, the du command only shows directories. With the -a (all) option it will show you files in addition to directories.

# du -ah | sort -hr | head -5

 

 

As you can see, it is now reporting files as well as directories.

3. Find Largest Files Only

The du command is not the right tool for the job if we are seeking only files. For this task we must turn to the find command. In the example below we are using find . -type f. This instructs find to look in the current directory for files only. We then pass the output -exec to run against the du -a command.

# find -type f -exec du -ah {} + | sort -hr | head -5

Method 2: Find the largest files & directories using NCDU

NCDU (NCURSE Disk Usage) is a Linux program to statistical hard drive capacity. This application will be extremely helpful when you need to determine which folder or file is consuming the most places in the server, the common problem when managing VPS.

1. Install Ncdu

  • On Ubuntu/Debian
# apt install ncdu
  • On CentOS
# yum install ncdu

2. Use NCDU

To start checking the capacity of the folder on the machine, using the command

# ncdu /

Wait for about 1 minute for the program to scan the whole folder, then the program will show the folder structure with the capacity

You can use the arrow up and down to select the folder. Click Enter to see details of the sub -folder list, or click the ” i ” button to view the detailed information of the current folder.

To see the details of the control commands of NCDU, press the “Shift + ?”

To escape the program, press the q button.

Wish you use success, Linux administration more effectively.

Good Luck!

Leave a Comment