LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-09-2023, 02:50 AM   #1
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Rep: Reputation: 31
Bash loop


I have 15 jpg files which want to reduce in size and rename xxx-01.jpg to xxx-15.jpg
As a starter I have tried:
Code:
#!/bin/bash

for i in $(ls *.jpg) ; do
convert $i $(basename $i) -resize 600x400 $i R.jpg
done
but it's not looping. What do I need to do to complete the loop?
 
Old 05-09-2023, 02:54 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
I would try:

Code:
for i in *.jpg ; do
as per why you shouldn't parse the output of ls(1).
 
1 members found this post helpful.
Old 05-09-2023, 02:57 AM   #3
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Please use shellcheck to check your script, it will definitely give you some ideas to make it better.
Would be nice to tell us what's happening, what is printed. Also you can use set -xv to debug your script and insert an echo to see what's going on.
Code:
# set -xv
for i in *.jpg ; do  # do not use ls here
    echo converting $i
    convert ...
done
 
1 members found this post helpful.
Old 05-09-2023, 03:44 AM   #4
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
When I run:
Code:
#!/bin/bash

for i in $(ls *.jpg) ; do
convert $i $(basename $i) -resize 600x400   $i *R.jpg
done
It gives me three files: *R-0.jpg *R-1.jpg and *R-2.jpg
All are the same image (last on the list), the first two reduced, the third full size


When I run:
Code:
# set -xv
for i in *.jpg ; do  # do not use ls here
    echo converting $i
    convert -resize 600x400
done
I get:

Code:
converting JPEG_20230509_131747_191157587250982617.jpg
convert-im6.q16: missing an image filename `600x400' @ error/convert.c/ConvertImageCommand/3226.
fifteen times
 
Old 05-09-2023, 03:48 AM   #5
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
Note error copying second script, should have been:
Code:
#!/bin/bash

# set -xv
for i in *.jpg ; do  # do not use ls here
    echo converting $i
    convert -resize 600x400
done
 
Old 05-09-2023, 03:56 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
convert -resize 600x400 <here missing operands>
 
1 members found this post helpful.
Old 05-09-2023, 03:57 AM   #7
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,295
Blog Entries: 3

Rep: Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719Reputation: 3719
Quote:
Originally Posted by Johng View Post
fifteen times
Ok. The loop part is solved then.

What do the file names look like? Do they already contain the incremented number 01 - 15 or should your script add that too?
 
1 members found this post helpful.
Old 05-09-2023, 04:15 AM   #8
rizitis
Member
 
Registered: Mar 2009
Location: Greece,Crete
Distribution: Slackware64-current, Slint
Posts: 613

Rep: Reputation: 474Reputation: 474Reputation: 474Reputation: 474Reputation: 474
Code:
#!/bin/bash

counter=1
for file in *.jpg; do
    # Rename file to Rxxx.jpg
    mv "$file" "R$(printf '%03d' $counter).jpg"
    
    # Convert to 600x400
    convert "R$(printf '%03d' $counter).jpg" -resize 600x400\! "R$(printf '%03d' $counter).jpg"
    
    counter=$((counter+1))
take this script as an example and modify it for your needs...
 
Old 05-09-2023, 04:25 AM   #9
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
Thank you for your help(s)

pan64:
Quote:
convert -resize 600x400 <here missing operands>
I don't understand


Turbocapitalist:
Quote:
The loop part is solved then
No, only 3 loops out of needed 15
and as per my reply the three were numbered: *R-0.jpg *R-1.jpg and *R-2.jpg note the unwanted "*" and missing leading zero

I want them to be the series: R-1.jpg R-2.jpg R-3.jpg . . . . up to R-15.jpg
 
Old 05-09-2023, 01:53 PM   #10
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
Never used convert before, but man convert says:
Code:
 convert-im6.q16 [input-option] input-file [output-option] output-file
So you need to tell convert the input-file and the output-file names.
Try this:

Code:
for i in * ; do 
   echo converting $i to R${i%.*}
   convert $i -resize 600x400 R${i%.*}.jpg; 
done
This code will convert MyPic1.jpg to RMyPic1.jpg.
But if RMyPic1.jpg already exists it will be overwritten!

So it may be better to create an empty subdirectory first and store there the new files:

Code:
mkdir out_dir
for i in * ; do 
   echo converting $i to out_dir/R${i%.*}
   convert $i -resize 600x400 out_dir/R${i%.*}.jpg; 
done
 
1 members found this post helpful.
Old 05-09-2023, 04:24 PM   #11
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
Thank you all for your suggestions.

rizitis: I ran your script but got a syntax error: unexpected end of file

Code:
#!/bin/bash

counter=1
for file in *.jpg; do
    # Rename file to Rxxx.jpg
    mv "$file" "R$(printf '%03d' $counter).jpg"

    # Convert to 600x400
   convert "R$(printf '%03d' $counter).jpg" -resize 600x400\! "R$(printf '%03d' $counter).jpg"

Racho: your script made a directory with all 15 files converted files with original file name preceded with letter R

Would it be possible to have the name of the output files starting with R01.jpg to R15.jpg??
 
1 members found this post helpful.
Old 05-09-2023, 04:55 PM   #12
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
Ok, try this:

Code:
mkdir out_dir
let num=1
for i in *.jpg ; do
   outfilename=$(printf "out_dir/R%02i.jpg" $num)
   echo converting $i to $outfilename
   convert $i -resize 600x400 $outfilename
   let num++
done
Before running it try to understand every step so you can adapt it to your needs whenever you want.
Understanding code before running it has the double purpose of learn from it and keeping you safe from potential dangerous code.
 
1 members found this post helpful.
Old 05-09-2023, 05:42 PM   #13
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
Thank you Racho, that worked perfectly
 
Old 05-23-2023, 11:11 PM   #14
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
A new need:
to convert the image files to out_dir with original name
 
Old 05-23-2023, 11:24 PM   #15
Johng
Member
 
Registered: Feb 2002
Location: NZ
Distribution: Kubuntu, Mint
Posts: 408

Original Poster
Rep: Reputation: 31
Cancel that, sorted:
Code:
mkdir out_dir
let num=1
for i in *.jpg ; do
   outfilename=$(printf "out_dir/$i.jpg")
   echo converting $i to $outfilename
   convert $i -resize 600x400 $outfilename
   let num++
done
 
  


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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 9 07-28-2015 03:49 PM
Bash script issue (for loop inside a loop) Mperonen Programming 3 08-08-2013 02:14 AM
[SOLVED] Bash - While Loop reading from two lists simultaneously - nested while loop wolverene13 Programming 11 10-01-2011 05:00 PM
[SOLVED] [BASH] non-empty variable before loop end, is empty after exiting loop aitor Programming 2 08-26-2010 09:57 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:32 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