tar exclude single files/directories, not patterns

tar
Udo G picture Udo G · May 9, 2012 · Viewed 27.8k times · Source

I'm using tar to make daily backups of a server and want to avoid backup of /proc and /sys system directories, but without excluding any directories named "proc" or "sys" somewhere else in the file tree.

For, example having the following directory tree ("bla" being normal files):

# find
.
./sys
./sys/bla
./foo
./foo/sys
./foo/sys/bla

I would like to exclude ./sys but not ./foo/sys.

I can't seem to find an --exclude pattern that does that...

# tar cvf /dev/null * --exclude=sys
foo/

or...

# tar cvf /dev/null * --exclude=/sys
foo/
foo/sys/
foo/sys/bla
sys/
sys/bla

Any ideas? (Linux Debian 6)

Answer

CharlesB picture CharlesB · May 9, 2012

You can specify absolute paths to the exclude pattern, this way other sys or proc directories will be archived:

tar --exclude=/sys --exclude=/proc /