如何使用linux'tar'重命名放入tar存档的文件 [英] how to rename files you put into a tar archive using linux 'tar'
问题描述
我正在尝试用几个文件创建tar归档文件,但在归档文件中重命名这些文件.现在我有这样的东西:
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 -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
在其中解压缩到目录testDir中的位置,您将看到以下文件:
Where, when extracted into directory testDir, you would see the files:
- testDir/file1
- testDir/file2
- testDir/path/to/renamedFile3
- testDir/etc
我该怎么做?
推荐答案
您可以使用--transform
修改文件名(除其他外).例如,要创建磁带存档/tmp/foo.tar
,将文件/etc/profile
和/etc/bash.bashrc
放入其中,同时还将profile
重命名为foo
,则可以执行以下操作:
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:
tar --transform='flags=r;s|bar|foo|' -cf file.tar file1 file2 bar fubar /dir/*
上述结果是bar
作为foo
添加到file.tar
.
Results of the above is that bar
is added to file.tar
as foo
.
r
标志意味着转换仅应用于常规文件.有关更多信息,请参见 GNU tar文档.
The r
flag means transformations are applied to regular files only. For more information see GNU tar documentation.
您可以多次使用--transform
,例如:
You can use --transform
multiple times, for example:
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
这篇关于如何使用linux'tar'重命名放入tar存档的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!