10

Is it possible to set an arbitrary path within a tar archive to place files which may not exist on your original filesystem? This differs from the -C switch in that I may want to take a back up of /etc/, but place it in the archive as /configurations/etc/.

  • 1
    IIRC the -s option does that, but don't have time to write up a whole answer right now. – Chris S Sep 12 '11 at 22:30
  • @ChrisS In GNU tar 1.28 (Ubuntu 16.04) option -s is short for --preserve-order and thus off-topic. here – Stéphane Gourichon Jan 12 '18 at 18:31
7

In GNU tar, --transform (--xform) is the option to make file name transformations. Documentation and examples can be found here

  • I would have seen this had I read man tar on my system rather than on linux.die.net!! Thanks for the answer, regex is even better than I'd hoped for. – andyortlieb Sep 13 '11 at 18:53
  • 2
    @andyorlieb, thanks for the input on "--xform" not working on your system, but please do not change the content of people's answers unless you have a definitive reference to back it up. Thank you. – Chris S Sep 13 '11 at 20:24
  • -s on OSX's tar looks similar – gman Nov 7 '15 at 6:38
  • Mentioned documentation goes to great length to explain --transform=expression at extract time, yet says nothing about add time (which is the actual question asked). – Stéphane Gourichon Jan 12 '18 at 17:56
3

In BSD tar, -s pattern is the option to make name transformations. Quoting from freebsd.org documentation:

-s  pattern
     Modify file or archive member names according to pattern.  The
     pattern has the format /old/new/[ghHprRsS] where old is a basic
     regular expression, new is the replacement string of the matched
     part, and the optional trailing letters modify how the replace-
     ment is handled.  If old is not matched, the pattern is skipped.
     Within new, ~ is substituted with the match, \1 to \9 with the
     content of the corresponding captured group.  The optional trail-
     ing g specifies that matching should continue after the matched
     part and stop on the first unmatched pattern.  The optional
     trailing s specifies that the pattern applies to the value of
     symbolic links.  The optional trailing p specifies that after a
     successful substitution the original path name and the new path
     name should be printed to standard error.  Optional trailing H,
     R, or S characters suppress substitutions for hardlink targets,
     regular filenames, or symlink targets, respectively.  Optional
     trailing h, r, or s characters enable substitutions for hardlink
     targets, regular filenames, or symlink targets, respectively.
     The default is hrs which applies substitutions to all names.  In
     particular, it is never necessary to specify h, r, or s.
  • 1
    I know that the tar says it is specifically about GNU tar, and Stéphane commented that essentially non-gnu tar is off-topic. However, as there isn't a tag for BSD tar, I think this is an appropriate answer. – Patrick M Feb 14 '18 at 22:48

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.