In this tutorial, you will learn how to use two simple and lightweight command line tools to help you analyze the space usage of your files and folders both on your local machine and on a remote system. Let’s get started.
The ‘du’ utility, which stands for disk usage, is used to provide an estimation of file space usage. It can also track folders and files which are consuming large amounts of space.
In order to find the size of a directory, issue the command below by replacing the the_file_path with the directory of your choice.
du -sh the_file_path
- du (disc usage) tool estimates the_file_path space usage
- The options -sh are:
-s, –summarize displays only total for each argument (directory)
-h, –human-readable human readable format ted size display (e.g., 10K 14M 4G)
check directory size linux
In order to check many directories at once and see the total, run the command :
-c, –displays a grand total
Total folder size Linux
If you do not want to include the subdirectories, use the argument :
The ncdu utility, which stands for NCurses Disk Usage, allows to see the directories that are using disk space the most.This C-programmed tool is a fast and simple space usage analyzer that can show the files or directories that take up a lot of space not only on the local hard disk but also on a remote destination location.
Ncdu can be found in major Linux distros’ default repositories.
On Arch Linux, Manjaro Linux, Antergos: Run the command below :
sudo pacman -S ncdu
On RHEL,Scientific Linux, CentOS:Run the command below
sudo yum install ncdu
On Fedora: Run the command below :
sudo dnf install ncdu
On SUSE, openSUSE: Run the command below
sudo zypper in ncdu
On Ubuntu and the like, you can install it by running the command :
sudo apt-get install ncdu
After the installation, invoke the following command :
This will bring up the screen below :
disk usage in ubuntu
As you can see, It will first analyze your current folder and will display space usage data where large files will be displayed on top (sorted in descending order).
You may be interested to read: How to find the largest files on Linux
As mentioned at the top of the screen in the snapshot above, you could use arrows (up/down) to move between items or keys (k, j) as well as shown in the small window below :
Once a line is selected , hit “i” key to view its details, for instance :
In order to hide the window, just press “i”.
If you would like to see the files and folders within a selected directory, just press ENTER key or the ‘right’ arrow : here we can see the files inside the directory “/.cache”
To return to the main screen or to the parent directory, press “left” arrow. The depth of navigating down the directories has no limit as long as there are folders to select and open.
It is also possible to display the size of a specific directory by indicating its path as shown below :
In order to analyze the entire root directory, run the command below:
sudo ncdu -x /
The -x switch is used to count only directories and files on the same filesystem as the given directory. In this way, mounted devices will not be scanned in case they have a different filesystem.
While analyzing a directory, ncdu will by default, update the data 10 times per second. In case you are performing a remote disk usage analysis, note that this might consume additional bandwidth. In quiet mode, this can be brought down to 2 seconds (a refresh every 2 seconds).
In order to run ncdu in quiet mode, run ncdu using the -q switch as shown below :
The analysis results can be archived and exported to any destination directory. This can be achieved as as shown in the command below :
ncdu -1xo- / | gzip > analysis.gz
For instance for the folder /.cache, this can be done as follows :
In order to display the analysis report that you created above (analysis.gz), run the command below :
zcat analysis.gz | ncdu -f-
To both analyze, export and view the report at the same time f the current directory, execute the command below :
ncdu -o- | tee analysis.file | ncdu -f-
If you want to include gzip compression, you would need to proceed as shown below :
ncdu -o- | gzip | tee analysis.gz | gunzip | ncdu -f-
You may be interested to read: Gunzip command in Linux
In case you would like to delete a selected file or directory on the ncdu screen, just press “d”. This will bring up the prompt box below :
If you want to know more about ncdu, run the command man ncdu in your terminal.
You will find du and ncdu tools very useful when you would like to find the size of files or directories on your hard disk or on your remote system. If you would like to find the largest files or directories using other methods including GUI based tools, you can read our article here.