Udo G
Udo G

Reputation: 13141

tar exclude single files/directories, not patterns

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)

Upvotes: 27

Views: 30325

Answers (3)

pjv
pjv

Reputation: 10708

In this case you might want to use:

--anchored --exclude=sys/\*

because in case your tar does not show the leading "/" you have a problem with the filter.

Upvotes: 7

Stephen Donecker
Stephen Donecker

Reputation: 2391

Using tar you can exclude directories by placing a tag file in any directory that should be skipped.

Create tag files,

touch /sys/.exclude_from_backup
touch /proc/.exclude_from_backup

Then,

tar -czf backup.tar.gz --exclude-tag-all=.exclude_from_backup *

Upvotes: 7

CharlesB
CharlesB

Reputation: 90496

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

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

Upvotes: 16

Related Questions