开发者

How to programatically add files to an existing tar file

开发者 https://www.devze.com 2023-03-13 16:24 出处:网络
I have one process that c开发者_如何学Pythonreates a tar based on some existing files, then I want another process to take that tar file and add MORE files to it.

I have one process that c开发者_如何学Pythonreates a tar based on some existing files, then I want another process to take that tar file and add MORE files to it.

How is this accomplished programmatically?


There are no folders as such in a tarfile. Each file can have a path, so a tarfile might contain

/some/path/foo
/some/path/bar
/another/path/baz

If you have a file /elsewhere/quartz which you wish to add to the tarfile as /some/path/quartz, this will do it:

tar -rvf tarfilename --transform 's,.*/,/some/path/,' /elsewhere/quartz

(This will work in GNU tar, I can't make promises about other versions.)

The stuff inside the single quotes is a regular expression substitution command, roughly "take everything up to a slash (as much as possible) and replace it with /some/path/".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号