3 Simple Ways to Get the Size of Directories in Linux

Two days ago I got a mail from our regular reader, who asked me how to get a summary of directories in Linux.

I know this can be achieved by the disk usage (Do) command, which I didn’t even find on my first attempt.

So I used the possible combination by digging into the Du Command Man page and finally got the best result.

Whenever a Linux administrator receives such a request, they can immediately assume that this is achieved using the du command (Disk Usage) and df (Disk FileSystem) command.

But alternatively, you can use the ncdu command or the tree command to achieve the same results.

By default the du command displays the size of the current directory files, which does not display the directory and its sub-directory size.

Read the article below to quickly summarize the size of each directory and their sub-directory.

Method-1: How to Get the Size of a Directory in Linux Using the Disk Usage (du) Command

The du command refers to disk usage. It is a standard Unix program that is used to estimate file space usage in the present working directory.

It recursively summarizes the disk usage to obtain a directory and its subdirectory size.

As I said at the beginning of the article, we are going to use the Disk Usage (Do) command with some options to achieve this. So use the following disk usage command combination to get the summary of folders and their sub-folders.

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 subdirectory and total size.

Details of the above command:

  • 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 below du command format to get the total size of a given directory.

$ du -hs /home/daygeek/Documents
or
$ du -h --max-depth=0 /home/daygeek/Documents/

20G    /home/daygeek/Documents

If you want to get the size of the first-level sub-directories, including their sub-directories, for a given directory on Linux, use the du command format below.

$ 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: How to Get the Size of a Directory in Linux Using the ncdu (NCurses Disk Usage) Command

The ncdu (NCurses Disk Usage) is a curses-based version of the well-known ‘du’, and provides a fast way to see what directories are using your disk space.

$ 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

The ncdu command scans the given directory and displays the files and folder size recursively.

Method-3: How to Get the Size of a Directory in Linux Using the tree Command

The tree command is a recursive directory listing program that produces a depth indented listing of files and directories in a tree-like format.

$ 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

The two commands above show directory summary, but the tree command will tell you every file size inside the directory and their subdirectory, and print the summary of the directory.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...