Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
07-24-2021, 09:58 AM
|
#1
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983
|
wget question, how to combine -i foo.txt and -O name.jpg
I currently use a text file labeled just grab.txt to grab a series of images from the schools site.
The images are just 1.jpg, 2.jpg, etc.... so that makes them easy to grab.
After I run a simple script to rename them to class.date.####.jpg
If there is only a small number of images to grab I'll do them by hand with:
wget URL -O class.date.####.jpg
many days there are between 10 - 1000 images to grab, thus the -i option with the grab.txt file.
Is there a way to combine the -i with -O in one step without having to edit and run the renaming script?
Thank you.
|
|
|
07-24-2021, 10:13 AM
|
#2
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
Looks like curl will be a better fit for this.
|
|
1 members found this post helpful.
|
07-24-2021, 10:49 AM
|
#3
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983
Original Poster
|
shruggy,
Thank you for the link, i have a question on the output
Code:
curl "URL.[1-100].jpg" -o class.date.[0001-0100].jpg
created a single file named lass.date.[0001-0100].jpg and that contained just the first file. I did see the output from curl grabbing 1-100.jpg , just didnt do as expected.
|
|
|
07-24-2021, 10:52 AM
|
#4
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
You should look up description of the -o option in the curl man page. It's
Code:
curl "URL/[1-100].jpg" -o "class.date.#1.jpg"
|
|
1 members found this post helpful.
|
07-24-2021, 11:04 AM
|
#5
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983
Original Poster
|
Quote:
Originally Posted by shruggy
You should look up description of the -o option in the curl man page. It's
Code:
curl "URL/[1-100].jpg" -o "class.date.#1.jpg"
|
Thank you that is better, but instead of class.date.0001.jpg through class.date.1000.jpg or what ever the actual range is for the day, today its 0001-0011, i get outputs with ## instead of leading 0's.
Attempting to replace ###1 with 0001 results in a single output again.
|
|
|
07-24-2021, 11:37 AM
|
#6
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
curl doesn't work this way. You cannot zero pad output name variables. You have to rename the files afterwards.
Code:
curl "URL/[1-100].jpg" -o "class.date.#1.jpg"
rename class.date. class.date.000 class.date.?.jpg
rename class.date. class.date.00 class.date.??.jpg
rename class.date. class.date.0 class.date.???.jpg
|
|
|
07-24-2021, 11:46 AM
|
#7
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983
Original Poster
|
in that case, im back to my original process of wget -i grab.txt and afterwards running my renaming script.
|
|
|
07-24-2021, 11:52 AM
|
#8
|
Senior Member
Registered: Mar 2020
Posts: 3,706
Rep:
|
Well, at least, you can
Code:
curl \
-o "class.date.000#1.jpg" "URL/[1-9].jpg" \
-o "class.date.00#1.jpg" "URL/[10-99].jpg" \
-o "class.date.0#1.jpg" "URL/[100-999].jpg"
|
|
|
07-24-2021, 11:57 AM
|
#9
|
LQ Guru
Registered: Oct 2004
Distribution: Arch
Posts: 5,216
|
Quote:
Is there a way to combine the -i with -O in one step without having to edit and run the renaming script?
|
Simple example:
cat testfile.txt
Code:
http://www.a.com/one.jpg
http://www.a.com/two.jpg
http://www.a.com/three.jpg
http://www.a.com/four.jpg
http://www.a.com/five.jpg
http://www.a.com/six.jpg
http://www.a.com/seven.jpg
http://www.a.com/eight.jpg
http://www.a.com/nine.jpg
http://www.a.com/ten.jpg
Code:
mylist=($(while read line; do echo "$line"; done < testfile.txt))
agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0"
num=1
for i in "${mylist[@]}"; do
echo "wget -U "$agent" "$i" -O class.date."$num".jpg"
num=$(($num +1))
done
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/one.jpg -O class.date.1.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/two.jpg -O class.date.2.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/three.jpg -O class.date.3.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/four.jpg -O class.date.4.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/five.jpg -O class.date.5.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/six.jpg -O class.date.6.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/seven.jpg -O class.date.7.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/eight.jpg -O class.date.8.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/nine.jpg -O class.date.9.jpg
wget -U Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0 http://www.a.com/ten.jpg -O class.date.10.jpg
|
|
1 members found this post helpful.
|
07-24-2021, 12:14 PM
|
#10
|
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,707
|
Why not simply
Code:
for f in {1..1000}
do
zf="0000$f"
echo curl "url.$f.jpg" -o "class.date.${zf: -4}.jpg"
done
?
|
|
1 members found this post helpful.
|
07-24-2021, 12:47 PM
|
#11
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983
Original Poster
|
Quote:
Originally Posted by boughtonp
Why not simply
Code:
for f in {1..1000}
do
zf="0000$f"
echo curl "url.$f.jpg" -o "class.date.${zf: -4}.jpg"
done
?
|
Running that with correct URL & 1...#, I get the echo output, but the command does not execute? would it not just be curl instead of echo curl inside the for loop?
edit to update:
So I removed the echo command and it works perfect. Thank you all for both the help and guidance.
Last edited by lleb; 07-24-2021 at 12:48 PM.
|
|
|
07-24-2021, 03:08 PM
|
#12
|
Senior Member
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,707
|
Yep, the echo was so I could confirm I hadn't typed something wrong, but I didn't want to make requests / cleanup files.
Also I'll note that space after the colon is important - without it the substring expansion would instead set a default value if the parameter was unset or null - more details in the docs.
Last edited by boughtonp; 07-24-2021 at 03:13 PM.
|
|
1 members found this post helpful.
|
07-24-2021, 05:31 PM
|
#13
|
Senior Member
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983
Original Poster
|
this is what i ended with
Code:
#!/bin/bash
# file to grab multiple web images/files and rename in local directory
# July 24, 2021
# f = total number of images to grab
# zf = listed number for output with leading zeros pre-padded in zf="0000$f"
# url = URL to grab files from
#url=https://foo/
#for f in {1..1000}
#do
# zf="0000${f}"
# curl "{url}${f}.jpg" -o "class.date.${zf: -4}.jpg"
#done
# copy/paste the URL in url= location
# adjust 1..1000 to match correct # of files to grab
# replace NN. with name of output file
url=
for f in {1..1000}
do
zf="0000${f}"
curl "${url}${f}.jpg" -o "NN.${zf: -4}.jpg"
done
Yes I intentionally have the raw code commented out. Helps me remember when I go back after time to read the code and figure out what its doing.
|
|
|
All times are GMT -5. The time now is 01:19 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|