How to get the Size of a Directory in Linux

Here's are the command-line ways to find out the size of file and folders in your Linux system.

In our everyday interaction with computers, knowing the size of files and folders in our systems is essential. With Linux systems, getting the size of files and directories in the GUI mode is straightforward. All you need to do is right-click on the specific directory and select the properties option. However, knowing how to get the size via the command-line (Terminal) can be equally important. If you are a system admin managing a server without a GUI, this method will come in handy.

This brief tutorial will give you a detailed guide on how to get the size of a Directory in Linux systems via the command-line.

Getting the Size of a Directory in Linux

To get the size of a directory via Terminal, we will use the du command. DU stands for Disk Usage. It lists the amount of space occupied by different files and directories.

The general syntax of the du command is as follows;

du [OPTION]... [FILE] [directory]...

If you execute the du command on a specified directory, it will summarize every sub-director disk usage. If no path is specified, the du command will disk usage report of the current working directory.

Let’s run the du command on our home directory. Take a closer look at the output.

$ du

du command
du command

The du command has given a detailed disk usage report of the directories and sub-directories present from the image above.

To display the size of a specified directory, e.g., .cache (hidden folder), run the command below.

du .cache

du command
du command

To display the output in ‘human-readable format,‘ use the -h option as in the example below.

du -h .cache

du command
du command

You can now see the size of directories in kilobytes (K) and megabyte (M) from the image.

Alternatively, you can specify the unit size that you want the du command to use. That can be -k for kilobytes and -m for megabytes.

du -k .cache/

du -m .cache/

To list sub-directories’ size with those occupying the largest size on the top, we use the -hr option. See the command below.

du -h --max-depth=1 | sort -hr

From the above output, all sub-directories occupying the largest disk space are displayed at the top. You can increase the du report deep level by increasing the –max-depth parameter to view more sub-directories.

To get the size of a directory omitting the sub-directories, we will use the -sh option.

sudo du -sh /var

The output will look as below.

sudo du -sh var
sudo du -sh var

Let’s take a look at the arguments used in the above command.

sudo: Since the root user owns most files and directories, we will need to use the sudo command to get elevated privileges.

s: To display only the size of the main directory and omit sub-directories.

h: Print the unit size in a human-readable format.

/var: The path to directory/ folder you want to get the size./var:

Conclusion

I believe the example above has given you a clear guide of getting the size of a directory using the du command. If you are managing a Remote Desktop server via utilities like Putty, you are limited to using the command-line with no access to a GUI. The above will come in handy when trying to get the size of directories.

If you have any questions or remarks about this tutorial, feel free to leave a comment below.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,281FansLike
386FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.

10 Best Screen Capturing Software for Linux

Do you want to make a video tutorial for YouTube or show-off your epic victory royale to your friends? Look no further — as we have compiled the 10 Best Screen Capturing Software for Linux. Whether you need to record your desktop screen or take a simple screenshot, FOSSLinux has got your back.

6 best task managers for Linux

One of the essential tools in any Linux distribution is a Task Manager. It is a system monitor application that gives you a report of all programs running on your computer and the status of your RAM and CPU usage.

Top 5 reasons to switch from Windows to Linux right now

Do you love Windows or Linux? Being on a Linux website, it's not hard to take a guess, but what are the strong reasons to switch to Linux? Linux is a 100% free OS. Is that reason alone enough? Let's find out! Never have there been more compelling reasons to make the switch. Here are just five great reasons for users to create that magical move from the wayward world of Windows to the lauded land of Linux.

VIDEO: MX Linux 19 Features and Desktop Tour

MX Linux is a Linux distribution based on Debian stable and using core antiX components. The distributions MX Tools is very popular among users and combined with other several ready-to-use tools, it is great for users who prefer to tweak their distro to their liking. In this video, we will take you through the features tour showing casing MX Linux 19.