I want to make a tar archive containing various files scattered around the filesystem:

$ tar czvf outfile.tar.gz /some/file /some/other/filename

This creates the archive successfully, but when I extract it, the files are still in a directory tree matching the original locations:

$ tar xvf outfile.tar.gz

I'd like to have them both extracted to the current directory, such that file and filename are created on their own.

I can do this at extract time using --transform, e.g.:

$ tar xvf outfile.tar.gz --transform=s,.*/,,

This does what I want, but it's a bit annoying to have to use an obscure option at extract-time, makes it more difficult to send to others, and so forth.

I could do it at create-time like this:

$ cd tmpdir
$ cp /some/file /some/other/filename .
$ tar czvf outfile.tar.gz *

But that seems a bit heavyweight, especially if there are large files or a lot of them.

Is there any way to control the filenames that tar stores at create-time directly?


Doesn't --transform work when creating the archive, then? It seems to work on my GNU tar 1.27.1:

$ tar czf out.tar.gz some/foo some/other/bar --transform=s,.*/,,
$ tar tzf out.tar.gz 

If it doesn't work, another possibility would be to make links to the files in the current directory (as you did with cp, but with ln):

$ ln /some/foo /some/other/bar .
$ tar czf out.tar.gz foo bar
  • That does work. All the Web examples I found used it only on extract, but seems it's incomplete. – Tom Hunt Sep 14 '16 at 20:08

There is a very useful tool named tardy that can be used to edit tar files:

tardy -Remove_Prefix 55 outfile.tar.gz transformed.tar.gz

Here 55 is an arbitrary number larger than the maximum depth of all directories in outfile.tar.gz. -Remove_Prefix 55 tells tardy to remove (at most) 55 leading directories from filenames.

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.