Odd and even file names

Hello,

I want to sort/identify 600 files according to odd or even numbers in the files names. How can I do this?

The goal is to perform different ImageMagick operations based on even or odd numbers in the file names. The file names have this pattern: bdf0001.tif, bdf0044.tif and bdf0136.tif

I have all the ImageMagick code together but don't know how to recognise or sort the files according to odd or even file names. The sorting can be part of the ImageMagick script or I can alternatively separate these steps: first sort the files and move them to different directories. Then do the image processing part and put the files back together manually.

Any help or suggestions are greatly appreciated,
Gargan

Are you looking for something like this ?

ls |  sed -n '/.*[02468]\.tif/p' | sort    # files with even numbers
ls |  sed -n '/.*[13579]\.tif/p' | sort    # files with odd numbers

An example how to move the file to the directories /dir/odd and /dir/even:

ls | awk '{s=$0;gsub(/[a-zA-Z]/,"",s);n=s%2?"odd":"even";print "mv "$0 " /dir/" n}'

If the output is correct you can pipe the output to sh to perform the action:

ls | awk '{s=$0;gsub(/[a-zA-Z]/,"",s);n=s%2?"odd":"even";print "mv "$0 " /dir/" n}' | sh

To relocate:

mkdir even odd; mv *[02468].tif even; mv *[13579].tif odd

For a list of each:

for f in *[02468].tif; do echo "$f"; done
for f in *[13579].tif; do echo "$f"; done

A test whose exit status can be used to decide on a further course of action (filenames without a digit are treated as if they contained a 0):

for f in *; do
    if [ "$(expr $(echo 0$f | tr -cd 0-9) % 2)" -eq 0 ]; then
        echo even
    else
        echo odd
    fi
done

Regards,
Alister

Responses have come in quicker than I can login! Thank you all, problem is solved!