Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying to download a tar.gz package with wget, decompress it and rename the decompressed directory all in one (piped) command.

My attemps were:

wget -O- $URL | tar fvxz - -C $DEST

wget -O- $URL | tar fvxz - | mv - $DEST

What am I failing at?

share|improve this question

migrated from Jun 11 '13 at 2:59

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

The standard is clear that the file operand doesn't need to immediately follow the f, but there are many broken implementations where it does. Perhaps OP has one. – kojiro Jun 10 '13 at 23:00
up vote 0 down vote accepted

If you have BSD or GNU tar, you can do this:

mkdir -p "$DEST" && wget -O- "$URL" | tar --strip-components 1 -xvzf- -C "$DEST"

Since GNU tar has --strip-components, but that's not a standard solution. Anyway, you can't really do this with a single pipeline since you have to have the directory already, and there are perfectly good ways of doing this if you're willing to give up on the whole single pipeline anyway.

mkdir -p "$DEST" &&
cd "$DEST" &&
wget -O- "$URL" | tar -xvzf-
cd * && # Assumes the result only has one directory
mv * .. # Assumes no dotfiles

You can resolve issues of dotfiles and tars of multiple directories by tweaking this answer, if necessary.

share|improve this answer
Use of the z flag already implies a non-ancient, more-than-POSIX-minimum tar. – Wumpus Q. Wumbley Jun 10 '13 at 23:14
@WumpusQ.Wumbley the use of the z flag doesn't seem sufficient to imply the existence of --strip-components. – kojiro Jun 10 '13 at 23:19

You must log in to answer this question.

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