how to find the size of folders and their sub-folders in linux?

No matter what operating system you use, you routinely would want to know the size of folders, sub-folders and files on your file system. Keeping your file system organized is just good practice and a tool to proper maintenance of your system. And Linux is no exception.

First, we will see how you can easily summarize the size of folders and sub-folders and files in Linux from the command line. There are primarily two commands that lets you do this: ls and du. We will also look at some other utilities that let you do this graphically as well.

Using ls command

ls is probably one of the widely used command that everybody knows. It is used to list the contents of a folder or directory. The ls command also can print many attributes and metadata  information about the files it is listing.

Let’s look at some of the command line options that are available with ls.

-l : uses the long listing format that displays several of the file metadata
-h or –human-readable : prints the file sizes in human readable formats, such as KB, MB or GB etc
-R or –recursive : It displays the sub-folders recursively.

Using the above options, you can easily list the sizes of the files inside a folder

bash$ ls -lh /path/to/folder/

In order to list files inside the sub-directories inside the folder, you can use the -R option

bash$ ls -lhR /path/to/folder

Although efficient in displaying the file sizes, it is not as good if you want the size of the “entire” folder. In Linux, the folders are nothing but files with some special properties. This means that you are likely to see folders listed with a size of 4k rather than the sum of all its files and sub-folders.

If you want to see the “true” size of the folder, you will need to use the du command.

Using du command

du (short for Disk Usage) is a Linux command that allows you estimate disk space usage. There are several command line options with du.

-h or –human-readable: This prints out the file sizes in human readable form such as KB, MB or GB
-d or –max-depth: Specify the maximum depth (or level) of the files below the folder that you want listed
-s or –summarize: display only the total for each argument

The du command with out any command line arguments will recurse into the current folder and print out the file size of each and every file and sub-folder. You can use the -h option to print the size in a human readable format.

bash$ du -h

To print out the total for a directory use the -s option. This will print the total or cumulative size of the folder including its folders and sub-folders. Note that -s option implies –max-depth=0.

bash$ du -hs /path/to/folder/

The –max-depth is a very useful option to limit the number of files you want listed. If you want to list the folder as well as the first level of sub-folders then you can use a depth of 1. Do not use -s option when you use -d or –max-depth

bash$ du -h -d 1 /path/to/folder/

size of folders using du

There are several other utilities that provide the same functionality as du but with “better” features. Some of them have better graphical user interface that allow you to use them interactively. These utilities may not be available to you right out of the box on some distros, so you might have to install them separately.

Using tree command

The tree command prints out the directory structure in a hierarchical format or as a tree. You can use the -s option to print out the size of the files and folders as well. The -h option is again the human readable format for sizes.

bash$ tree /path/to/folder -s -h

Another option is the ncdu (NCurses Disk Usage) utility, which is a curses based version of the du command. It gives you a curses based graphical interface where you can select folders and view contents and sizes.

treesize is another utility that can be used for viewing the folder sizes. It is primarily a disk analyzer tool with a good user interface.