coffee
coffee

Reputation: 31

How to programatically add files to an existing tar file

I have one process that creates 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?

Upvotes: 3

Views: 4823

Answers (1)

Beta
Beta

Reputation: 99094

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/".

Upvotes: 3

Related Questions