title: shell script to increment file names when a directory contents changes (centos)
body: <p>I have a folder containing 100 pictures from a webcam. When the webcam sends a new picture, I want this one to replace number 0 and have all the other jpg's move up one number. I've set up a script where inotify monitors a directory. When a new file is put into this directory the script renumbers all the files in the picture directory, renames the new uploaded picture and puts it in the folder with the rest.
  This script 'sort of' works. 'Sort of', because sometimes it does what it's supposed to do and sometimes it complains about missing files:</p>
  
  <p>mv: cannot stat `webcam1.jpg': No such file or directory</p>
  
  <p>Sometimes it complains about only one file, sometimes 4 or 5. Of course I made sure all 100 files were there, properly named before the script was run. After the script is run, the files it complains about are indeed missing. 
  This is the script, in the version I tested the full paths to the directories are used of course.</p>
  
  <pre><code>#!/bin/bash
  dir1= /foo # directory to be watched
  while inotifywait -qqre modify "$dir1"; do
  cd /f002 #directory where the images are
  for i in {99..1}
  do
   j=$(($i+1))
   f1a=".jpg"
   f1="webcam$i$f1a"
   f2="test"
   f2="webcam$j$f1a"
   mv $f1 $f2 
  done
  rm webcam100.jpg
  mv dir1/*.jpg /f002/webcam0.jpg 
  done
  </code></pre>
  
  <p>I also need to implement some error checking, but for now I don't understand why it is missing files that are there.</p>