- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find the Total Size of All Files in a Directory on Linux
You can use various types of commands to get the total size of all files in a directory on Linux. There are also some GUI tools in Linux to display the total size of the directory in a more simple format.
Getting the correct information about the directories can help a user to find the storage allocation of the complete system. That's why Linux users always look for ways to find the total size of the directory.
In this guide, we will explain various methods to find the total size of all files in the directory on Linux.
Finding the Total Size of All Files in a Directory on Linux
Let's start with the commands you can try to display the correct size of the files in a directory −
The du Command
By default, most Linux distributions include the du command, which stands for disk usage. Through this command, you can get the directory size in many formats, so let's run the du command simply −
~$: du 830720 ./Information 166144 ./Images 996868
When you type the du command without arguments, it only shows the total directory size in kilobytes. Hence, if you want to find the current directory size only, excluding the sub-directories, you can use the --summarize or -s option −
~$: du -s 996868 .
You can use the '-h' option to get the output in a more readable format.
~$: du -h 812M ./Information 163M ./Images 974M . ~$: du -sh 974M .
The number indicates the space used, and the letters M, G, and K define megabytes, gigabytes, and kilobytes.
Bonus Tip − The du command tells the size of a directory by default. You can find its apparent size by adding the --apparent-size option to the du command.
~$: du -sh --apparent-size 973 .
The 'apparent size' refers to the actual size of the directory. If you want to see the size of a specific directory instead of your current directory, you can use the command below −
~$: du -sh ~/Documents 974M /home/prateek/Documents
Note − You may find errors in some entries because you don't have permission to access that directory. Use the sudo /su command to get admin privileges, which will remove your error.
You can get the directory size in the format you want. For this, you need to add the size format such as 'k' (for kilobytes) or 'm' for (megabytes) as follows −
~$: du -k ~/Documents 830720 /home/prateek/Documents/Information 166144 /home/prateek/Documents/Images 996868 /home/prateek/Documents
Use the -c option with the du command to find the total size of the directory −
~$: du -sch 974 . 974 total
You can find the directory size excluding some files using the below du command −
~$: du -sch --exclude '*.sh' 974 . 974 total
The above command displays the directory size, excluding the size of mentioned files. Using the 'max-depth' option, you can set the limit of the scan to a certain level.
du -hc --max-depth=N <path of the directory> Or du -dN -hc
Here, N is used for the level of directories. For example, let's scan the first layer of the subdirectory −
~$: du -hc --max-depth=1 4.0K ./Pictures 974M ./Documents 4.0K ./Music 12K ./.gnupg 113M ./snap 60M ./.cache 776K ./.config 664K ./.local 4.0K ./Desktop 4.0K ./Videos 4.0K ./.ssh 4.0K ./Templates 4.0K ./Public 8.0K ./opt 4.0K ./Downloads 1.2G 1.2G total
You will notice that in the above commands, we have removed the -s argument and used the -d argument so that you can find out how much space a particular directory is occupying. This way, you can find out the size of multiple directory levels.
Using the du command with the -a flag, you can get the directory size and its file and sub-directories.
du -ah
To sort the sub-directories according to the disk size used by them, use the following command −
~$: du -h --max-depth=N | sort -hr 974 . 812 ./Information 163 ./Images
The subdirectory using the maximum space will be displayed on the top.
The ncdu Command
The ncdu command stands for 'ncdu disk usage,' which is not pre-installed in some Linux distros. You can easily install it using the following command −
sudo apt install ncdu -y (for Ubuntu/Debian) sudo yum install ncdu (RedHat/CentOS)
This command shows the interactive display of your directory as follows −
ncdu
Once you execute the following command, it will display the complete information about the total size of the directory −
ncdu 1.15.1 - Use the arrow keys to navigate press ? for help --- /home/prateek ---------------------------------------------------------- 973.5 MiB [####### ##] /Documents 112.4 MiB [# ] /snap 59.2 MiB [ ] /.cache 776.0 KiB [ ] /.config 664.0 KtB [ ] /.local 12.0 KiB [ ] /.gnupg 8.0 KiB [ ] /opt e 4.0 KiB [ ] /Videos e 4.0 KiB [ ] /Templates e 4.0 KiB [ ] /Public e 4.0 KiB [ ] /Pictures e 4.0 KiB [ ] /Music e 4.0 KiB [ ] /Downloads e 4.0 KiB [ ] /Desktop e 4.0 KiB [ ] /.ssh 4.0 KiB [ ] .bashrc 4.0 KiB [ ] .bash history 4.0 KiB [ ] .profile 4.0 KiB [ ] .bash_logout 4.0 KiB [ ] TestFile.txt 0.0 B .sudo_as_admin_successful Total disk usage: 1.1 GiB Apparent size: 1.1 GiB Items: 3924
In the left corner, you can see the current directory scanned. You can see the directory size in the first column of the table. Using the down and up arrows, you can move between different lines. Moreover, you can browse through a directory with the right arrow, and with the left arrow, you can back out.
You can define the path to the directory with the ncdu command to target a specific directory.
ncdu /var
This command will display the following information −
ncdu 1.15.1 ~ Use the arrow keys to navigate, press ? for help --------/var----------------------------------------------------------------- . . 2.7 GiB [##########] /lib . 573.2 MiB [## ] /cache . 92.2 MiB [ ]/log 3.7 MiB [ ]/snap 3.4 MiB [ ]/backups . 48.0 KiB [ ]/tmp . 48.0 KiB [ ]/spool e 4.0 KiB [ ]/opt e 4.0 KiB [ ]/metrics e 4.0 KiB [ ]/mail e 4.0 KiB [ ]/local e 4.0 KiB [ ]/crash @ 0.0 B [ ]lock @ 0.0 B [ ]run Total disk usage: 3.3 GiB Apparent size: 3.3 GiB Items: 12731
The tree Command
The tree is a command-line utility that lists files or directories in a tree-like format. Some versions of Linux do not have the tree command by default, but it is available in your Linux repositories. Let's install the tree command utility through the below command −
sudo apt install tree -y (for Ubuntu/Debian) sudo yum install tree (for RedHat/CentOS)
If we run this command without any argument, it will only list all the directories and subdirectories. Hence, please use the '-d' and 'h' options with it as follows −
~$: tree -dh [4.0K] . |__ [4.0K] Images |__ [ 20K] Information
You can use the above command to find the directory size of your current directory. Here we use,
-d to indicate only the directories.
-h to read the directory size in human-readable form.
You can also find the directory size with their files by removing the -d flag from the above command.
~$: tree -h
Similar to the du and ncdu command, you can also find the size of a specific directory with its help. For this, you have to include the path of the directory after the tree command like this −
~$: tree -dh ~/Documents [4.0K] /home/prateek/Documents |__ [4.0K] Images |__ [ 20K] Information
Bonus Tip − You can also combine the 'du' and 'tree' commands as follows −
~$: tree --du -h
GUI Tools You Can Try
There are a few tools you can use to find out the total size of the directory on Linux. Here, we have only included open-source and free tools.
QDirStat
It is a Qt-based file/directory system analyzer showing directories and files in a heat-map representation like a tree system. With the help of this tool, you can find out the directory usage and clean the system by deleting or cleaning files, etc. Use the below command to install this tool −
~$: sudo apt install qdirstat -y
You can open this tool by entering the 'qdirstat' in the terminal or searching for the tool from the application menu.
FileLight
It is a very lightweight and easy-to-use GUI tool that allows you to view the space occupied by directories and files in the form of a pie chart. It uses concentric rings to show the size and usage of the directory so that you can understand it better. You can install it by running the following command in the terminal −
~$: sudo apt install filelight -y
Once successfully installed, you can open it from the applications menu or enter the 'filelight' in the terminal −
To view the disk usage of a particular file or directory, click on 'scan' in the left corner and follow these steps −
Scan > Scan folder > The folder/directory you want to scan
Conclusion
You can find the total size of all the files in a directory in Linux through the GUI and CLI methods. If you are new to Linux, you can find out the size of the directory and its files using the GUI method.
There are many tools with the help of which you can find this, but in this guide, we have included two tools, 'QDirStat' and 'FileLight,' which are the latest, free, and open-source. You can easily install these tools and check the size of directories in graphical form. This way, you can find the total size of all the files in the directory through any method at your convenience.