How to identify folders occupying a lot of space on Linux

In Windows you have a graphical interface where you can determine which folder takes up how much space. Most Linux servers don’t have a graphical front end (i.e. no GUI desktop) because they take up valuable resources that must be used for your web server software. You may still have large file folders that Linux doesn’t use that might need cleaning up. This is especially true if you are running a VPS or dedicated server with SSD drives, which are very fast but also expensive per GB and therefore you usually get less space. It’s very important to keep your server clean so you don’t waste money on storing unnecessary files!

This article will show you how to use ncdu to determine the size of folders on your system, there is an alternative method using tree and du.

Using ncdu to identify a large folder

Install ncdu on Debian or Ubuntu with this command

                      sudo apt-get install ncdu
                    

On CentOS 6.8 and above

                      yum install epel-release
yum install ncdu
                    

Enter the root directory (not the home directory of the root user / root)

                      cd /
                    

Execute ncdu

                      ncdu
                    

Once executed, ncdu will scan the entire file system, the interface will be shown below.

Using ncdu, you get the menu. The largest folders are displayed in ascending order from largest to smallest.

The idea is to show the large folders first, then show the subdirectories again in ascending order.

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

                      --- / --------------------------------------------------------------------------
   10.4 GiB [##########] /var
    8.1 GiB [#######   ] /root
    2.2 GiB [          ] /usr
    2.2 GiB [          ] /lib
    2.1 GiB [          ]  swapfile
  134.5 MiB [          ] /boot
   67.6 MiB [          ] /tmp
   34.7 MiB [          ] /run
   12.8 MiB [          ] /bin
   11.4 MiB [          ] /sbin
    6.3 MiB [          ] /etc
e   4.1 KiB [          ] /lost+found
    8.1 KiB [          ] /media
    2.1 KiB [          ] /lib64
e   2.1 KiB [          ] /srv
e   2.1 KiB [          ] /snap
e   2.1 KiB [          ] /opt
e   2.1 KiB [          ] /mnt
e   2.1 KiB [          ] /home
.   0.0   B [          ] /proc
    0.0   B [          ] /sys
 Total disk usage:  28.0 GiB  Apparent size:  28.0 GiB  Items: 354728
                    

We use the arrow to select folders and use q to exit and delete unneeded files

Here are the commands to control ncdu, you should be able to get using the arrow keys to move and q to exit

                      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 we’ve 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 in a folder that you know or suspect might be large

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

Press space to go to the next page and use q to get to the additional pages screen.

Related Posts