[NTLUG:Discuss] tar --exclude doesn't

kbrannen@gte.net kbrannen at gte.net
Wed Oct 1 02:11:51 CDT 2003


Courtney Grimland wrote:
> Using GNU tar version 1.13...
> 
> I am trying to do a system backup, and want to exclude the contents
> of a few dirs, like /home/* and /usr/src/*.  I still want the /home
> and /usr/src directories in the archive, but not their contents.

Then you'll need a list of --exclude options.

> 
> For the life of me, I cannot get the --exclude option of tar to work
> as advertised.

 From the man page:

  --exclude=FILE
        exclude file FILE

Please note, a single file, no globbing promised.

[globbing, a.k.a wildcard expansion; in general unix utils don't do this 
themselves, the shell does it for you and passes the now multiple args to the 
program (or the wildcarded string if there is no expansion possible)]

...
> For the record, I've tried all of the following:
> 
> tar -cv --exclude="one/*" -f test.tar ./
> tar -cv --exclude='one/*' -f test.tar ./
> tar -cv --exclude one/* -f test.tar ./
> tar -cv --exclude "one/*" -f test.tar ./
> tar -cv --exclude 'one/*' -f test.tar ./
> 
> I've also tried moving the options around (the -f test.tar before the
> --exclude, etc), as well as the --exclude-from=FILE option,
> specifying a file with the same globbing patterns.  All with the same
> results.  I know I've done this before, but apparently I'm missing
> something now.

You'll either need to create the command on the fly (because there is no 
globbing here), or use the -X option (or --exclude-from as you mentioned), as 
in (shell script examples):

# on the fly
x=""
for f in /usr/src/* /home/*; do x="$x --exclude $f"; done
`tar -cv $x -f test.tar .`

# using -X
ls /usr/src/* /home/* > /tmp/excludes
tar -cv -X /tmp/excludes -f test.tar .

Both snippets are untested, use at you're own risk, blah, blah, blah; but they 
look good to me. :-)

HTH,
Kevin




More information about the Discuss mailing list