views:

91

answers:

1

I have JPG files numbered 3006-3057 that I would like to reverse number. I would be content renaming them by adding a backwards number count to the beginning of the name: img_3006.jpg > 99_img_3006.jpg and img_3057.jpg > 48_img_3057.jpg.

+1  A: 
basenumber=9999
for file in *.jpg
do
  base="${file%.*}"
  filenumber="${base#*_}"
  mv "$file" "$((basenumber-filenumber))_$file"
done
Ignacio Vazquez-Abrams
Thanks for the idea. Any thoughts on getting this to work:for i in $(ls *JPG); do echo "4000 - `echo $i | grep -o [0-9]*`" | bc ; done
Donnied
The script above renames all the files 9999. The cognitive block I'm having is thinking of the script that doesn't number down all the way for each file. The seems like it should be a five minute hack but I'm just seeing popping items on a stack.
Donnied
The script will fail if there's more than one `_` or if there isn't a number between the `_` and the `.`. The first can be fixed by replacing the `#` in line 5 with `##`. The second, well, that can't be fixed.
Ignacio Vazquez-Abrams
Once I removed leading identification from the file names things worked fine.for i in *JPG; do mv $i `echo $i|grep -o '[0-9]*.JPG'`; done
Donnied