Re: I can't get tar to exclude files!!
Admaster Communications <email@example.com> writes:
> I'm running Debian GNU/Linux 1.3 and I've tried many permutations of
> tar -zcvf test.tar * --exclude=leave_me_out.txt
> The file I ask to be excluded is reliably INcluded!! What is the correct
The problem is that --exclude applies only to files tar finds by
descending directories, not to files mentioned explicitly on the
command line. When you use *, that's the same as typing every file
name explicitly (since the shell does not pass tar the *, but instead
passes it each file's name - try 'echo *' some time to see what the
shell does); therefore, --exclude doesn't apply.
The following appears to do what you want - be aware, however, that
this tar archive contains ".", so when extracting it it will change
the permissions of the current directory to the permissions of the
directory when the tar archive was made:
tar -czvf ../test.tar.gz --exclude='*exclude*pattern*' .
(Note the ' marks and the final period - also note that test.tar.gz
has to be in a different directory, or it will try to include itself -
Note that it's usually bad form to create a tar archive of files
without a directory. That is, if the directory
/home/martind/work/vander contains the files I want to make a tar
archive out of, it's better to do:
cd /home/martind/work/; tar czf vander.tar.gz vander
than to do:
cd /home/martind/work/vander; tar czf ../vander.tar.gz *
The first method will mean that when one extracts the archive, all the
extracted files go into a vander/ subdir. The second method means
that all the newly extracted files go into the current directory,
where there may already be much stuff. Note that if you use the first
method, a --exclude will work as you might expect it should.
If you really must have a tar archive with files in the current
directory, and don't want to have "." in the archive, then try: (this
should all be one long line)
find * -prune \! -name '*exclude*pattern1*' -print |
xargs tar czvf testtar.tar.gz --exclude='*exclude*pattern2*'
The difference between '*exclude*pattern1*' and '*exclude*pattern2*'
is that the first pattern excludes files in the current directory and
the second excludes files in subdirs. of the current directory. For
example, if I had the following files on my system:
and then I did:
find * -prune \! -name '*frog*' -print | \
xargs tar czvf mytar.tar.gz --exclude='*bar*'
I would then get (as the output of 'tar tzf mytar.tar.gz'):
Note that the '*frog*' excluded the 'frog' file in the top directory
and the 'froglegs' directory. Also note that the '*bar*' didn't
exclude the bar and tbark in the top directory (the way the long
command is written causes tar to be called with the arguments 'bar',
'tbark', and 'sounds' - these are the output of the find command), but
did exclude the 'dogbark.au' in the sounds directory - this is because
tar was handed 'sounds' as an argument, and descended into the
To UNSUBSCRIBE, email to firstname.lastname@example.org
with a subject of "unsubscribe". Trouble? Contact email@example.com