LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   for loop and space character in names (https://www.linuxquestions.org/questions/programming-9/for-loop-and-space-character-in-names-4175720307/)

salmanahmed 12-29-2022 08:59 AM

for loop and space character in names
 
Hi
I am trying to convert a large number of JPG files into PNG. For this purpose I tried "for" loop as follows:
Code:

for i in *.jpg;do convert $i $i.png;done
This commands works fine for all files, except for those which contains spaces in their names. For example, if a file is named as "my picture.jpg", then "convert" or any other utility gives following error:
Quote:

convert: unable to open image 'my': No such file or directory
convert: unable to open image 'picture.jpg': No such file or directory
So, what change I should make in this "for" loop to accommodate files with space character in their names?
Thanks

Racho 12-29-2022 09:58 AM

try this:

Code:

for i in *.jpg;do convert "$i" "$i.png";done

salmanahmed 12-29-2022 10:38 AM

Quote:

Originally Posted by Racho (Post 6400997)
try this:

Code:

for i in *.jpg;do convert "$i" "$i.png";done

Oh my... Why didn't I think of such an obvious solution :doh:

Thanks a lot :hattip:

MadeInGermany 12-30-2022 03:07 AM

A modifier chops off the old extension:
Code:

convert "$i" "${i%.*}.png"

dugan 12-30-2022 01:06 PM

The code in your top post would work as-is if you use ZSH.


All times are GMT -5. The time now is 12:15 AM.