LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   find -exec text replace (https://www.linuxquestions.org/questions/linux-newbie-8/find-exec-text-replace-4175550994/)

Sefyir 08-17-2015 07:40 PM

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)

goumba 08-17-2015 08:15 PM

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 {} \;

Sefyir 08-19-2015 02:00 PM

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

suicidaleggroll 08-19-2015 03:13 PM

Quote:

Originally Posted by Sefyir (Post 5407165)
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.

goumba 08-19-2015 07:14 PM

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.

suicidaleggroll 08-19-2015 08:19 PM

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).

goumba 08-19-2015 10:19 PM

It is possible. I don't want to drag this too far off-topic, but fyi it also does the same for backslash.

Sefyir 08-20-2015 11:28 AM

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)


All times are GMT -5. The time now is 06:25 AM.