Using the ‘exclude’ option with ‘tar’

I’m always confused with the use of ‘exclude’ options with ‘tar’. Here I’m sharing a few tips I found online, just in case it might help you. The meanings of –exclude and –exclude-from are always confusing:

  • Use –exclude when files to be excluded are given as a pattern on the command line.
  • Use –exclude-from to introduce the name of a file which contains a list of patterns, one per line; each of these patterns can exclude zero, one, or many files.

When using –exclude=pattern, be sure to quote the pattern parameter, so GNU tar sees wildcard characters like ‘*’.

For example, write:

$ tar -c -f <em>archive.tar</em> --exclude '*.o' <em>directory</em>

tar does not act on a path name explicitly listed on the command line if one of its file name components is excluded. If files that end with ‘*.o’ are excluded when creating an archive, but explicitly name the file ‘dir.o/foo’ after all the options have been listed, ‘dir.o/foo’ will be excluded from the archive.
Only shell syntax, or globbing will work with exclude options in tar. The command might fail if regexp syntax is used to describe files to be excluded in the command.

Reference:

Leave a Reply