add a directory when creating tar archive

末鹿安然 提交于 2020-06-12 08:00:07

问题


I want to tar some files (and directories) but want to create an additional directory those files should reside in.

So for example for the following directory structure:

myproject
  + file1
  + file2
  + subdir1
    + file3

I cd into the myproject directory and want to create the tar archive

tar czf myproject.tgz --place-into-dir myproject-1.0 file2 subdir1

to receive a tar archive with the following content:

myproject-1.0
  + file2
  + subdir1
    + file3

I am searching for an option like my fictional "--place-into-dir" above. So the files should not be placed directly into the archive, but instead into an additional directory that is only created in the tar archive.

I want to avoid first creating that directory, copying all the files to that directory and then deleting them again only to create the tar archive.


回答1:


You can create a directory on the fly inside the archive using --transform:

tar czf myproject.tgz --transform 's,^,myproject-1.0/,' file2 subdir1



回答2:


GNU tar has --transform option:

--transform=EXPRESSION, --xform=EXPRESSION

use sed replace EXPRESSION to transform file names

This should help you:

tar --transform 's/^myproject$/\0-1.0/' -czf myproject.tgz myproject


来源:https://stackoverflow.com/questions/40990371/add-a-directory-when-creating-tar-archive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!