Some times we require to see size of a directory or it’s sub directories or hidden directories so that we can come to know how much size they are occupying on our disk. In this post we will see how to use du to get the sizes

Example1: Find out the size of a folder.

Note: Do not execute below command on a folder which have many files and folder like / folder because it will list all the folders and sub folders with in it, which may go bit deep.

	du /folder-name

Example1:

	du /etc/logrotate.d/

Output:

	surendra@linuxnix:/etc/logrotate.d$ du /etc/logrotate.d/

	76    /etc/logrotate.d/

Example2: Ok, the above command just show size in bytes, how about showing them in KB, MB or GB etc? We can accomplish that with -h option which is for human readable.

	du -h /foldername

Example:

	surendra@linuxnix:/usr/share$ du -h /usr/share/apache2/

	16K    /usr/share/apache2/default-site

	8.0K    /usr/share/apache2/build

	268K    /usr/share/apache2/icons/small

	1.4M    /usr/share/apache2/icons

	16K    /usr/share/apache2/error/include

	208K    /usr/share/apache2/error

	1.7M    /usr/share/apache2/


Note: We can use -k option to show file/folder size in KB and -m for MB.

Example3: If you observe above commands will give you the sizes of each folder and sub folders in that given directory. In order to get summerized report we can use -s options

	du -hs /foldername

Example:

	surendra@linuxnix:/usr/share$ du -hs /usr/share/apache2/

	1.7M    /usr/share/apache2/

Example4: How to see only just one level below sub directories or folder

	du -hs /foldername/*

Example:

	surendra@linuxnix:/usr/share$ du -hs /usr/share/apache2/*

	16K    /usr/share/apache2/apache2-maintscript-helper

	4.0K    /usr/share/apache2/ask-for-passphrase

	8.0K    /usr/share/apache2/build

	16K    /usr/share/apache2/default-site

	208K    /usr/share/apache2/error

	1.4M    /usr/share/apache2/icons

If you observe example4 and example3 outputs you will come to know files size in subdirectories are clubbed in to it’s parent directory.

Example5: I have hidden directories in my folder, how can I list only hidden folders size

	du -hs .[!.]*

Let me explain what .[!.]* is all about. In order to understand this you should know . indicates present directory and .. indicates parent directory. what .[!.] indicates is my folder starts with . and it will not contain . again after the first dot. Which means we are effectively eliminating .. which is my parent directory. and .[!.]* is all the hidden files/folder in that given directory.

Example6: Above example lists only hidden files and directories, how about list all folders/files in that directory.

	du -hs .[!.]* *

Example output:

	8.0K    .thumbnails

	1.4M    .thunderbird

	8.0K    .vim

	28K    .viminfo

	522M    .wine

	4.0K    .Xauthority

	4.0K    .xsession-errors

	4.0K    .xsession-errors.old

	2.0G    10.04_clean_image_for_12.04_16072015

	4.0K    20150430-assets_rcvr.csv

	4.0K    2.4.0-A2 load tests

Example7: Some times we want to execute this command in many folders which is tedious job, how about making this as an alias so that the typing takes less time.

	alias sm="du -hs * | grep -E ^[0-9]+M"

	alias sg="du -hs * | grep -E ^[0-9]+G"

The first command will list all the folders which are more than or equal to 1M and less than 1000M and second command will show all the files/folder which are more than or equal to 1G

	Example:

	surendra@linuxnix:~$ sm

	860M    code

	27M    Music

	26M    Pictures

	579M    ubuntu-14.04.2-server-i386.iso surendra@linuxnix:~$ sg

	16G    Desktop

In our next post we will see how to free up disk by removing unwanted files.

 

The following two tabs change content below.
Mr Surendra Anne is from Vijayawada, Andhra Pradesh, India. He is a Linux/Open source supporter who believes in Hard work, A down to earth person, Likes to share knowledge with others, Loves dogs, Likes photography. He works as Devops Engineer with Taggle systems, an IOT automatic water metering company, Sydney . You can contact him at surendra (@) linuxnix dot com.