How to Identify Large Folders Taking up Space on Linux

On Windows you have a GUI where you can identify large folders taking up space. On most Linux servers, which are headless (i.e. no GUI desktop), these helpful graphical interfaces are not present because they take up precious resources which should be used for your web server software. You can still have large folders with files you are not using on Linux that can require cleaning up. This is especially true if you use a VPS or dedicated server with SSD drives which are very fast but also expensive per GB and therefore you normally get less space. It is critical to keep your server clean so you are not spending money on storing unnecessary files!

I have used this technique multiple times on Codeable after Dan Dulaney, a fellow Codeable expert, from WP Tech Guides shared this time-saving tip with me.

This tutorial shows you how to use ncdu to identify large folders taking up space on your system, there is an alternative technique using tree and du outlined as well.

Using ncdu to Identify Large Folder

Install ncdu on Debian or Ubuntu with this command

sudo apt-get install ncdu

On CentOS 6.8 and greater

yum install epel-release
yum install ncdu

Enter the root directory (not the root user’s home directory /root)

cd /

Execute ncdu

ncdu

After ncdu scans your entire file system you will be shown the interface below.

Using ncdu you get an awesome drill down menu. The largest folders are shown in ascending order from biggest to smallest.

The idea is you enter the largest folder first which then takes you to a similar menu showing the subfolders again in ascending order.

After entering the subfolder you can see which of those subfolders are taking up the most space.

--- / --------------------------------------------------------------------------
   14.4 GiB [##########] /var
   10.9 GiB [#######   ] /root
    1.3 GiB [          ] /usr
    1.0 GiB [          ] /lib
    1.0 GiB [          ]  swapfile
  197.7 MiB [          ] /boot
   96.2 MiB [          ] /tmp
   40.2 MiB [          ] /run
   15.6 MiB [          ] /bin
   12.9 MiB [          ] /sbin
    7.3 MiB [          ] /etc
e  16.0 KiB [          ] /lost+found
   12.0 KiB [          ] /media
    4.0 KiB [          ] /lib64
e   4.0 KiB [          ] /srv
e   4.0 KiB [          ] /snap
e   4.0 KiB [          ] /opt
e   4.0 KiB [          ] /mnt
e   4.0 KiB [          ] /home
.   0.0   B [          ] /proc
    0.0   B [          ] /sys
 Total disk usage:  29.0 GiB  Apparent size:  29.0 GiB  Items: 449055

I use the arrows to choose the folders and when I am done use q to quit and remove unnecessary files

Here are the commands for controlling ncdu, you should be able to get by with the arrow keys to navigate and q to quit

qncdu helpqqqqqqqqqqqqqqqqq1:Keysqqq2:Formatqqq3:Aboutqqk
    1.1 Gix                                                          x
  164.8 Mix       up, k  Move cursor up                              x
   20.7 Mix     down, j  Move cursor down                            x
    7.7 Mix right/enter  Open selected directory                     x
  812.0 Kix  left, <, h  Open parent directory                       x
   56.0 Kix           n  Sort by name (ascending/descending)         x
   56.0 Kix           s  Sort by size (ascending/descending)         x
   12.0 Kix           C  Sort by items (ascending/descending)        x
    8.0 Kix           d  Delete selected file or directory           x
    8.0 Kix           t  Toggle dirs before files when sorting       x
e   4.0 Kix           g  Show percentage and/or graph                x
    4.0 Kix                        -- more --                        x
    4.0 Kix                                         Press q to close x
    4.0 Kimqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqj

This is by far the fastest way I have learned to identify large folders on Linux systems.

Tree and du

On Ubuntu and Debian systems

sudo apt-get install tree

On CentOS

yum install tree

You can run this command inside the folder you know or suspect to be large

tree --du -d -shaC | grep -Ev '(  *[^ ]* ){2}\[' | more

You press space to jump to the next page and use q to quit displaying additional pages.

Sources

Combine the Best of tree and du