User Tools

Site Tools


how_to_find_the_size_of_folders

How to Find the Size of a Folder

2016
Updated Nov 2023

This article explains 2 simple commands that most people want to know when they start using Linux. They are finding the size of a directory and finding the amount of free disk space that exists on your machine. The command you would use to find the directory size is du.

To find the free disk space you could use df.

All the information present in this article is available in the man pages for du and df. In case you get bored reading the man pages and you want to get your work done quickly, then this article is for you.


Better Examples

To find the size of a directory that you are currently in

  du -h -s

For example, here is the contents of my home folder:

drwxr-xr-x. 2 root root  90 Aug  9 11:05 nics
drwxr-xr-x. 3 root root 135 Nov  8 18:22 openstack
drwxr-xr-x. 3 root root  17 Aug  9 11:12 streamer

To see the combined size of these folders, I use:

  du -h -s

This gives me the output:

  30G     .

Lets say I am interested in how much is in the 'streamer' folder (staying in /home) I can just add a '*' at the end.

  du -h -s *

This gives:

12K     nics
1.1M    openstack
30G     streamer

Lets use this command on the /home/streamer/bin folder:

  cd /home/streamer/bin
  ll
  drwxr-xr-x. 2 root root        128 Aug  9 11:15 1080i
  -rw-r--r--. 1 root root 2636986136 Aug  9 11:21 avp.ts
  drwxr-xr-x. 2 root root         44 Aug  9 11:15 bbc
  drwxr-xr-x. 2 root root        192 Aug  9 11:18 CRa
  drwxr-xr-x. 2 root root         67 Aug  9 11:18 example
  drwxr-xr-x. 2 root root         63 Aug  9 11:18 itv
  -rw-r--r--. 1 root root         87 Aug  9 11:21 kill_all_mpeg2ts_broadcaster.sh
  -rwxr-xr-x. 1 root root     825163 Aug  9 11:21 libos.so
  drwxr-xr-x. 2 root root       4096 Sep 20 15:53 MK.ts
  -rwxr-xr-x. 1 root root      17387 Aug  9 11:21 mpeg2ts_broadcaster
  -rw-r--r--. 1 root root  813563044 Aug  9 11:21 Oro4KTest.ts
  drwxr-xr-x. 2 root root        105 Aug  9 11:19 virgin
  drwxr-xr-x. 2 root root       4096 Aug  9 11:20 zb

Now I use the following:

  du -h -s 

The output is:

  30G     .

However, if I use the same with a star * at the end:

du -h -s *

1.3G    1080i
2.5G    avp.ts
15G     bbc
502M    CRa
832K    example
576M    itv
4.0K    kill_all_mpeg2ts_broadcaster.sh
808K    libos.so
2.4G    MK.ts
20K     mpeg2ts_broadcaster
776M    Oro4KTest.ts
554M    virgin
7.5G    zb


I could have done the same thing from say root by issuing the command:

du -h -s /home/streamer/bin/*

1.3G    /home/streamer/bin/1080i
2.5G    /home/streamer/bin/avp.ts
15G     /home/streamer/bin/bbc
502M    /home/streamer/bin/CRa
832K    /home/streamer/bin/example
576M    /home/streamer/bin/itv
4.0K    /home/streamer/bin/kill_all_mpeg2ts_broadcaster.sh
808K    /home/streamer/bin/libos.so
2.4G    /home/streamer/bin/MK.ts
20K     /home/streamer/bin/mpeg2ts_broadcaster
776M    /home/streamer/bin/Oro4KTest.ts
554M    /home/streamer/bin/virgin
7.5G    /home/streamer/bin/zb




du - Finding the size of a directory


$ du

Typing the above at the prompt gives you a list of directories that exist in the current directory along with their sizes. The last line of the output gives you the total size of the current directory including its subdirectories. The size given includes the sizes of the files and the directories that exist in the current directory as well as all of its subdirectories. Note that by default the sizes given are in kilobytes.

$ du /home/david

The above command would give you the directory size of the directory /home/david

$ du -h

This command gives you a better output than the default one. The option '-h' stands for human readable format. So the sizes of the files / directories are this time suffixed with a 'k' if its kilobytes and 'M' if its Megabytes and 'G' if its Gigabytes.

$ du -ah

This command would display in its output, not only the directories but also all the files that are present in the current directory. Note that 'du' always counts all files and directories while giving the final size in the last line. But the '-a' displays the filenames along with the directory names in the output. '-h' is once again human readable format.

$ du -c du -ch will display the sizes in human readable form

This gives you a grand total as the last line of the output. So if your directory occupies 30MB the last 2 lines of the output would be

30M .
30M total

The first line would be the default last line of the 'du' output indicating the total size of the directory and another line displaying the same size, followed by the string 'total'. This is helpful in case you this command along with the grep command to only display the final total size of a directory as shown below.

$ du -ch | grep total This would have only one line in its output that displays the total size of the current directory including all the subdirectories.

Note : In case you are not familiar with pipes (which makes the above command possible) refer to Article No. 24 . Also grep is one of the most important commands in Unix. Refer to Article No. 25 to know more about grep.

$ du -s
This displays a summary of the directory size. It is the simplest way to know the total size of the current directory.

$ du -S
This would display the size of the current directory excluding the size of the subdirectories that exist within that directory. So it basically shows you the total size of all the files that exist in the current directory.

$ du –exculde=mp3
The above command would display the size of the current directory along with all its subdirectories, but it would exclude all the files having the given pattern present in their filenames. Thus in the above case if there happens to be any mp3 files within the current directory or any of its subdirectories, their size would not be included while calculating the total directory size.

'df' - finding the disk free space / disk usage

$ df
Typing the above, outputs a table consisting of 6 columns. All the columns are very easy to understand. Remember that the 'Size', 'Used' and 'Avail' columns use kilobytes as the unit. The 'Use%' column shows the usage as a percentage which is also very useful.

$ df -h
Displays the same output as the previous command but the '-h' indicates human readable format. Hence instead of kilobytes as the unit the output would have 'M' for Megabytes and 'G' for Gigabytes.

Most of the users don't use the other parameters that can be passed to 'df'. So I shall not be discussing them.

I shall in turn show you an example that I use on my machine. I have actually stored this as a script named 'usage' since I use it often.

Example :

I have my Linux installed on /dev/hda1 and I have mounted my Windows partitions as well (by default every time Linux boots). So 'df' by default shows me the disk usage of my Linux as well as Windows partitions. And I am only interested in the disk usage of the Linux partitions. This is what I use :

$ df -h | grep /dev/hda1 | cut -c 41-43

This command displays the following on my machine

45%
Basically this command makes 'df' display the disk usages of all the partitions and then extracts the lines with /dev/hda1 since I am only interested in that. Then it cuts the characters from the 41st to the 43rd column since they are the columns that display the usage in % , which is what I want.

Note : In case you are not familiar with pipes (which is used in the above command) then refer to Article No. 24 . 'cut' is another tool available in Unix. The above usage of cut gets the the characters that are present in the specified columns. If you are interested in knowing how to mount you Windows partitions under Linux, please refer to Article No. 3 .

There are a few more options that can be used with 'du' and 'df' . You could find them in the man pages.

how_to_find_the_size_of_folders.txt · Last modified: 2023/11/08 23:38 by walkeradmin