The Linux system directory size is obtained through the du command

  • 2021-08-31 09:53:45
  • OfStack

Small partners who have used Linux system know that ls command under Linux system is usually used by us to view the contents of file directories, but I don't know if you have paid attention to it carefully. The size of every directory displayed by ls command is only 4 KB.


$ ls -lh | grep ^d

drwxr-xr-x 3 alvin alvin 4.0K Aug 2 13:57 Bank_Details
drwxr-xr-x 2 alvin alvin 4.0K Mar 15 2019 alvin
drwxr-xr-x 6 alvin alvin 4.0K Feb 16 2019 drive-alvin
drwxr-xr-x 13 alvin alvin 4.0K Jan 6 2019 drive-mageshm
drwxr-xr-x 15 alvin alvin 4.0K Sep 29 21:32 Thanu_Photos

Actually, this is because Linux is a file. I believe everyone has heard this sentence, so the 4 KB you see is only the file size used to store directory meta-information, not the directory size in our usual sense. Then the question comes, how to get the actual size of the file directory? Don't worry, the du command under Linux can help you.

du is the abbreviation of English disk usage, which represents disk utilization. It is a standard Unix program, which is used to estimate the usage of file space in the current working directory.

The use of the du command is illustrated below, using the/home/alvin/Documents as an example.

View the total size of the specified directory


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

20G  /home/alvin/Documents

The total size of the target directory can be calculated in both ways, where:

-h is displayed in K, M, G and other size formats -s denotes display summation --max-depth=N for directory level N

View the size of each directory (including subdirectories) under the specified directory

The-s option is removed, and the size of each directory can be displayed without statistical summation. Only the first 20 items are displayed below:


$ du -h /home/alvin/Documents/ | sort -rh | head -20

20G  /home/alvin/Documents/
9.6G  /home/alvin/Documents/drive-alvin
6.3G  /home/alvin/Documents/Thanu_Photos
5.3G  /home/alvin/Documents/Thanu_Photos/Camera
5.3G  /home/alvin/Documents/drive-alvin/Thanu-videos
3.2G  /home/alvin/Documents/drive-mageshm
2.3G  /home/alvin/Documents/drive-alvin/Thanu-Photos
2.2G  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month
916M  /home/alvin/Documents/drive-mageshm/Tanisha
454M  /home/alvin/Documents/drive-mageshm/2g-backup
415M  /home/alvin/Documents/Thanu_Photos/WhatsApp Video
300M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017
288M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017
226M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017
219M  /home/alvin/Documents/Thanu_Photos/WhatsApp Documents
213M  /home/alvin/Documents/drive-mageshm/photos
163M  /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent
161M  /home/alvin/Documents/Thanu_Photos/WhatsApp Images
154M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017
150M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016

View the size of each file and directory in the specified directory

Combined with the use of wildcard character *, you can achieve the effect of displaying the size information of files and directories under the specified directory:


$ du -hs /home/alvin/Documents/* | sort -rh | head -10

9.6G  /home/alvin/Documents/drive-alvin
6.3G  /home/alvin/Documents/Thanu_Photos
3.2G  /home/alvin/Documents/drive-mageshm
756K  /home/alvin/Documents/Bank_Details
272K  /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png
172K  /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png
164K  /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf
132K  /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png
112K  /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png
96K  /home/alvin/Documents/distro-info.xlsx

View the size of each directory under the specified directory (excluding subdirectories)

Use the-S option to count the size in the form of excluding subdirectories:


$ du -hS /home/alvin/Documents/ | sort -rh | head -20

5.3G  /home/alvin/Documents/Thanu_Photos/Camera
5.3G  /home/alvin/Documents/drive-alvin/Thanu-videos
2.3G  /home/alvin/Documents/drive-alvin/Thanu-Photos
1.5G  /home/alvin/Documents/drive-mageshm
831M  /home/alvin/Documents/drive-mageshm/Tanisha
454M  /home/alvin/Documents/drive-mageshm/2g-backup 
300M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Jan-2017
288M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2017
253M  /home/alvin/Documents/Thanu_Photos/WhatsApp Video
226M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Sep-2017
219M  /home/alvin/Documents/Thanu_Photos/WhatsApp Documents
213M  /home/alvin/Documents/drive-mageshm/photos
163M  /home/alvin/Documents/Thanu_Photos/WhatsApp Video/Sent
154M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/June-2017
150M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2016
127M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2016
100M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Oct-2016
94M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Nov-2017
92M  /home/alvin/Documents/Thanu_Photos/WhatsApp Images
90M  /home/alvin/Documents/drive-alvin/Thanu-photos-by-month/Dec-2017

View the size of level 1 subdirectories in the specified directory

This is very simple, just set max-depth to 1:


$ du -h --max-depth=1 /home/alvin/Documents/

3.2G  /home/alvin/Documents/drive-mageshm
4.0K  /home/alvin/Documents/alvin
756K  /home/alvin/Documents/Bank_Details
9.6G  /home/alvin/Documents/drive-alvin
6.3G  /home/alvin/Documents/Thanu_Photos
20G  /home/alvin/Documents/

View the size of the specified directory (including statistics)

Using the-c option to count the results, you can see that the sum of the sizes of the files and directories in the specified directory is 20 G, which is equal to the size of/home/alvin/Documents/20 G in Command 1 above:


$ du -hsc /home/alvin/Documents/* | sort -rh | head -10

20G  total
9.6G  /home/alvin/Documents/drive-alvin
6.3G  /home/alvin/Documents/Thanu_Photos
3.2G  /home/alvin/Documents/drive-mageshm
756K  /home/alvin/Documents/Bank_Details
272K  /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png
172K  /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-NightLight.png
164K  /home/alvin/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf
132K  /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png
112K  /home/alvin/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png

Related articles: