LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
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


Reply
  Search this Thread
Old 07-24-2021, 09:58 AM   #1
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 07-24-2021, 10:13 AM   #2
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Looks like curl will be a better fit for this.
 
1 members found this post helpful.
Old 07-24-2021, 10:49 AM   #3
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
Old 07-24-2021, 10:52 AM   #4
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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.
Old 07-24-2021, 11:04 AM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by shruggy View Post
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.
 
Old 07-24-2021, 11:37 AM   #6
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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
 
Old 07-24-2021, 11:46 AM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
in that case, im back to my original process of wget -i grab.txt and afterwards running my renaming script.
 
Old 07-24-2021, 11:52 AM   #8
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
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"
 
Old 07-24-2021, 11:57 AM   #9
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,146
Blog Entries: 6

Rep: Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832Reputation: 1832
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.
Old 07-24-2021, 12:14 PM   #10
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554

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.
Old 07-24-2021, 12:47 PM   #11
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
Quote:
Originally Posted by boughtonp View Post
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.
 
Old 07-24-2021, 03:08 PM   #12
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,616

Rep: Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554Reputation: 2554

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.
Old 07-24-2021, 05:31 PM   #13
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora/Pop!_OS
Posts: 2,983

Original Poster
Rep: Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551Reputation: 551
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between ./foo, . foo and source foo? stf92 Linux - Newbie 7 04-26-2014 01:04 AM
Difference between packages linux-headers-foo and linux-headers-foo-generic? jhwilliams Ubuntu 1 10-19-2009 05:04 AM
cat onelinefile.txt >> newfile.txt; cat twofile.txt >> newfile.txt keep newline? tmcguinness Programming 4 02-12-2009 06:38 AM
Which config file should I use... foo or foo.new? davidguygc Slackware 6 08-01-2007 05:21 PM
void foo(void) and void foo() lackluster Programming 9 02-15-2003 10:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 09:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration