通过省略顶层目录来从目录创建 tar 文件

通过省略顶层目录来从目录创建 tar 文件

我们如何使用 tar 来创建/提取目录,并省略或删除或删除头/顶级目录名称?

答案1

man tar(GNU tar 1.34)中的一些有用选项:

   -C, --directory=DIR
          Change to DIR before performing any operations.  This option
          is order-sensitive, i.e. it affects all options that follow.

   -P, --absolute-names
          Don't strip leading slashes from file names when creating archives.

   --strip-components=NUMBER
          Strip NUMBER leading components from file names on extraction.

   --transform=EXPRESSION, --xform=EXPRESSION
          Use sed replace EXPRESSION to transform file names.

man bsdtar(bsdtar 3.6.2)中的等效选项:

 -C directory, --cd directory, --directory directory
         In c and r mode, this changes the directory before adding the
         following files.  In x mode, change directories after opening
         the archive but before extracting entries from the archive.

 -P, --absolute-paths
         Preserve pathnames.  By default, absolute pathnames (those that
         begin with a / character) have the leading slash removed both
         when creating archives and extracting from them.  Also, tar will
         refuse to extract archive entries whose pathnames contain .. or
         whose target directory would be altered by a symlink.  This option
         suppresses these behaviors.

 -s pattern
         Modify file or archive member names according to pattern.
         ...

 --strip-components count
         Remove the specified number of leading path elements.  Pathnames
         with fewer elements will be silently skipped.  Note that the
         pathname is edited after checking inclusion/exclusion patterns
         but before security checks.

来自busybox tar --help(busybox tar 1.34.1):

    -C DIR  Change to DIR before operation

    --strip-components NUM  NUM of leading components to strip

相关内容