LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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 12-29-2022, 08:59 AM   #1
salmanahmed
Member
 
Registered: Jun 2020
Posts: 158

Rep: Reputation: Disabled
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
 
Old 12-29-2022, 09:58 AM   #2
Racho
Member
 
Registered: Oct 2021
Posts: 59

Rep: Reputation: Disabled
try this:

Code:
for i in *.jpg;do convert "$i" "$i.png";done
 
1 members found this post helpful.
Old 12-29-2022, 10:38 AM   #3
salmanahmed
Member
 
Registered: Jun 2020
Posts: 158

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Racho View Post
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

Thanks a lot
 
Old 12-30-2022, 03:07 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
A modifier chops off the old extension:
Code:
convert "$i" "${i%.*}.png"
 
1 members found this post helpful.
Old 12-30-2022, 01:06 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,224

Rep: Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320Reputation: 5320
The code in your top post would work as-is if you use ZSH.
 
1 members found this post helpful.
  


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
Space bar key does no longer produce space character on screen hillflyer Linux - Laptop and Netbook 7 02-01-2021 02:16 PM
[SOLVED] Move file names with space to other directory with different file name with space pradeepdee6 Linux - Newbie 9 08-08-2012 09:19 AM
Find and Replace character/special character from the file MyRelam Red Hat 8 05-21-2012 12:52 AM
about wide character and multiple byte character George2 Programming 5 05-23-2006 01:03 AM

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

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