25

For example I have a link http://www.abc.com/123/def/ghi/jkl.mno. I want to download it using wget or curl and get the name of output file as def_ghi_jkl.mno, where the part def_ghi is taken from the link.

I will put this wget command in a script to download multiple files so it can't be giving the output file name explicitly.

5 Answers 5

33

curl has the -o, --output option which takes a single argument indicating the filename output should be written to instead of stdout. If you are using {} or [] to surround elements in the URL (usually used to fetch multiple documents), you can use # followed by a number in the filename specifier. Each such variable will be replaced with the corresponding string for the URL being fetched. To fetch multiple files, add a comma-separated list of tokens inside the {}. If parts of the URLs to be fetched are sequential numbers, you can specify a range with [].

Examples:

  curl http://www.abc.com/123/{def}/{ghi}/{jkl}.mno -o '#1_#2_#3.mno'

Note the quotes around the option argument (not needed unless the the filename starts with one of the expanded variables).  This should result in the output file def_ghi_jkl.mno.

  curl http://www.abc.com/123/{def}/{ghi}/{jkl,pqr,stu}.mno -o '#1_#2_#3.mno'

This should result in the output files def_ghi_jkl.mno, def_ghi_pqr.mno and def_ghi_stu.mno.

 curl http://www.abc.com/123/{def}/{ghi}/[1-3].mno -o '#1_#2_#3.mno'

This should result in the output files def_ghi_1.mno, def_ghi_2.mno, def_ghi_3.mno.

4

wget has a switch -O (long form --output-document) which allows you to specify the name of the file to save to. (Presumably curl has something similar.) So you could do:

wget -O def_ghi_jkl.mno http://www.abc.com/123/def/ghi/jkl.mno

and it will do what you want.

You could probably create a wrapper around wget if you want to automate this naming scheme, but it'd be pretty hard to get bullet-proof and is definitely out of the scope of this answer. (The simple case of a single file downloaded from an explicit URL shouldn't be very hard to get right, but that's not wget's only mode of operation. To name just one case that makes this slightly non-trivial, you can specify multiple URLs on the command line.)

Note that -O is not the same at all as -o, which write's wget's own output to the named file.

2
  • 1
    It's part of a script, explicit giving file name like that wont work.
    – gvz
    Sep 20, 2013 at 9:56
  • 1
    @user47567 Why not? What about the fact that wget or curl is being called from a script makes this impractical? (And really, if you have such constraints on what answers will work, that information should really go into the question from the beginning.)
    – user
    Sep 20, 2013 at 11:09
3

If you prefer curl, the most direct way is:

curl -L -o <filename> "https://drive.google.com/uc?export=download&id=<file id>"

filename: the name of the downloaded file

file id: the id of the file in the web view of google drive in the format https://drive.google.com/file/d/file id/view

2

Here is Some Bash Substitution trick

link="http://www.abc.com/123/def/ghi/jkl.mno"
OutputFile=$( echo ${link:23: 23}| tr "/" "_" )
echo $OutputFile
def_ghi_jkl.mno

{$link:23: 23} will remove "http://www.abc.com/123/" it is ${parameter:offset:length}, then tr will replace / to _.

So now you can easily use with wget or curl

wget $link  -O $OutputFile

Also we can use awk , this will extract last three filed from input string :

OutputFile=$( echo $link | awk -F/ 'BEGIN{OFS="_"}{ print $( NF-2),$(NF - 1 ),$NF}' )
2

The file name you need can not be derived by wget itself, so it needs to be handeled by a shell script:

$ url='http://www.example.com/123/def/ghi/jkl.mno'
$ outFile=$(echo "$url" | cut -d /  -f 5- | tr / _)
$ echo $outFile 
def_ghi_jkl.mno
$ wget "$url" -O "$outFile"

Or if you like it as one line:

wget "$url" -O "$(echo "$url" | cut -d /  -f 5- | tr / _)"

You must log in to answer this question.

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