Get the App
SLTechnology News&Howtos  ›  Development  › 

How to get the directory size under Linux

Shulou Source: shulou.com Published: 2022-06-01 16:02:50 10月01日 Update

This article will explain in detail how to get the directory size under Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

When using Linux, we may need to get the size of a specific directory. In GUI, there are file managers, such as Dolphin,Gnome,Nautilus, etc. But what if we need to get the size on the command line.

There are some applications on Linux that can get the actual size of the directory. Among them, the disk utilization (du) command has been widely used by Linux administrators.

I'll show you how to use various options to get the folder size.

What is the du command?

The du command indicates disk utilization (Disk Usage). This is a standard Unix program for estimating file space usage in the current working directory.

It uses recursion to summarize disk usage to get the size of the directory and its subdirectories.

As I said, when using the ls command, the directory size shows only 4KB. See the output below.

$ls-lh | grep ^ ddrwxr-xr-x 3 daygeek daygeek 4.0K Aug 2 13:57 Bank_Detailsdrwxr-xr-x 2daygeek daygeek 4.0K Mar 15 2019 daygeekdrwxr-xr-x 6 daygeek daygeek 4.0K Feb 16 2019 drive-2daygeekdrwxr-xr-x 13 daygeek daygeek 4.0K Jan 6 2019 drive-mageshmdrwxr-xr-x 15 daygeek daygeek 4.0K Sep 29 21:32 Thanu_Photos1) how to get only the size of the parent directory on Linux

Use the following du command format to get the total size of a given directory. In this example, we will get the total size of the / home/daygeek/Documents directory.

$du-hs / home/daygeek/Documents

Or

$du-h-max-depth=0 / home/daygeek/Documents/20G / home/daygeek/Documents

Details:

Du.-that's an order.

-h-displays the size in an easy-to-read format (for example, 1K 234m 2G)

-s-displays only the total number of each parameter

-max-depth=N-the print depth of the directory

2) how to get the size of each directory on Linux

Use the following du command format to get the total size of each directory, including subdirectories.

In this example, we will get the total size of each / home/daygeek/Documents directory and its subdirectories.

$du-h / home/daygeek/Documents/ | sort-rh | head-2020g / home/daygeek/Documents/9.6G / home/daygeek/Documents/drive-2daygeek6.3G / home/daygeek/Documents/Thanu_Photos5.3G / home/daygeek/Documents/Thanu_Photos/Camera5.3G / home/daygeek/Documents/drive-2daygeek/Thanu-videos3.2G / home/daygeek/Documents/drive-mageshm2.3G / home/daygeek/Documents/drive-2daygeek/ Thanu-Photos2.2G / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month916M / home/daygeek/Documents/drive-mageshm/Tanisha454M / home/daygeek/Documents/drive-mageshm/2g-backup415M / home/daygeek/Documents/Thanu_Photos/WhatsApp Video300M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Jan-2017288M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-2017226M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Sep-2017219M / home/daygeek/Documents/Thanu_Photos/WhatsApp Documents213M / home/daygeek/Documents/drive-mageshm/photos163M / home/daygeek/Documents/Thanu_Photos/WhatsApp Video/Sent161M / home/daygeek/Documents/Thanu_Photos/WhatsApp Images154M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/June-2017150M / home/daygeek/Documents/drive-2daygeek/ Thanu-photos-by-month/Nov-20163) how to get a summary of each directory on Linux

Use the following du command format to get only the summary of each directory.

$du-hs / home/daygeek/Documents/* | sort-rh | head-109.6G / home/daygeek/Documents/drive-2daygeek6.3G / home/daygeek/Documents/Thanu_Photos3.2G / home/daygeek/Documents/drive-mageshm756K / home/daygeek/Documents/Bank_Details272K / home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png172K / home/daygeek/Documents/user-friendly-zorin-os-15 -has-been-released-NightLight.png164K / home/daygeek/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf132K / home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png112K / home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.png96K / home/daygeek/Documents/distro-info.xlsx4) how to get the size of each directory without subdirectories on Linux

Use the following du command format to show the total size of each directory, excluding subdirectories.

$du-hS / home/daygeek/Documents/ | sort-rh | head-205.3G / home/daygeek/Documents/Thanu_Photos/Camera5.3G / home/daygeek/Documents/drive-2daygeek/Thanu-videos2.3G / home/daygeek/Documents/drive-2daygeek/Thanu-Photos1.5G / home/daygeek/Documents/drive-mageshm831M / home/daygeek/Documents/drive-mageshm/Tanisha454M / home/daygeek/Documents/drive-mageshm/2g-backup300M / home/ Daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Jan-2017288M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-2017253M / home/daygeek/Documents/Thanu_Photos/WhatsApp Video226M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Sep-2017219M / home/daygeek/Documents/Thanu_Photos/WhatsApp Documents213M / home/daygeek/Documents/drive-mageshm/photos163M / home/daygeek/Documents/ Thanu_Photos/WhatsApp Video/Sent154M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/June-2017150M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Nov-2016127M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Dec-2016100M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Oct-201694M / home/daygeek/Documents/drive-2daygeek/Thanu-photos -by-month/Nov-201792M / home/daygeek/Documents/Thanu_Photos/WhatsApp Images90M / home/daygeek/Documents/drive-2daygeek/Thanu-photos-by-month/Dec-20175) how to get only the size of a first-level subdirectory on Linux

If you want to get the size of a first-level subdirectory (including its subdirectories) of a given directory on Linux, use the following command format.

$du-h-- max-depth=1 / home/daygeek/Documents/3.2G / home/daygeek/Documents/drive-mageshm4.0K / home/daygeek/Documents/daygeek756K / home/daygeek/Documents/Bank_Details9.6G / home/daygeek/Documents/drive-2daygeek6.3G / home/daygeek/Documents/Thanu_Photos20G / home/daygeek/Documents/6) how to get the total in the du command output

If you want to get the totals in the du command output, use the following du command format.

$du-hsc / home/daygeek/Documents/* | sort-rh | head-1020g total9.6G / home/daygeek/Documents/drive-2daygeek6.3G / home/daygeek/Documents/Thanu_Photos3.2G / home/daygeek/Documents/drive-mageshm756K / home/daygeek/Documents/Bank_Details272K / home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-TouchInterface1.png172K / home/daygeek/Documents/user-friendly-zorin-os -15-has-been-released-NightLight.png164K / home/daygeek/Documents/ConfigServer Security and Firewall (csf) Cheat Sheet.pdf132K / home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-Todo.png112K / home/daygeek/Documents/user-friendly-zorin-os-15-has-been-released-ZorinAutoTheme.pngvia: https://www.2daygeek.com/find-get-size-of-directory-folder-linux-disk-usage-du-command/

This is the end of the article on "how to get the directory size under Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Size directory command subdirectory subdirectory format file disk article output utilization situation summary more examples program this is management good practical Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL NVidia Redmi OPPO Reno Apple