Bash, wget remove comma from output filename

I read the url file line by line, then pass the url to wget:

FILE=/home/img-url.txt
while read line; do
url=$line
wget -N -P /home/img/ $url
done < $FILE

      

This works, but some file contains a comma in the filename. How to save a file without comma?

Example:

http://xy.com/0005.jpg -> saved as 0005.jpg
http://xy.com/0022,22.jpg -> save as 002222.jpg not as 0022,22

      

I hope you find my question interesting.

UPDATE:

We have a good solution, but is there any solution to the stamping time error?

WARNING: timestamping does nothing in combination with -O. See the manual
for details.

      

+3


source to share


3 answers


This should work:

url="$line"
filename="${url##*/}"
filename="${filename//,/}"
wget -P /home/img/ "$url" -O "$filename"

      

Using -N and -O will both raise a warning message. The wget manual says:



-N (for checking the timestamp) is not supported in conjunction with -O: since the file is always created, it will always have a very new timestamp.

So when you use the -O option, it actually creates a new file with a new timestamp, and thus the option -N

becomes bogus (it cannot do what it is for). If you want to keep timestapming, then a workaround could be as follows:

url="$line"
wget -N -P /home/img/ "$url"
file="${url##*/}"
newfile="${filename//,/}"
[[ $file != $newfile ]] && cp -p /home/img/"$file" /home/img/"$newfile" && rm /home/img/"$file"

      

+1


source


In the body of the loop, you need to generate the filename from the url without commas and without the main part of the url and tell wget to save under a different name.



url=$line
file=`echo $url | sed -e 's|^.*/||' -e 's/,//g'`
wget -N -P /home/image/dema-ktlg/ -O $file $url

      

+1


source


In the meantime, I wrote this:

url=$line
$file=`echo ${url##*/} | sed 's/,//'`
wget -N -P /home/image/dema-ktlg/ -O $file $url

      

Everything seems to be fine, is there some trivial problem with my code?

0


source







All Articles