If I have a tar.gz archive containing a single folder named like <somename>-<somenumber>, how do I extract it so it unpacks into a folder called <somename>?

For example, the file somearchive.tar.gz contains a top-level folder somearchive-0.1.2, and I tried something like:

tar xvfz somearchive.tar.gz --transform s/[a-zA-Z]+\-[0-9\.]+/somearchive/

but that extracts it to the default folder.

  • At least with GNU tar, the --transform option expects a GNU basic regular expression I think - so you need to backslash escape the + in order to make it a quantifier. Try --transform 's/[a-zA-Z]\+-[0-9.]\+/somearchive/' (- doesn't need escaping, and . doesn't when inside []) Aug 27, 2019 at 1:21
  • I'd advise against that. The traditional way of doing things is to extract the archive and symlink somename -> somename-somenumber. That way you can easily rollback if needed. If that is not an issue, see unix.stackexchange.com/a/535763/364705
    – markgraf
    Aug 27, 2019 at 7:29

2 Answers 2


As steeldriver pointed out, tar --transform expects a sed replace expression, which uses basic regular expression syntax, not extended regular expression syntax, and in particular the “one or more” operator is \+, not +. See Why does my regular expression work in X but not in Y?

tar xvfz somearchive.tar.gz --transform 's/^[a-zA-Z]\+-[0-9.]\+/somearchive/'

Or you could make it simply

tar xvfz somearchive.tar.gz --transform 's!^[^/]*!somearchive!'

Gilles's answer perfectly fit my question. However, I also found this alternative works as well:

mkdir somearchive
tar xvfz somearchive.tar.gz --strip 1 -C somearchive

You must log in to answer this question.

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