0

I'm clicking photos on a camera (Fuji x100s) that doesn't store the filenumber tag in the exif. Though it adds this information in the file name, eg, DSCF0488.JPG, DSCF0489.JPG, DSCF0490.JPG.

How do I extract this number and set it as the file number?

zsquare
  • 9,916
  • 6
  • 53
  • 87
  • Could you be more specific about the tool you are using and perhaps give an example of how it should be called? What system are you using? – Tom Fenech Aug 13 '14 at 15:40

1 Answers1

0

To extract the numbers from the files in your example, using native bash regex, you could do something like this:

for i in *.JPG; do 
    [[ $i =~ ([[:digit:]]+) ]] && echo ${BASH_REMATCH[1]}
done

Running that loop from the directory containing the files in your question would give the output:

0488
0489
0490

So if you have a tool called exif_script that can add this information to your files, you could do something like:

for photo in *.JPG; do 
    if [[ $photo =~ ([[:digit:]]+) ]]; then
        file_number="${BASH_REMATCH[1]}"
        exif_script # set number to $file_number
    fi
done

If you don't have a new enough bash to support regular expression matching, you could use sed:

for i in *.JPG; do file_number=$(echo "$i" | sed 's/[^0-9]\{1,\}\([0-9]\{1,\}\).JPG/\1/'); done

The value of $file_number will be the same as in the first piece of code but this approach should work on the vast majority of platforms.

Tom Fenech
  • 72,334
  • 12
  • 107
  • 141