Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I currently do in a shellscript very inefficiently:

cp -a $MYPATH /tmp/$VERSION
cd /tmp
tar cjf archive.tar.bz2 $VERSION

I want everything to be contained in the directory called $VERSION - is there a way to do this without having to copy everything as in the above script?

share|improve this question

migrated from stackoverflow.com May 14 '13 at 17:17

This question came from our site for professional and enthusiast programmers.

    
Why copying it in the first place? Why not tar cjf archive.tar.bz2 $MYPATH? –  Chewie May 14 '13 at 12:37
1  
@Chewie he said, he want the $version as parent dir in archive. –  Kent May 14 '13 at 12:39
    
Oh, I see it now. –  Chewie May 14 '13 at 12:45

3 Answers 3

up vote 5 down vote accepted

The GNU version of tar supports the --transform option (and its alias --xform), you could use it like this

tar --transform "s/^$MYPATH/$VERSION/" -cf archive.tar.bz2 "$MYPATH"

For example, given this directory tree

foo
└── foo.txt

the command

tar --transform "s/^foo/bar/" -cf foo.tar.bz2 foo

will produce an archive like

$ tar -tf foo.tar.bz2
bar/
bar/foo.txt
share|improve this answer
    
Thanks, that does the job beautifully. –  Robby75 May 15 '13 at 4:30

To tar the current directory and add a prefix, this worked for me:

tar --transform 's,^\.,$VERSION,' -cf foo.tar .
share|improve this answer

If you can get away without preserving symbolic links within the file tree you're tarring, you could do

ln -s $MYPATH /tmp/$VERSION
cd /tmp
tar cjhf archive.tar.bz2 $VERSION

The h option means dereference symlinks, i.e. include the file or directory that the link points to rather than simply recording the fact that there was a symlink and what it pointed to.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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