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:

create flat tar archive: ignoring all parents when adding folders


1 Answer 1


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.

  • 3
    Note that --transform doesn't work with macOS tar. Mar 17, 2021 at 8:27

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.