如何使用linux'tar'重命名放入tar存档的文件

时间:2022-12-01 10:33:01

I'm trying to create a tar archive with a couple files, but rename those files in the archive. Right now I have something like this:

我正在尝试使用几个文件创建tar存档,但重命名存档中的这些文件。现在我有这样的事情:

tar -czvf file1 /some/path/to/file2 file3 etc

But I'd like to do something like:

但我想做的事情如下:

tar -czvf file1=file1 /some/path/to/file2=file2 file3=path/to/renamedFile3 etc=etc

Where, when extracted into directory testDir, you would see the files:

哪里,当提取到目录testDir时,你会看到文件:

  • testDir/file1
  • TESTDIR /文件1
  • testDir/file2
  • TESTDIR /文件2
  • testDir/path/to/renamedFile3
  • TESTDIR /路径/到/ renamedFile3
  • testDir/etc
  • TESTDIR的/ etc

How can I do this?

我怎样才能做到这一点?

4 个解决方案

#1


17  

You can modify filenames (among other things) with --transform. For example, to create a tape archive /tmp/foo.tar, putting files /etc/profile and /etc/bash.bashrc into it while also renaming profile to foo, you can do the following:

您可以使用--transform修改文件名(以及其他内容)。例如,要创建磁带存档/tmp/foo.tar,将文件/ etc / profile和/etc/bash.bashrc放入其中,同时将配置文件重命名为foo,您可以执行以下操作:

tar --transform='flags=r;s|bar|foo|' -cf file.tar file1 file2 bar fubar /dir/*

Results of the above is that bar is added to file.tar as foo.

上面的结果是bar被添加到file.tar作为foo。

The r flag means transformations are applied to regular files only. For more information see GNU tar documentation.

r标志表示转换仅应用于常规文件。有关更多信息,请参阅GNU tar文档。

You can use --transform multiple times, for example:

您可以多次使用--transform,例如:

tar --transform='flags=r;s|foo|bar|' --transform='flags=r;s|baz|woz|' -cf file.tar /some/dir/where/foo/is /some/dir/where/baz/is /other/stuff/* /dir/too

#2


2  

With --transform, there's no need to make a temporary testDir first. To prepend testDir/ to everything in the archive, match the beginning anchor ^:

使用--transform,不需要先创建临时testDir。要将testDir /前置到存档中的所有内容,请匹配起始锚点^:

tar --transform "s|file3|path/to/renamedFile3|" \
    --transform "flags=r;s|^|testDir/|" \
    -czvf my_archive.tgz file1 /some/path/to/file2 file3 etc

The r flag is critical to keep the transform from breaking any symlink targets in the archive (which also match ^).

r标志对于保持转换不会破坏归档中的任何符号链接目标(也与^匹配)至关重要。

#3


2  

We can refer to the man tar, the -O option is the best choice since files can be written to standard out.

我们可以参考man tar,-O选项是最好的选择,因为文件可以写成标准输出。

-O      (x, t modes only) In extract (-x) mode, files will be written to
         standard out rather than being extracted to disk.  In list (-t)
         mode, the file listing will be written to stderr rather than the
         usual stdout.

here are the examples:

以下是示例:

# 1. without -O
tar xzf 20170511162930.db.tar.gz
# result: 20170511162930.db

# 2. with -O
tar xzf 20170511162930.db.tar.gz -O > latest.db
# result: latest.db

#4


0  

After not liking any solution that I've found, I've just written tarlogs.py, which lets you specify arbitrary names for tar entries. Each tar entry is constructed from one (or several) regular (or gzipped) inputs. You can also add directories, which will be recursed into as with regular tar. So in your case,

在不喜欢我找到的任何解决方案后,我刚刚编写了tarlogs.py,它允许您为tar条目指定任意名称。每个tar条目由一个(或几个)常规(或gzip)输入构成。您还可以添加目录,这些目录将与常规tar一起递归。所以在你的情况下,

tarlogs.py -o file1 -i /some/path/to/file2 -o file2 -i file3 -o path/to/renamedFile3 -o /etc >output.tar

(-o with no -i inputs simply uses the output path as input, with no renaming)

(-o没有-i输入只使用输出路径作为输入,没有重命名)

#1


17  

You can modify filenames (among other things) with --transform. For example, to create a tape archive /tmp/foo.tar, putting files /etc/profile and /etc/bash.bashrc into it while also renaming profile to foo, you can do the following:

您可以使用--transform修改文件名(以及其他内容)。例如,要创建磁带存档/tmp/foo.tar,将文件/ etc / profile和/etc/bash.bashrc放入其中,同时将配置文件重命名为foo,您可以执行以下操作:

tar --transform='flags=r;s|bar|foo|' -cf file.tar file1 file2 bar fubar /dir/*

Results of the above is that bar is added to file.tar as foo.

上面的结果是bar被添加到file.tar作为foo。

The r flag means transformations are applied to regular files only. For more information see GNU tar documentation.

r标志表示转换仅应用于常规文件。有关更多信息,请参阅GNU tar文档。

You can use --transform multiple times, for example:

您可以多次使用--transform,例如:

tar --transform='flags=r;s|foo|bar|' --transform='flags=r;s|baz|woz|' -cf file.tar /some/dir/where/foo/is /some/dir/where/baz/is /other/stuff/* /dir/too

#2


2  

With --transform, there's no need to make a temporary testDir first. To prepend testDir/ to everything in the archive, match the beginning anchor ^:

使用--transform,不需要先创建临时testDir。要将testDir /前置到存档中的所有内容,请匹配起始锚点^:

tar --transform "s|file3|path/to/renamedFile3|" \
    --transform "flags=r;s|^|testDir/|" \
    -czvf my_archive.tgz file1 /some/path/to/file2 file3 etc

The r flag is critical to keep the transform from breaking any symlink targets in the archive (which also match ^).

r标志对于保持转换不会破坏归档中的任何符号链接目标(也与^匹配)至关重要。

#3


2  

We can refer to the man tar, the -O option is the best choice since files can be written to standard out.

我们可以参考man tar,-O选项是最好的选择,因为文件可以写成标准输出。

-O      (x, t modes only) In extract (-x) mode, files will be written to
         standard out rather than being extracted to disk.  In list (-t)
         mode, the file listing will be written to stderr rather than the
         usual stdout.

here are the examples:

以下是示例:

# 1. without -O
tar xzf 20170511162930.db.tar.gz
# result: 20170511162930.db

# 2. with -O
tar xzf 20170511162930.db.tar.gz -O > latest.db
# result: latest.db

#4


0  

After not liking any solution that I've found, I've just written tarlogs.py, which lets you specify arbitrary names for tar entries. Each tar entry is constructed from one (or several) regular (or gzipped) inputs. You can also add directories, which will be recursed into as with regular tar. So in your case,

在不喜欢我找到的任何解决方案后,我刚刚编写了tarlogs.py,它允许您为tar条目指定任意名称。每个tar条目由一个(或几个)常规(或gzip)输入构成。您还可以添加目录,这些目录将与常规tar一起递归。所以在你的情况下,

tarlogs.py -o file1 -i /some/path/to/file2 -o file2 -i file3 -o path/to/renamedFile3 -o /etc >output.tar

(-o with no -i inputs simply uses the output path as input, with no renaming)

(-o没有-i输入只使用输出路径作为输入,没有重命名)