I have the root folder USA and sub-folders California and Texas. Texas is an excluded sub-folder but it has a specific file in it that I'd like to include in the tar backup. Let's call this file Austin.

How do I do this?

  • do you have control over the creation of the tar file? Just add Texas/Austin as another [FILE] to include in the tar file. For example: $ tar cvf /backup/states.tar --exclude=USA/Texas USA USA/Texas/Austin – Jeff Schaller Jul 4 '15 at 2:26

Run tar twice? First exclude Texas and then just extract the file?


I don't know how to do it conveniently with BSD or GNU tar. (Inconveniently: don't exclude the directory itself but exclude every file in the directory except the one you want to include.)

You can do it relatively easily with pax, which is POSIX's replacement for cpio and tar. Pax should be available everywhere, though some Linux distributions don't include it as part of the default installation.

Pax manages exclusions and renaming via the same tool: the -s option. If a file is renamed to the empty string, it's excluded. The -s options are processed in order, and once a file is matched, subsequent options are not processed for that file (unless the g suffix is used). Thus you can unexclude a file by renaming it to itself before the exclusion rule that would match it.

pax -w >foo.tar -s '!^USA/Texas/Austin$!&!' -s'!^USA/Texas/.*!!' USA 

Encountered same requirement as this. Came here to find a solution, but did not get one. Anyway, I managed to achieve it by running tar twice, so posting here.

First run creating the tar file by excluding Texas folder:

tar cvf foo.tar --exclude=Texas USA/

Then append required file to just created tar file:

tar vfr foo.tar USA/Texas/Austin/

This time we removed c (create tar) option and used r (append).


Order matters. There is no need to run tar twice.

tar cvf dest.tar USA/Texas/Austin --exclude=USA/Texas USA

tested on GNU tar 1.29

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.