1

I want to tar the files foo and bar into a tar archive archive.tar, but I want them to appear, within the archive, as being within a directory, bazdir. Thus when I untar someplace I want bazdir to be created and foo and bar to be created within it.

How can I do that?

This would be the opposite of:

https://stackoverflow.com/questions/939982/how-do-i-tar-a-directory-of-files-and-folders-without-including-the-directory-it
or
create flat tar archive: ignoring all parents when adding folders

4

You can use the --transform option. For example:

touch foo bar
tar cf archive.tar foo bar --transform 's,^,bazdir/,'
tar tvf archive.tar 
-rw-r--r-- tigger/tigger     0 2017-10-11 19:32 bazdir/foo
-rw-r--r-- tigger/tigger     0 2017-10-11 19:32 bazdir/bar

For more details and more complex options see How to create a common base folder with tar and how to rename folders? - on the sister site, ask ubuntu.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.