Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
1  
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
add comment

migrated from stackoverflow.com Jun 11 '13 at 2:59

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

1 Answer

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
add comment

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.