title: Shell script to rename multiple files from their parent folders name
body: <p>I have a file structure like  this:</p>
  
  <ul>
  <li>00000010
  <ul>
  <li>000000001.file1</li>
  <li>000000001.file2</li>
  </ul></li>
  <li>00000020
  <ul>
  <li>00000003.file1</li>
  <li>00000003.file2</li>
  <li>00000003.file3</li>
  </ul></li>
  <li>...</li>
  </ul>
  
  <p>So there are folders with 8-digit names containing one ore more files with name starting with 8-digit numbers. But theses filenames are – let's say – out of sync. So Now I try to rename them recursively in bash to archive:</p>
  
  <ul>
  <li>00000010
  <ul>
  <li>000000010.file1</li>
  <li>000000010.file2</li>
  </ul></li>
  <li>00000020
  <ul>
  <li>00000020.file1</li>
  <li>00000020.file2</li>
  <li>00000020.file3</li>
  </ul></li>
  <li>...</li>
  </ul>
  
  <p>My script does look like:</p>
  
  <pre><code>#! /bin/bash
  
  find * -maxdepth 1 -name "*" -type d | while read -r dir
  do
          rename 's/$dir\/[0-9]{8}/$dir/' *
  done
  </code></pre>
  
  <p>But this is not working and gives errors like</p>
  
  <blockquote>
    <p>Global symbol "$dir" requires explicit
    package name at (eval 1) line 1.</p>
  </blockquote>
  
  <p>How could I write it to rename the files according to their folder names?</p>
  
  <p>Thank you for help!</p>