How to pass a file that changes name to another command in Bash?

4

I frequently use wget to download tarballs and zip files from the web, then either untar then or gunzip them. I do:

wget google.com/somefile.zip
unzip somefile.zip
rm somefile.zip

Is there a way for me to automatically pass the zip file to tar or unzip WHILE wget-ting? In pseudocode:

wget google.com/somfile.zip && unzip

n0pe

Posted 2011-06-09T14:32:55.403

Reputation: 14 506

Answers

7

You could avoid using intermediary files and the problem would disappear

wget -O - http://example.com/file.zip | funzip

RedGrittyBrick

Posted 2011-06-09T14:32:55.403

Reputation: 70 632

So this will work despite not giving unzip the file name of the document? If so, +1 and accepted. Nice and clean. – n0pe – 2011-06-09T14:41:06.393

Yeah, note the difference between unzip which acts on files and funzip which acts on stdin. – slhck – 2011-06-09T14:43:25.087

ahhh yeah I didn't see that. Many thanks! – n0pe – 2011-06-09T14:47:31.907

@MaxMackie yes, the -O - means 'output to stdout'. There is no file, no document. There's just a pipe directly to funzip which will extract to the CWD. – Rich Homolka – 2011-06-09T14:47:52.983

I do rather like this one better than my answer. I've never run across funzip before! Thanks! – Kirk – 2011-06-09T14:56:51.410

1

MYFILE=filename; wget google.com/${MYFILE} && unzip ${MYFILE} && rm -f ${MYFILE}

Kirk

Posted 2011-06-09T14:32:55.403

Reputation: 2 182

0

Both answers here work, and @RedGrittyBrick 's answer eliminates the temp file that you delete anyway (I'd accept his). I'll add another way.

You can do string manipulation on the variables you use for the URL to get the output file.

URL=google.com/somefile.zip
wget $URL
FILE=${URL##*/} # strip everything up to last /
unzip $FILE
rm -f $FILE

Rich Homolka

Posted 2011-06-09T14:32:55.403

Reputation: 27 121

I think you mean "everything up to last /"? – slhck – 2011-06-09T15:09:15.807

@slhck good point, I changed to be last /, in both comment and code. Thanks – Rich Homolka – 2011-06-09T15:52:28.193