2

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.

Cerin
  • 1,601
  • 4
  • 18
  • 25
  • 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 `[]`) – steeldriver Aug 27 '19 at 01: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 https://unix.stackexchange.com/a/535763/364705 – markgraf Aug 27 '19 at 07:29

2 Answers2

1

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 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
0

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
Cerin
  • 1,601
  • 4
  • 18
  • 25