Often you may want to get folder size in Linux, especially if your disk is becoming full. But when we list a folder or directory using ls command, its size is always displayed as 4kb. This is not the size of its contents but only its metadata. In this article, we will learn how to get size of directory in Linux using du command. It is useful in finding out the biggest directories on your disk so that you know which folders are occupying most space on your disk. You can use these commands on almost every Linux distribution.
How to Get Size of Directory in Linux
For our purpose, we will use du command. du command returns the space of specified file or directory. If you input a directory path for du command, it will summarize the total space occupied by its subdirectories & files. If you do not provide any path, du command will give you the disk space of your current working directory. It also supports numerous options to help you get the information you need. Here is the syntax for du command.
du [options] <path_to_file_or_directory>
For example, if you want to find size of folder /home/ubuntu, use the following command.
$ du /home/ubuntu 24123 /home/ubuntu/db 4086 /home/ubuntu/empty 4086 /home/ubuntu/games 1324235 /home/ubuntu/lib 4086 /home/ubuntu/local ... 2067884 total
You will see that it not only displays total size of folder but also lists size of each file and subfolder in it. It will display total space of directory only in the last line of output. This can be quite big depending on the number subfolders and files in the specific folder. If you want to see the size of only the specified directory but not its subdirectories and files, use -s option.
$ du -s /home/ubuntu 2067884 /home/ubuntu
Also, by default, du command will display all sizes in bytes. The above output means that /home/ubuntu occupies 2067884 bytes of data. If you want du command to display information in human readable formats such as Mb, Gb, etc. use -h option.
$ du -sh /home/ubuntu 2.0G /home/ubuntu
When you run du command on a specific directory, it will go through all its files and subfolders and list their size. If you want to view the disk space of only immediate subfolders and not their subfolders, that is, first-level subdirectories, you can control it using –max-depth option. In the following example, we want to view information of only immediate subfolders, that is, level 1, and not any of their subfolders.
$ du --max-depth=1 /home/ubuntu 8 /home/ubuntu/.gnupg 98684 /home/ubuntu/setup_files 194168 /home/ubuntu/.dropbox-dist 1276816 /home/ubuntu/Projects 3656 /home/ubuntu/mysql-master 9592 /home/ubuntu/.cache 25000 /home/ubuntu/.dropbox 158404 /home/ubuntu/Dropbox 8 /home/ubuntu/.vim 49176 /home/ubuntu/.local 2068116 /home/ubuntu
You can also use du command to calculate the apparent size of file or folder. Apparent size is the size of actual data present in the file. For this purpose, you can use –apparent-size option.
$ du --max-depth=1 --apparent-size /home/ubuntu 8 /home/ubuntu/.gnupg 98680 /home/ubuntu/setup_files 193924 /home/ubuntu/.dropbox-dist 1226088 /home/ubuntu/Projects 2639 /home/ubuntu/mysql-master 9508 /home/ubuntu/.cache 34514 /home/ubuntu/.dropbox 158342 /home/ubuntu/Dropbox 38595 /home/ubuntu/.local 1980319 /home/ubuntu
You will see that there is a difference in size information with and without using –apparent-size option. During file transfer, apparent size is used to determine the amount of data to be transferred.
Now that you know how to get directory size in Linux, you can use du command to list the largest folders on your disk. Here is the command to list top 5 largest folders in Linux.
$ sudo du -h /home/ubuntu | sort -rh | head -5 2.0G /home/ubuntu 1.3G /home/ubuntu//category 1.3G /home/ubuntu/Projects 764M /home/ubuntu/data 397M /home/ubuntu/products
In the above command, we pipe the output of du command to sort command to sort directories by their size, then pipe it to head command that will print only top 5 largest directories.
In this article, we have learnt how to get size of directory in Linux. You can customize it as per your requirement.
Also read:
How to Calculate Time Difference in Python
How to Measure Time Taken by Python Program
How to Encrypt & Decrypt Files in Python
How to Recursively Download Files & Folders in Wget
Bash Script to Run Commands on Remote Server
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.