How to get the size of a Linux system directory using the du command

How to get the size of a Linux system directory using the du command

Anyone who has used the Linux system should know that the ls command under the Linux system is usually used to view the contents of the file directory. But have you noticed that the size of each directory displayed by the 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 everything is a file in Linux. I believe everyone has heard this sentence, so the 4 KB you see is just the file size used to store directory metadata, not the directory size in our usual sense. So the question is, 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 disk usage, which stands for disk usage. It is a standard Unix program used to estimate the usage of file space in the current working directory.

The following will take /home/alvin/Documents as an example to introduce the use of the du command.

View the total size of a specified directory

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

20G /home/alvin/Documents

Both of the above methods can calculate the total size of the target directory, where:

  • -h means display in K, M, G and other size formats
  • -s means display the total
  • --max-depth=N means the directory level is N

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

Remove the -s option and do not perform a total count. This will display the size of each directory. The following only displays the first 20 items:

$ 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 *, the size information of files and directories in the specified directory can be displayed:

$ 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 exclude subdirectories from the size statistics:

$ 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

Check the size of the first-level 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 function)

Using the -c option, we can count the results. We can see that the total size of the files and directories in the specified directory is 20 GB, which is consistent with the size of /home/alvin/Documents/ (20 GB) obtained in the first command 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

This is the end of this article about how to get the size of a Linux system directory through the du command. For more information about how to get the size of a Linux system directory through the du command, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

<<:  vue+el-upload realizes dynamic upload of multiple files

>>:  HTML table markup tutorial (2): table border attributes BORDER

Recommend

HTML markup language - form

Click here to return to the 123WORDPRESS.COM HTML ...

How to configure user role permissions in Jenkins

Jenkins configuration of user role permissions re...

Use iframe to submit form without refreshing the page

So we introduce an embedding framework to solve th...

iframe src assignment problem (server side)

I encountered this problem today. I reassigned the...

The whole process of Vue page first load optimization

Table of contents Preface 1. Image Optimization 2...

Will mysql's in invalidate the index?

Will mysql's IN invalidate the index? Won'...

Use of Docker UI, a Docker visualization management tool

1. Introduction to DockerUI DockerUI is based on ...

Introducing multiple custom fonts in CSS3

Today I found a problem in HTML. There are many d...

Detailed explanation of the error problem of case when statement

Preface In the MySQL database, sometimes we use j...

Detailed explanation of angular content projection

Table of contents Single content projection Multi...

Why are the pictures on mobile web apps not clear and very blurry?

Why? The simplest way to put it is that pixels are...

MySQL 5.7.27 installation and configuration method graphic tutorial

MySQL 5.7.27 detailed download, installation and ...

Implementation of select multiple data loading optimization in Element

Table of contents Scenario Code Implementation Su...