Printing Disk Usage with du

The du command prints the size of each file and directory that is inside the current directory. Its most basic usage is as easy as it gets:

That outputs a long list of directories and how much space their files take up. You can modify that with the -a parameter, which instructs du to print the size of individual files as well as directories. Another useful parameter is -h, which makes du use human-readable sizes like 18M (18MB) rather than 17532 (the same number in bytes, unrounded). The final useful basic option is -c, which prints the total size of files.

So, using du we can get a printout of the size of each file in our home directory, in human-readable format, and with a summary at the end, like this:

du -ahc /home/paul

Two advanced parameters deal with filenames you want excluded from your count. The first is -exclude, which allows you to specify a pattern that should be used to exclude files. This pattern is a standard shell file matching pattern as opposed to a regular expression, which means you can use ? to match a single character or * to match 0 or many characters. You can specify multiple --exclude parameters to exclude several patterns. For example:

du —exclude="*.xml" —exclude="*.xsl"

Of course, typing numerous --exclude parameters repeatedly is a waste of time, so you can use -x to specify a file that has the list of patterns you want excluded. The file should look like this:

That is, each pattern you want excluded should be on a line by itself. If that file were called xml_exclude.txt, we could use it in place of the previous example like this:

du -X xml exclude.txt

You can make your exclusion file as long as you need, or you can just specify multiple -x parameters.

Running du in a directory where several files are hard-linked to the same inode counts the size of the file only once. If you want to count each hard link separately for some reason, use the -l parameter (lowercase L).

Was this article helpful?

0 0

Post a comment