Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Say I have file named ugly_name.tar, which when extracted, becomes ugly_name directory. What command can I use such that the resulting directory name is pretty_name instead?

share|improve this question
    
Good question! I remember looking once, but didn't find any solution. –  phunehehe Apr 10 '11 at 15:22

3 Answers 3

up vote 22 down vote accepted

This should work:

mkdir pretty_name && tar xf ugly_name.tar -C pretty_name --strip-components 1

-C changes to the specified directory before unpacking (or packing). --strip-components removes the specified number of directories from the filenames stored in the archive.

Note that this is not really portable. GNU tar and at least some of the BSD tars have the --strip-components option, but doesn't seem to exist on other unix-like platforms.

The dumb way of doing this would work pretty much everywhere though.

tar xf ugly_name.tar && mv ugly_name pretty_name
share|improve this answer
    
I think I'll stick with the dumb version :) –  phunehehe Apr 11 '11 at 2:25
4  
@phunehehe: one advantage of the smart version is u don't have to know in advance what the untarred directory name is –  Tshepang Apr 12 '11 at 10:00

With GNU tar ≥1.16, use --transform to apply a sed regexp transformation to each file name (the transformation is applied on the full path in the archive):

tar xf foo.tar --transform 's!^ugly_name\($\|/\)!pretty_name\1!'

If ugly_name is the toplevel component of all the file names in the archive, you don't need to bother with precise matching:

tar xf foo.tar --transform 's/ugly_name/pretty_name/'

If you don't know the name of the toplevel directory, you can still use this method, though you might prefer --strip-components instead.

tar xf foo.tar --transform 's!^[^/]\+\($\|/\)!pretty_name\1!'

With pax, the POSIX archive utility, use the -s option, which takes a sed-like replacement expression as an argument.

pax -rf foo.tar -s '!^ugly_name\($\|/\)!pretty_name\1!'
pax -rf foo.tar -s '/ugly_name/pretty_name/'
pax -rf foo.tar -s '!^[^/]\+\($\|/\)!pretty_name\1!'
share|improve this answer

Use this :

tar -xvf ugly_name.tar
mv ugly_name pretty_name
share|improve this answer
4  
You may prefer to use && list operator between those two commands, so the second get executed only if the first one succeeded. –  manatwork Oct 12 '12 at 9:48

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.