How to exclude a set of files from disk space usage calculation using du

We have already discussed the most common options used with du - a tool to check/calculate file space usage. In its most basic form, du tells you the size of all the files in a given directory. Using -s option it sums up the total size of a directory, but what if we don’t want to include a set of files while calculating the utilized space. Using the --exclude and --exclude-from option, we can easily mention the files to be excluded.

Lets say we want to exclude all the *.png files while calculating the file space usage.

[shredder12]$ du -h --exclude=*.png
399M

[shredder12]$ du -sh
513M

[shredder12]$ du -ch *.png

115M total

The --exclude option takes a wildcard as input.

The -h option is to show the size in a human readable format rather than a huge figure in bytes.

The -c option is used to add an extra line showing the grand total of all the files.

If the files to be excluded don’t match any pattern then one can create a list of all the filenames and pass the file containing that list as argument to du’s --exclude-from option.

Say we have the list in the file named “list”.

[shredder12]$ du -h --exclude-from=list

Post new comment

  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <img> <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <h1> <h2> <h3> <h4> <h5> <h6> <p> <br>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Image links with 'rel="lightbox"' in the <a> tag will appear in a Lightbox when clicked on.

More information about formatting options

Type the characters you see in this picture. (verify using audio)
Type the characters you see in the picture above; if you can't read them, submit the form and a new image will be generated. Not case sensitive.