2

I'm downloading a list of files from urls that contain addtional information past the filenames with wget. This results in filenames like:

pythonbook.pdf@y=11&x=123

All I really want is the pythonbook.pdf part. My file list is basically a list of URLs like:

https://dl.domain.com/pythonbook.pdf@y=11&x=123

How can I not have the unnecessary part of the filename?

1
  • 1
    if a list of urls has the same common part pythonbook.pdf, then that parameter string is the only thing that distinguishes those filenames. In such case you need to elaborate the final naming convention Jan 3, 2018 at 14:18

1 Answer 1

1

Probably easiest to iterate over the whole list in this case (as wget doesn't seem to have support for the dynamic creation of output file names):

while read url; do
    t=${url##*/}  # removes protocol and hostname
    pdf=${t%@*}   # removes part from @ onwards
    wget -O "$pdf" "$url"
done < LIST-OF-URLs

url is the URL read from the list of URLs in the file, pdf is just the file name part of it.


In case the names are non-unique you can add a number to the file name, e.g. like this (assuming all files are PDFs):

i=0
while read url; do
    t=${url##*/}      # removes protocol and hostname
    pdf=${t%.pdf@*}   # removes part from .pdf@ onwards
    wget -O "$pdf-$i.pdf" "$url"
    ((i++))
done < LIST-OF-URLs

or (if the files have different suffixes)

i=0
while read url; do
    t=${pdf##*/}    # removes protocol and hostname
    pdf=${t%@*}     # removes part from @ onwards
    wget -O "$i-$pdf" "$url"
    ((i++))
done < LIST-OF-URLs
2
  • So the $pdf part is the new file name? Jan 3, 2018 at 15:09
  • @Columbia_Bob uups, naming was a bit confusing, hope it's better now.
    – nohillside
    Jan 3, 2018 at 15:16

You must log in to answer this question.

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