It is very easy to check the size of directories and files in Linux using the GUI, but it is not always easy to get the size of a directory using the command line.
The ls command can be used to list the contents of a directory, but it does not display the exact directory size and always shows each directory size as 4096 bytes (4 KB), which is the size of space on the disk that is used to store the meta-information for the directory, not what it contains
You can get the actual size of a directory using the du command (Disk Usage), which is widely used by Linux administrators, but you can explore other commands for this purpose. Let’s explore them.
This guide shows you how to find the directory size in Linux using the below three commands:
- du command
- ncdu command
- tree command
Method-1: Get the size of a directory in Linux with du command
The du command refers to disk usage. It is a standard Unix program that is used to estimate disk space usage in the present working directory when no path is specified.
It recursively summarizes the disk usage to obtain a directory and its sub-directory sizes.
Use the below du command format to get the total size of each directory, including sub-directories.
$ du -hc /home/daygeek/Documents/ | sort -rh | head -20 20G total 20G /home/daygeek/Documents/ 9.6G /home/daygeek/Documents/drive-2daygeek 6.3G /home/daygeek/Documents/Thanu_Photos 5.3G /home/daygeek/Documents/Thanu_Photos/Camera 5.3G /home/daygeek/Documents/drive-2daygeek/Thanu-videos 3.2G /home/daygeek/Documents/drive-mageshm 2.3G /home/daygeek/Documents/drive-2daygeek/Thanu-Photos 2.2G /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month 916M /home/daygeek/Documents/drive-mageshm/Tanisha 454M /home/daygeek/Documents/drive-mageshm/2g-backup 415M /home/daygeek/Documents/Thanu_Photos/WhatsApp Video 300M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Jan-2017 288M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-2017 226M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Sep-2017 219M /home/daygeek/Documents/Thanu_Photos/WhatsApp Documents 213M /home/daygeek/Documents/drive-mageshm/photos 163M /home/daygeek/Documents/Thanu_Photos/WhatsApp Video/Sent 161M /home/daygeek/Documents/Thanu_Photos/WhatsApp Images 154M /home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/June-2017
The above command will print the size of each file and the actual size of each directory, including their sub-directory as well as the total size.
du:It’s a command
-h:Print sizes in human readable format (e.g., 1K, 234M, 2G)
-c:Produce a grand total
/home/daygeek/Documents/:The path of directory
sort -rh:Sort the results with numerical value
head -20:Output the first 20 lines result
Use the following du command format to get the total size of a specific directory:
$ du -hs /home/daygeek/Documents or $ du -h --max-depth=0 /home/daygeek/Documents/ 20G /home/daygeek/Documents
If you want to find out the size of the first-level sub-directories, including their sub-directories, for a given directory on Linux, use the following du command format:
$ du -h --max-depth=1 /home/daygeek/Documents/ 3.2G /home/daygeek/Documents/drive-mageshm 4.0K /home/daygeek/Documents/daygeek 756K /home/daygeek/Documents/Bank_Details 9.6G /home/daygeek/Documents/drive-2daygeek 6.3G /home/daygeek/Documents/Thanu_Photos 20G /home/daygeek/Documents/
Method-2: Find the size of a directory in Linux with ncdu command
The ncdu (NCurses Disk Usage) is a curses-based version of the well-known ‘du’ command, and provides a fast way to see which directories are consuming your disk space.
The ncdu command scans the given directory and displays their files and folder sizes recursively as shown below:
$ ncdu /home/daygeek/Documents/ ncdu 1.14.1 ~ Use the arrow keys to navigate, press ? for help --- /home/daygeek/Documents ------------------------------------------------------------------------- 9.6 GiB [##########] /drive-2daygeek 6.3 GiB [###### ] /Thanu_Photos 3.2 GiB [### ] /drive-mageshm 756.0 KiB [ ] /Bank_Details 272.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-TouchInterface1.png 172.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-NightLight.png 164.0 KiB [ ] ConfigServer Security and Firewall (csf) Cheat Sheet.pdf 132.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-Todo.png 112.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png 96.0 KiB [ ] distro-info.xlsx 92.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-ZorinConnect.png 88.0 KiB [ ] disco-duro-png-3.png 72.0 KiB [ ] distro-info-v1.xlsx 44.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-DoNotDisturb.png 36.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-NightLight.jpg 36.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-TouchInterface1.jpg 28.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-Todo.jpg 24.0 KiB [ ] user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.jpg 16.0 KiB [ ] multiple ways to check the hostname in linux.txt 16.0 KiB [ ] ConfigServer Security and Firewall (csf) Cheat Sheet.ods 16.0 KiB [ ] How to Understand Linux Top Command Output and Usage.ods 12.0 KiB [ ] pure-ftpd.png Total disk usage: 19.0 GiB Apparent size: 19.0 GiB Items: 6104
Method-3: Check a directory size in Linux with tree command
The pstree command displays directory contents recursively in a tree-like format, which is very convenient way to display the directory hierarchy and that improves the readability of the output.
$ tree --du -h /opt/ktube-media-downloader /opt/ktube-media-downloader ├── [830K] ktube-media-downloader ├── [ 246] ktube-media-downloader.desktop ├── [2.2M] lnav_0.8.1_amd64.deb └── [ 45M] WgetInterface ├── [ 71K] libwgetinterface.a └── [ 45M] vivaldi-stable_1.7.735.46-1_amd64.deb 49M used in 1 directory, 5 files
As you can see, the tree command output is straight forward compared to the du & ncdu commands.
You have learnt the three different commands to find the size of a directory in Linux.
If you have questions, feel free to leave a comment below, and we will get back to you as soon as we can. Happy learning!