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?


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
| improve this answer | |
  • I think I'll stick with the dumb version :) – phunehehe Apr 11 '11 at 2:25
  • 40
    @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
  • 2
    May I add, that this works for more than "Pretty_name". You can also untar to a specific directory this way. Example: tar -xvf /tmp/backupfiles.tar -C /var/www/ – General Redneck Jan 23 '16 at 2:28
  • 2
    I usually use the dumb way, and I use wildcards in case I don't know what the ugly name will be or I don't want to write it down. mv ugly-* pretty_name – David Grayson Jul 26 '17 at 5:14
  • 2
    Dumb way doesn't work if there's already an ugly_name directory. By the way, beauty is in the eye of the beholder. – kmiklas Apr 23 '19 at 14:51

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!'
| improve this answer | |

Simplest way

tar xvf dml/ugly_name.tar --one-top-level=pretty_name --strip-components 1
| improve this answer | |
  • 1
    (1) This is clearly not the simplest way, as smac89 posted an answer featuring --one-top-level=pretty_name without --strip-components 1.  (2) Conversely, the accepted answer (posted by Mat) features --strip-components 1 without --one-top-level=pretty_name.  (3) Can you explain why your answer, combining these options, is better than any of the older ones? Please do not respond in comments; edit your answer to make it clearer and more complete. – G-Man Says 'Reinstate Monica' Sep 21 '18 at 3:31
  • maybe a real example may help you clarify: ` wget dev.mysql.com/get/Downloads/MySQL-5.7/… tar xvf mysql-5.7.23-linux-glibc2.12-x86_64.tar.gz --one-top-level=mysql57 --strip-components 1 ` – marquicus Sep 22 '18 at 4:11
  • This answer is more elegant than the one with the most upvote. – KaiserKatze May 21 '19 at 15:46
  • @G-Man --strip-component 1 is required, with either the mkdir or --strip-components 1 method, otherwise you just get pretty_name/ugly_name/. – davidA Sep 24 '19 at 23:14
  • 2
    This is indeed the simplest way – smac89 Sep 25 '19 at 1:35

Use this :

tar -xvf ugly_name.tar
mv ugly_name pretty_name
| improve this answer | |
  • 9
    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
  • 2
    This could have undesirable consequences if a directory ugly_name already exists. – Drew Dormann May 12 '17 at 22:05


As pointed out in the comments, this method does not work for archives that are properly formed, however as another answer shows, you should add --strip-components 1 for this to really work. I have tested with this new option and it works both for archives with a top-level name and ones without.

Original answer

From askUbuntu, this answer worked for me:

tar zxvf ugly_name.tgz --one-top-level=pretty_name


Tells tar to create a new directory beneath the extraction directory (or the one passed to `-C') and use it to guard against tarbombs. In the absence of dir argument, the name of the new directory will be equal to the base name of the archive (file name minus the archive suffix, if recognized). Any member names that do not begin with that directory name (after transformations from `--transform' and `--strip-components') will be prefixed with it. Recognized file name suffixes are `.tar', and any compression suffixes recognizable by See -auto-compress.

| improve this answer | |
  • 1
    The issue with this is that it doesn't rename the top level directory, instead it creates a new level, named pretty_name, with the original top-level directory inside that. E.g. if tar xzf ugly_name.tgz would create a directory called ugly_name/, then the command you've provided would create pretty_name/ugly_name/. – davidA Sep 24 '19 at 23:12
  • 1
    @davidA Good point. I have updated my answer to include a fix for this – smac89 Sep 25 '19 at 1:39

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.