0

I found a post here How can I run mogrify, but prefix the filename? which is talking about resizing a batch of images.

In it there's a for in shell script. How do I except some single file(s) while iterate most rest of them.

for image in *.png; do convert "$image" -resize 29.5% "$(dirname $image)/th_$(basename $image)"; done

For example:

I have those files in my folder (the don't have any common names like a same prefix):

example_1_some_suffix.png
some_prefix_example_2.png
example_3.png
example_4.png
example_a.png

I can use for images in *.png to iterate all, but what if I want only to do things with the first four files while leave the fifth one out?

2 Answers 2

1

You can turn on extglob to be able to specify the mask in more detail

shopt -s extglob
for image in ?(a)+(b)*.png ; do ...

If you just want skip a given file, you can do

for image in *.png ; do
    [[ $image == file-to-skip.png ]] && continue
    ...
1
  • thanks, but seems not working for the second way. I write it in a single line like: for image in *.png ; do [[ $image == file-to-skip.png ]] && continue ... is that ok? Jan 8, 2015 at 14:00
1

Assuming you have bash version 4:

declare -A exceptions=(
    [example_1_some_suffix.png]=skip
    [some_prefix_example_2.png]=skip
    [example_3.png]=skip
    [example_4.png]=skip
    [example_a.png]=skip
)
for image in *.png; do 
    [[ "${exceptions["$image"]}" == skip ]] && continue 
    # ...

Without using associative arrays:

exceptions=(
    example_1_some_suffix.png 
    some_prefix_example_2.png 
    example_3.png 
    example_4.png 
    example_a.png 
)
for image in *.png; do 
    [[ " ${exceptions[*]} " == *" $image "* ]] && continue 
    # ...

All quotes and spaces required.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .