简体   繁体   中英

Bash, wget remove comma from output filename

I am reading a file with URL's by line by line then I pass URL to the 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 comma in the filename. How I can save the file without the 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 some nice solution, but is there any solution to the time stamping error?

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

In the body of the loop you need to generate the filename from the URL without commas and, without the leading part of the URL, and tell wget to save under other name.

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

This should work:

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

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

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

So, when you use -O option, it actually creates a new file with new timestamping and thus the -N option becomes dummy (it can't do what it is for). If you want to preserve the timestapming, then a workaround might be this:

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"

In the meantime I wrote this:

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

Seems to work fine, is there any trivial problem with my code?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM