how to rename files you put into a tar archive using linux 'tar'

前端 未结 4 1117
梦谈多话
梦谈多话 2021-01-03 21:32

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


        
4条回答
  •  借酒劲吻你
    2021-01-03 21:41

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

    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 ^).

提交回复
热议问题