I have the root folder
USA and sub-folders
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
How do I do this?
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
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
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