title: Rename and increment on duplicate
body: <p>I have a series of image files with filename formats as follows.</p>
  
  <pre><code>9407-C-9406-B-038.jpg
  9407-C-9406-B-118.jpg
  9422-AC-012.jpg
  9422-AC-112.jpg
  9422-BD-043.jpg
  9422-BD-Still-001.jpg
  9405_M.jpg
  9792A.jpg
  9792B.jpg
  </code></pre>
  
  <p>The relevant portion for my purpose is the first 4 characters, the rest is irrelevant.</p>
  
  <p>I'd like to rename the files such that the leading 4 char string is retained, the extension is retained, but the rest is incremented when a duplicate is encountered : 
  e.g :</p>
  
  <pre><code>9422-AC-012.jpg becomes 942200.jpg
  9422-AC-112.jpg becomes 942201.jpg
  9422-BD-043.jpg becomes 942202.jpg
  9422-BD-Still-001.jpg becomes 942203.jpg
  9405_M.jpg becomes 940500.jpg
  9792A.jpg becomes 979200.jpg
  9792B.jpg becomes 979201.jpg
  </code></pre>
  
  <p>Using 'rename' I can strip the string after the first 4 chars, and I can increment in totality, which results with the incremental portion of the filename ending up in the thousands. </p>
  
  <p><code>rename -n  -N 0001 's/(?&lt;=.{4}).*/$N.jpg/' *.jpg</code></p>
  
  <p>Can anyone suggest a way to strip the filename after the first 4 chars, rename the file, and increment only when a duplicate is encountered?</p>