如何在将文件添加到 tar 时指定任意前导路径?

如何在将文件添加到 tar 时指定任意前导路径?

是否可以在 tar 存档中设置任意路径来放置原始文件系统中可能不存在的文件?这与 -C 开关不同,因为我可能想要备份 /etc/,但将其作为 /configurations/etc/ 放在存档中。

答案1

在 GNU tar 中,--transform( --xform) 是进行文件名转换的选项。文档和示例可以在这里找到这里

答案2

在 BSD tar 中,-s pattern是进行名称转换的选项。引用自freebsd.org 文档

-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.

相关内容