Imagemagic script

Associate
Joined
1 Aug 2003
Posts
1,053
I was trying to write a script to mogrify some files but it seems to be crapping up on me, it's reached the point where I'm not sure what's going on any more. I'm getting the ' at the end of the lines but not the ' at the beginning of addresses. I'm sure it's something simple - thanks.

Any advice would be greatfully received.

rm /root/currentjpegs.txt
updatedb
locate *.j* > /root/currentjpegs.txt
locate *.J* >> /root/currentjpegs.txt
grep /usr/data/Projects/Projects\ 200 currentjpegs.txt > jpegs.txt
grep -iv therm jpegs.txt > therm.txt
sed -i 's/\/usr/mogrify\ -quality\ 75\ \''/usr/g' /root/therm.txt
sed -i 's/$/'\''/g' /root/therm.txt
 
Last edited:
I'm a bit rusty but I'll have a go. First to try and work out what you are trying to achieve:

1: clear any previous filelist
2: update locate db
3&4: find all files with a .j* or .J* ext to get all jpgs on your server and add to filelist
5: filter the file list down to jpgs stored in project directories 200* (pre 2010)
6: filter out jpgs with "therm" in the path and save the rest to therm.txt (is this right? that's what the -v switch does)
7&8: put each path in therm.txt into a mogrify command syntax

i can't see exactly how you are trying to do the last bit, but I think I know what you are trying to do. I think you may need to escape/backslash more of your forward slashes, but I would do it this way in a single sed line:

sed -i 's:.*:/usr/mogrify -quality 75 \'&\':' /root/therm.txt

(you can replace the / after the s with another character so you don't have to eascape them all in the rest of your regexp) This line just grabs the whole line and puts it into the format I think you're trying to achieve:

/usr/mogrify -quality 75 '/usr/data/Projects/Projects 2008/Proj2/images/phot01.jpg'

trouble is I can't get on my linux server right now to check, but even if it doesn't work it might put you on the right track :D
 
That's pretty much what I'm after. The final command that I'd written, (sed -i 's/$/'\''/g' /root/therm.txt) worked perfectly, it was the one that added the mogrify command and the preceeding ' that wasn't working.

Your command didn't seem to work, I'm not used to the concatenating of commands like that, I too am very rusty in sed but I always separated out my commands, it made the commands longer and some would argue messier but it's a sign of how often I make mistakes as it makes them easier to track down ;)

sed -i 's/\/usr/mogrify\ -quality\ 75\ \''/usr/g' /root/therm.txt

Definitely adds the required mogrify command, but when I modify it to

sed -i 's/\/usr/mogrify\ -quality\ 75\ \'\/usr/g' /root/therm.txt


It doesn't know what I mean, which is odd because the first instance looks wrong to me but is the one it understands.
 
Last edited:
Well.... clearly I'm exceptionally rusty with sed as I didn't realise this...

In order to shove in an ' it's not just a simple matter of \' you have to type '\' which seems odd but it works.

sed -i 's/\/usr/mogrify\ -quality\ 75\ '\''\/usr/g' /root/therm.txt

What's even more odd is that I seemed to know that when I wrote the command to input the tailing '... clearly more coffee is needed.

I don't suppose you can offer advice with the advance of the script. I will need to add a check to make sure that it doesn't endlessly try to recompress the same images if rerun. My plan is to insert a line prior to:

grep /usr/data/Projects/Projects\ 200 currentjpegs.txt > jpegs.txt

That tells it to remove all instances that appear in both currentjpegs.txt and jpegs.txt. The way that I would have thought it would be done is:

comm -3 /root/currentjpegs.txt /root/jpegs.txt

I can see how it seems messy to have all these text files kicking about, but I want them there so that they can be read and reviewed if necessary.
 
definitely more coffee required, just looking at my own code I must have been drinking something much stronger in the opposite direction. Now I see your working code, I see what you were after, and with access to actually try it out for myself can offer what I should have given you in the first place...

It's not so much concatenating commands, as just concatenating dumb strings. instead of replacing "/usr/" with "mogrify -blah /usr/" my command simply prefixes every line with "mogrify -blah ". I thought you were doing things with quotes that I should be ashamed of thinking. Anyway, here's my revised, streamlined, working line :)

sed -i 's:.*:mogrify -quality 75 &:' /root/therm.txt

Regarding comm, it should work so long as locate always returns search results in alphabetical order. I think it does, but can't find that explicitly documented anywhere.

The other thing is that if a jpeg that has been compressed is subsequently deleted, then it will appear in jpegs.txt but not currentjpegs.txt, so will be returned by the comm -3, albeit with leading spaces so you should be able to spot it and handle it.

And you will need to amend the subsequent grep line to append (>>) rather than overwrite, else you will only avoid recompressing the images on the next run, instead of evermore.
 
Back
Top Bottom