The --exclude and -exclude-from options cause du to exclude files from the disk usage calculation.
- The -exclude option can be used with wildcards or a single filename:
$ du --exclude "WILDCARD" DIRECTORY
Consider this example:
# Excludes all .txt files from calculation
$ du --exclude "*.txt" *
# Exclude temp.txt from calculation
$ du --exclude "temp.txt" *
- The --exclude option will exclude one file or files that match a pattern. The -exclude-from option allows more files or patterns to be excluded. Each filename or pattern must be on a single line.
$ ls *.txt >EXCLUDE.txt
$ ls *.odt >>EXCLUDE.txt
# EXCLUDE.txt contains a list of all .txt and .odt files.
$ du --exclude-from EXCLUDE.txt DIRECTORY
The -max-depth option restricts how many subdirectories du will examine. A depth of 1 calculates disk usage in the current directory. A depth of 2 calculates usage in the current directory and the next subdirectory:
$ du --max-depth 2 DIRECTORY
The -x option limits du to a single filesystem. The default behavior for du is to follow links and mount points.
The du command requires read permission for all files, and read and execute for all directories. The du command will throw an error if the user running it does not have proper permissions.