最上位ディレクトリを省略してディレクトリから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

関連情報