I’d like to download, and extract an archive under a given directory. Here is how I’ve been doing it so far:
wget http://downloads.mysql.com/source/dbt2-0.37.50.3.tar.gz tar zxf dbt2-0.37.50.3.tar.gz mv dbt2-0.37.50.3 dbt2
I’d like instead to download and extract the archive on the fly, without having the tar.gz written to the disk. I think this is possible by piping the output of wget to tar, and giving tar a target, but in practice I don’t know how to put the pieces together.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You can do it by telling wget to output its payload to stdout (with the flag -O-) and suppress its own output (with the flag -q):
wget -qO- your_link_here | tar xvz
To specify a target directory:
wget -qO- your_link_here | tar xvz -C /target/directory
If you happen to have GNU tar, you can also rename the output dir:
wget -qO- your_link_here | tar --transform 's/^dbt2-0.37.50.3/dbt2/' -xvz
Method 2
Another option is to use curl which writes to stdout by default:
curl -s some_url | tar xvz -C /tmp
Method 3
This oneliner does the trick:
tar xvzf -C /tmp/ < <(wget -q -O - http://foo.com/myfile.tar.gz)
short explanation:
the right side in the parenthesis is executed first (-q tells wget to do it quietly, -O - is used to write the output to stdout).
Then we create a named pipe using the process substitution operator from Bash <( to create a named pipe.
This way we create a temporary file descriptor and then direct the contents of that descriptor to tar using the < file redirection operator.
Method 4
Named pipe with stdin solution and really mind the flags for tar’s -xvz
tar -xvz -C /tmp/ -f <(wget -q -O - https://github.com/user/repo/release/download/v/v.tar.gz)
Method 5
One liner that handles redirects and can extract tar.bz2 files. Use xzfor extracting gzip files.
curl -L https://downloads.getmonero.org/cli/linux64 | tar xj
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0