Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a directory setup like the following:

folder_x
file.y
file.z

I would like to create a tar file so that when it's extracted the structure will look something like this:

dir_q/folder_x
dir_q/file.y
dir_q/file.z

How could I get this to work by using tar? For reference, the current command that I'm using is:

tar -czf archive.tar.gz file.y file.z folder_x
share|improve this question

I've never used the --transform option, but I gather it would do what you need:

tar -czf archive.tar.gz --transform 's,^,dir_q/,' file.y file.z folder_x

Realize the use of , instead of / as separator, so we can add a trailing slash.

share|improve this answer
    
Hmm, it doesn't seem to be doing it properly: file.z -> dirfile.z and dirfolder_x. I'll play around with the regex too see if I can get it the way I want it. – Tim Cooper Jan 25 '11 at 19:40
1  
Here's how I do it: tar -czf archive.tar.gz --transform 's,^,dir_q/,' file.y file.z folder_x Sed allows , instead of /, avoiding ambiguity when there are forward slashes in your expression. Normally I would say you can escape the slash with a backslash, but sed (at least, gnu sed) doesn't interpret it correctly. – jdb Jul 15 '14 at 21:33

If you don't mind creating the new directory...

mkdir dir_q;tar -C $_ -xzvf archive.tar.gz

alternatively, make a new directory, untar the file to that directory, then re-tar it from the parent directory to include it's name in the extracted version. That way you do not have to remember some super long invocation.

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .