anvd
anvd

Reputation: 4047

Resize only images with a specific pattern

I have a directory with some blog images. Now I am looking for a way of doing a bulk resize only for the images with a generated uuid. They have a fixed structure so it seems like a good pattern.

How can I resize only these files excluding all the other?

mogrify -resize 600 *.png


eeafc286-ac2c-4bed-9e9b-d7d3c55e5965.png
ef0318d2-d3e2-42c2-a45e-8ededbb947ed.png
efcd681e-2c12-4a5a-ac77-5d6bf0a76810.png
error.png
f35a14cf-4b3d-4fc3-a8d3-6ea59a059a36.png
f4b60929-47c3-4b56-9486-e5efd62dc2e8.png
f6b3c4bd-f5ba-4d1d-96dd-6c61d5444a03.png
f76e04a3-75f4-4139-b1c9-080fe1e9fea4.png
fc141aa9-1d49-401f-a38a-734f7b0c142f.png
fdfff9df-2dab-4110-bd2f-b65635a5cb21.png
john.jpg
site_1.png
site_2.png
tech.jpg

Upvotes: 1

Views: 161

Answers (4)

Kyle Banerjee
Kyle Banerjee

Reputation: 2784

There are a million ways to do this. The safest is to just ls |grep "yourpattern" > toprocess verify the files are right and then cat toprocess| while read line; do mogrify -resize 600 $line"; done

Upvotes: 0

el-teedee
el-teedee

Reputation: 1341

I would try to use this approach:

  • compute the best regular expression that matches exactly the files you want
  • use it as find argument to first find the files to modify
  • then, iterate on file list and call mogrify

Sample squeleton would look like this:

files=$( find -regexp "$pattern" )
for file in "$files"; do
  mogrify "$file" ;
done

Or even shorter with the find -exec:

# Note: not tested, valid mogrify arguments must be added/handled
# call mogrify for each 'find' results
find -regexp "$pattern" -exec mogrify {} +;
# call mogrify in a subshell for each 'find' results, for finest argument handling
find -regexp "$pattern" -exec sh -c 'mogrify "$@"' {} +;

Where you will have to "play with argument passing and order" to make the command work.

Note1: this answer is not about the "what best pattern matches your need" question, but about more general approach of "finding custom files and executing a command on them"
Note2: my answer does not contain tested and verified commands, but instead an approach using find and find -exec.

(constructive) feedbacks are welcome.

Upvotes: 0

that other guy
that other guy

Reputation: 123480

I would probably have gone with *-*-*-*-*png, but if you want to be specific you could do a coarse match with a glob and a finer match with regex:

for file in *.png
do
    [[ $file =~ ^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}\.png$ ]] || continue
    mogrify -resize 600 "$file"
done

Upvotes: 1

blhsing
blhsing

Reputation: 106588

If you want to use file globbing to match the exact pattern, you can do:

mogrify -resize 600 [0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f]-[0-9a-f][0-9a-f][0-9a-f][0-9a-f]-[0-9a-f][0-9a-f][0-9a-f][0-9a-f]-[0-9a-f][0-9a-f][0-9a-f][0-9a-f]-[0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f][0-9a-f].png

Upvotes: 0

Related Questions