To avoid operating on files whose names match a particular pattern, use the --exclude or --exclude-from options.
The --exclude=pattern option prevents any file or member whose name matches the shell wildcard (pattern) from being operated on. For example, to create an archive with all the contents of the directory src except for files whose names end in .o, use the command ‘tar -cf src.tar --exclude='*.o' src’.
You may give multiple --exclude options.
Use the --exclude-from option to read a list of patterns, one per line, from file; tar will ignore files matching those patterns. Thus if tar is called as ‘tar -c -X foo .’ and the file foo contains a single line *.o, no files whose names end in .o will be added to the archive.
Notice, that lines from file are read verbatim. One of the frequent errors is leaving some extra whitespace after a file name, which is difficult to catch using text editors.
As of version 1.26, the following files are excluded:
When creating an archive, the --exclude-caches option family causes tar to exclude all directories that contain a cache directory tag. A cache directory tag is a short file with the well-known name CACHEDIR.TAG and having a standard header specified in http://www.brynosaurus.com/cachedir/spec.html. Various applications write cache directory tags into directories they use to hold regenerable, non-precious data, so that such data can be more easily excluded from backups.
There are three ‘exclude-caches’ options, each providing a different exclusion semantics:
Another option family, --exclude-tag, provides a generalization of this concept. It takes a single argument, a file name to look for. Any directory that contains this file will be excluded from the dump. Similarly to ‘exclude-caches’, there are three options in this option family:
Multiple --exclude-tag* options can be given.
For example, given this directory:
$ find dir dir dir/blues dir/jazz dir/folk dir/folk/tagfile dir/folk/sanjuan dir/folk/trote
The --exclude-tag will produce the following:
$ tar -cf archive.tar --exclude-tag=tagfile -v dir dir/ dir/blues dir/jazz dir/folk/ tar: dir/folk/: contains a cache directory tag tagfile; contents not dumped dir/folk/tagfile
Both the dir/folk directory and its tagfile are preserved in the archive, however the rest of files in this directory are not.
Now, using the --exclude-tag-under option will exclude tagfile from the dump, while still preserving the directory itself, as shown in this example:
$ tar -cf archive.tar --exclude-tag-under=tagfile -v dir dir/ dir/blues dir/jazz dir/folk/ ./tar: dir/folk/: contains a cache directory tag tagfile; contents not dumped
Finally, using --exclude-tag-all omits the dir/folk directory entirely:
$ tar -cf archive.tar --exclude-tag-all=tagfile -v dir dir/ dir/blues dir/jazz ./tar: dir/folk/: contains a cache directory tag tagfile; directory not dumped