Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

how to rename files you put into a tar archive using linux '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

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

  • testDir/file1
  • testDir/file2
  • testDir/path/to/renamedFile3
  • testDir/etc

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/*

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

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...