LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-17-2015, 08:40 PM   #1
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 604

Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
find -exec text replace


I have file
file1.jpg file2.jpg in /var/foo
For me to switch from jpg to png I would in a for loop
Code:
for i in *jpg; do convert "$i" "${ijpg}png"; done
How can i do this in a find -exec command?

here's my attempt (doesn't work)
Code:
find /dir -iname \*jpg -exec convert {} ${{}jpg}png \;
I know it can be done, I've forgotten the syntax for it however!
I want it to find every *jpg file, and substitute {} with convert file1.jpg file1.png (etc)

Last edited by Sefyir; 08-17-2015 at 08:44 PM.
 
Old 08-17-2015, 09:15 PM   #2
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Current: Debian and OpenSUSE. Past: Arch, RedHat (pre-RHEL). FreeBSD & OpenBSD novice, Hackintosh
Posts: 1,191
Blog Entries: 7

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Me, I'd write a shell script containing your line above, and exec that.

conv.sh
Code:
#!/bin/bash

convert "$1" "${1.jpg}png"
Code:
find /dir -iname \*.jpg -exec conv.sh {} \;
 
Old 08-19-2015, 03:00 PM   #3
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 604

Original Poster
Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Thanks for the idea goumba. That could be useful for more expansive scripts running on each file

I recalled what I did (maybe this will be useful for whoever stumbles here)

If I wanted to convert a bunch of *jpgs to png and then rename in a single find command
Code:
find /dir/to/jpgs/ -iname \*jpg -execdir convert {} {}.png \; -execdir rename 's/jpg\.png/png/' {}.png \;
This finds all files ending with jpg - foo.jpg
foo.jpg -> foo.jpg.png (a png file) -> foo.png
 
Old 08-19-2015, 04:13 PM   #4
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
Quote:
Originally Posted by Sefyir View Post
Code:
"${ijpg}png"
Are you sure that's right? I've never seen that kind of string replace syntax before, it doesn't make any sense to me, and it doesn't work in my tests. This would work:
Code:
"${i/jpg/png}"
but that's very different than what you wrote.

Code:
$ i=file.jpg
$ echo "$i"
file.jpg
$ echo "${i/jpg/png}"
file.png
$ echo "${ijpg}png"
png
Which makes sense. You're adding the string "png" onto the end of the variable "ijpg". Since "ijpg" doesn't exist, all you get is "png", so your convert command expands into "convert file.jpg png". Maybe convert is smart enough to do that properly, if so it makes your find command much simpler, if not then you're chasing a rabbit down a hole that doesn't exist.

Last edited by suicidaleggroll; 08-19-2015 at 04:18 PM.
 
1 members found this post helpful.
Old 08-19-2015, 08:14 PM   #5
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Current: Debian and OpenSUSE. Past: Arch, RedHat (pre-RHEL). FreeBSD & OpenBSD novice, Hackintosh
Posts: 1,191
Blog Entries: 7

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
suicidaleggroll, I found it odd too, but I noticed even in code tags, that the forum will mangle certain character sequences (learned this in a recent bash thread), so I ignored it, but good thing that you did indeed point out the OPs error.
 
Old 08-19-2015, 09:19 PM   #6
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,573

Rep: Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134Reputation: 2134
The forum does strip out the first % symbol in a post, regardless if it's in code tags. Maybe there was one in his post that got ripped out? (Note: I had to put two % symbols in up there to get the one to come through).
 
Old 08-19-2015, 11:19 PM   #7
goumba
Senior Member
 
Registered: Dec 2009
Location: New Jersey, USA
Distribution: Current: Debian and OpenSUSE. Past: Arch, RedHat (pre-RHEL). FreeBSD & OpenBSD novice, Hackintosh
Posts: 1,191
Blog Entries: 7

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
It is possible. I don't want to drag this too far off-topic, but fyi it also does the same for backslash.
 
Old 08-20-2015, 12:28 PM   #8
Sefyir
Member
 
Registered: Mar 2015
Distribution: Linux Mint
Posts: 604

Original Poster
Rep: Reputation: 301Reputation: 301Reputation: 301Reputation: 301
Ahh... yes. That's weird. I do use the percent symbol there. It looks like it got ripped out and makes it look like a very non-valid command. forum limitation I believe..
a $i followed by a percent symbol is used to strip out characters and then add in the correct png to make convert file.jpg file.png

I have noticed the percent symbol being stripped, even doubles since I'll edit the post and it'll go away too. Very annoying behavior for code tags

e.g.
Code:
"${i%%jpg}png"
(I put in 3 percent symbols and had to add more once I edited it)

Last edited by Sefyir; 08-20-2015 at 01:27 PM.
 
  


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
find and replace text SUSE rfrusher Linux - Newbie 5 10-22-2011 02:55 AM
[SOLVED] command to find/replace in a text file? sneakyimp Linux - Newbie 17 02-15-2011 01:30 AM
[SOLVED] Exact text find and replace gdavis2287 Programming 3 08-14-2010 01:35 AM
Find text and replace another field jaysin_aus Linux - Server 9 07-26-2007 03:16 PM
How to 'Find and Replace' text in vi editor? concoran Programming 2 11-21-2001 10:40 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:29 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration