I would like to exclude some directories from backup. But not all with specific name - only those lying under specified root directory. With rsync I would use exclude mask like this:


which means that all directories with name directoryToExclude lying below /path/excludeRoot will be excluded.

On the other hand: each directory with name directoryToExclude but lying elsewhere will be included

Is it possible to do it with tar?


That is, unfortunately not straightforwardly possible as the --exclude family of options is not powerful enough.

One approach would be to obtain the list of objects using external program (say find) and then use -T switch to tell tar which files/objects should be included.

Quoting from the man page:

-T, --files-from FILE
  get names to extract or create from FILE

If the filenames contain special symbols (and/or spaces) I would add --null to the tar command line; to generate the list I would also use -print0 with find -- to separate the files/dirs with NUL (\000) character.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.