[This article was written for SunOS.
Many versions of tar don't have some or all of
Some do it in a different way.
Check your tar manual page, or use the
that we provide
on the disc. -JP]
On some systems,
creates filenames starting with a
to keep track of dependencies.
Various editors create backup files whose names end with a
percent sign (
I often keep the original copy of a program with the .orig
extension and old versions with a .old
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
[Check your tar
manual page for the F
This flag specifies that the matching argument to
is the name of a file that lists files to exclude from the archive.
Here is an example:
find project ! -type d -print | \
egrep '/,|%$|~$|\.old$|SCCS|/core$|\.o$|\.orig$' > Exclude
tar cvfX project.tar Exclude project
In this example,
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.
is then used as a filter to exclude certain files from the archive.
is given several regular expressions to match certain files.
This expression seems complex but is simple once you understand
a few special characters:
The slash is not a special character.
However, since no filename
can contain a slash, it matches the beginning of a filename,
as output by the
The vertical bar separates each regular expression.
The dollar sign is one of the two regular expression
and specifies the end of the line, or filename in this case.
The other anchor, which specifies the beginning of the line, is
But because we are matching filenames output by
the only filenames that can match
are those in the top directory.
Normally the dot matches any character in a regular
expression. Here, we want to match the actual
which is why the backslash is used to
the normal meaning.
A breakdown of the patterns and examples of the files
that match these patterns is given here:
||starting with ,
||ending with %
||ending with ~
||ending with .old
||Source Code Control System (20.13
||with name of core
||core dump (52.9
||ending with .o
||ending with .orig
Instead of specifying which files are to be excluded, you can specify which
files to archive using the
As with the exclude flag, specifying a directory tells
to include (or exclude) the entire directory.
You should also note that the syntax of the
option is different from the typical
The next example archives all C files and makefiles.
It uses egrep
grouping operators to make
anchor character apply to all patterns inside the
find project -type f -print | \
egrep '(\.[ch]|[Mm]akefile)$' > Include
tar cvf project.tar -I Include
I suggest using
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
command is to use
as the output file:
tar cvfX /dev/null Exclude project
There are times when you want to make an archive of several directories.
You may want to archive a source directory and
another directory like
The natural, but wrong, way to do this is to use the command:
tar cvf /dev/rmt8 project /usr/local
When using tar
, you must never specify a directory name starting with a
This will cause problems when you restore a directory,
as you will see later (20.10
The proper way to handle the incorrect example above is to use the
tar cvf /dev/rmt8 project -C /usr local
This will archive
has more information.
For 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:
tar tf appe.tar
Next, I create an exclude file, named exclude
, that contains the
Now, I run the following tar
tar xvfX appe.tar exclude
x appe, 6421 bytes, 13 tape blocks
x code/appendix/font_styles.c, 3457 bytes, 7 tape blocks
x code/appendix/xmemo.c, 10920 bytes, 22 tape blocks
x code/appendix/xshowbitmap.c, 20906 bytes, 41 tape blocks
If you're archiving the current directory (
) instead of
starting at a subdirectory, remember to start with
two pathnames in the Exclude
the archive that tar
creates and the Exclude
That keeps tar
from trying to archive its own output!
cat > Exclude
find . -type f -print | \
egrep '/,|%$|~$|\.old$|SCCS|/core$|\.o$|\.orig$' >>Exclude
tar cvfX somedir.tar Exclude .
In that example,
to create the file quickly; you could use a text
Notice that the pathnames in the Exclude
file start with
that's what the tar
command expects when you tell it to archive
the current directory (
The long find
command line uses the
to add other pathnames to the end of the Exclude
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