20.8 Telling tar Which Files to Exclude or Include[This article was written for SunOS. Many versions of tar don't have some or all of these features. Some do it in a different way. Check your tar manual page, or use the GNU tar (19.6 ) that we provide on the disc. -JP]
On some systems,
make
(28.13
)
creates filenames starting with a
comma ( I often don't want to save these files on my backups. There may be some binary files that I don't want to archive, but don't want to delete either. A solution is to use the X flag to tar (20.1 ) . [Check your tar manual page for the F and FF options, too. -JIK ] This flag specifies that the matching argument to tar is the name of a file that lists files to exclude from the archive. Here is an example: % In this example, find (17.1 ) lists all files in the directories, but does not print the directory names explicitly. If you have a directory name in an excluded list, it will also exclude all the files inside the directory. egrep (27.5 ) is then used as a filter to exclude certain files from the archive. Here, egrep is given several regular expressions to match certain files. This expression seems complex but is simple once you understand a few special characters:
A breakdown of the patterns and examples of the files that match these patterns is given here:
Instead of specifying which files are to be excluded, you can specify which
files to archive using the
-I
option.
As with the exclude flag, specifying a directory tells
tar
to include (or exclude) the entire directory.
You should also note that the syntax of the
-I
option is different from the typical
tar
flag.
The next example archives all C files and makefiles.
It uses egrep
's % I suggest using find to create the include or exclude file. You can edit it afterward, if you wish. One caution: extra spaces at the end of any line will cause that file to be ignored. One way to debug the output of the find command is to use /dev/null (13.14 ) as the output file: % 20.8.1 Including Other DirectoriesThere are times when you want to make an archive of several directories. You may want to archive a source directory and another directory like /usr/local . The natural, but wrong, way to do this is to use the command: %
The proper way to handle the incorrect example above is to use the -C flag: % This will archive /usr/local/... as local/... . Article 20.10 has more information. 20.8.2 Type Pathnames ExactlyFor the above options to work when you extract files from an archive, the pathname given in the include or exclude file must exactly match the pathname on the tape. Here's a sample run. I'm extracting from a file named appe.tar . Of course, this example applies to tapes, too: % Next, I create an exclude file, named exclude , that contains the lines: code/appendix/zcard.c code/appendix/zcard.icon Now, I run the following tar command: % 20.8.3 Exclude the Archive File!If you're archiving the current directory ( % In that example,
we used
Or, instead of adding the archive and exclude file's pathnames to the exclude file, you can move those two files somewhere out of the directory tree that tar will read. - | |||||||||||||||||||||||||||
|