Monday, December 29, 2008

Linux CLI for fun: du

One of the best things about Linux is the power of the Command Line Interface (CLI). Many new Linux users making the transition from either Windows or Mac to Linux may have difficulty getting away from the GUI method of doing things and into the CLI but there are times when the CLI is the only way to perform a particular task or just the fastest way. The only way to get proficient in the CLI is to open up a terminal and get some practice.

A good example of a CLI command that is very useful is the du command. I am always interested in how much disk space a particular file or folder is taking on my hard drive. This is especially important to know when writing data to removable media that is limited in size such as a CD or DVD. There is a GUI method which is to select the view that shows the details of files and folders but the only the size of the files is show and not the folders in a particular directory. What if I want to know the size of a bunch of folders in a directory and what if I want that information in a text file for later use.

The du command is a great tool for doing this and only takes a few seconds to use. Just cd to the directory that you want to get information on and type du. Actually it is not that easy as the display you get is the size of each and every file in the directory and every sub directory. Since this very is not very effective, lets break this down a little more.

First you can add a file or directory name to the du command to only find out the size a that particular file or directory. For example du thefile will give me the size of the file named thefile. The size is listed in KB but since there are no tags or legend this is not readily apparent. To fix this add the -h argument to the command so the line would look like du -h thefile.

Next when looking at folders, if you only want to so the total size of the parent folder and not have the sub directories listed, add the -s argument to the command so the line would look like du -hs thedirectory.

There are many more arguments for the du command that can be found in the man pages but -h and -s are the most commonly used. Listed below are some additional uses for the command

du -sh ./ - shows the size of the current directory in human readable format without listing any sub directories
du -sh * > outputfile - shows the size of the files and directories in the current directory in human readable format without listing any sub directories directory and sends the output to a file.
du -sh * | grep something - shows the size of the files and directories in the current directory in human readable format without listing any sub directories directory and only lists files or directories with something in the name

5 comments:

Anonymous said...

the CL is useless and it should be replaced with a nice gui!!! I hate it I cant stand it.

I hope I never see a CL article again!!!

Anonymous said...

As the author said you can open a gui file manager and use it to get info. The command line is faster and more powerful though. Instead of clicking on the menu, finding the application, opening it and right-clicking for more details, you can open a terminal and type the command you want and hit enter.

Use what works for you.

Minjie Zha said...

I like the CLI very much. It is faster and provides more freedom to do what I want than the GUI.
This is a good introduction for command du. I use "du -sh dir" everyday. :)

Anonymous said...

Try

du -h --max-depth=1

Work wonders for me. ;-)

Anonymous said...

Any one who claims the cli is useless is useless themselves...