Command to Display Directory Sizes in Linux

Written by A.Jesin Monday, 25 October 2010 09:38

Even though Linux has developed so much in terms of GUI which has become comparable to the latest Aero theme from Microsoft’s stables (especially in KDE environment) the command line becomes an inevitable tool for performing advanced administrative tasks like setting user quotas, compiling from the source to name it a few. But unlike the command line in windows, Linux command line works differently. A good example is showing the sizes of directories while listing the contents. The “ls” command in Linux shows a list of files and directories in the present directory but doesn’t show the sizes of directories and the file sizes are shown in bytes which makes it difficult to read and understand. So the correct command for this purpose is “du

Display sizes of all files and directories

Just entering the du command will show the entire list of files and directories in the current directory with their sizes in KB, the last line shows the size of the entire directory without any units. Not so useful isn’t it ? Lets move on to something more useful.

Display sizes in human readable format

So earlier when the sizes of the directories were displayed it was difficult to interpret whether the displayed size was in KB or MB and several digits were displayed for files of huge sizes which would’ve left you confused. A parameter -h will display sizes in KB, MB or GB according to the file size which makes it human readable.

Look at the screenshot below for du -h in action

linux-du-command-h-parameter

The du -h command displays the file and directory sizes in units

Display the size in one line

In the previous examples you would’ve noted that the entire list of files and directories in the current location are displayed. What if you only need the size of the current directory you are in ? Use the -s parameter and a single line showing the size is displayed. This parameter can be combined with -h to display the size in human readable format.

Type du -sh to display the size in one line in human readable format

linux du command sh parameter

Use the -s parameter to display the size in one line

Display the size of the specified directory

Till now we have been seeing how to display the size of the present working directory, now we’ll see how to specify the particular directory for which the size should be displayed. The syntax is du [parameters] [path/to/directory]

The screenshot below shows the command du -sh /etc in action

linux du command sh parameter directory specified

The size of the specified directory is displayed in human readable format

You aren’t limited to only these options just type du –help to get a complete list of parameters for the du command.

Also read:

Enjoyed reading this article, subscribe to stay up-to-date with more such articles

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>