LinuxQuestions.org
Help answer threads with 0 replies.
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, 09:59 AM   #1
salmanahmed
Member
 
Registered: Jun 2020
Posts: 131

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, 10:58 AM   #2
Racho
LQ Newbie
 
Registered: Oct 2021
Posts: 26

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, 11:38 AM   #3
salmanahmed
Member
 
Registered: Jun 2020
Posts: 131

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, 04:07 AM   #4
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,228

Rep: Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995Reputation: 995
A modifier chops off the old extension:
Code:
convert "$i" "${i%.*}.png"
 
Old 12-30-2022, 02:06 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 10,773

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 03:16 PM
[SOLVED] Move file names with space to other directory with different file name with space pradeepdee6 Linux - Newbie 9 08-08-2012 10:19 AM
Find and Replace character/special character from the file MyRelam Red Hat 8 05-21-2012 01:52 AM
about wide character and multiple byte character George2 Programming 5 05-23-2006 02:03 AM

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

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