cls1991
cls1991

Reputation: 56

wget: Download files with filename from list of urls using wget

I have a file music.txt with audios to download, looking like this:

http://somehow/music/hexfilename1.mp3 1.mp3    
http://somehow/music/hexfilename2.mp3 2.mp3
http://somehow/music/hexfilename3.mp3 3.mp3
http://somehow/music/hexfilename4.mp3 4.mp3

I just want to download the mp3 files, and save as specified names. For instance, download from http://somehow/music/hexfilename1.mp3, we will save as 1.mp3.

Upvotes: 0

Views: 3066

Answers (2)

Chromium
Chromium

Reputation: 517

Actually, this is an easy work, but there may be 2 situations:

  1. If your music.txt is like that in your answer, with url being the first group, and output file being the second, then you can use the following command:

    IFS="\n"
    for line in $(cat music.txt)
    do
        url=$(echo $line | awk '{ print $1 }')
        out=$(echo $line | awk '{ print $2 }')
        wget "$url" -O "$out"
    done
    
  2. For the other situation, you just don't need that much script and simply use a for loop to go thru each line and pass the parameters to wget.

Hope this helps.

Upvotes: 3

user4401178
user4401178

Reputation:

You can read them into variables using a while loop with read:

while read a b; do wget -O "$b" "$a"; done < music.txt

Upvotes: 5

Related Questions