title: Recursively renaming folders and files to sub-directory name and moving files
body: <p>I have several sub-directories with no unanimous naming pattern within my home directory (for example <code>~/123</code>, <code>~/456</code>, <code>~/789</code>).</p>
  
  <p>Within each of these sub-directories, I have two folders named <code>alignment1</code> and <code>alignment2</code>. In the folders <code>alignment1</code> and <code>alignment2</code> there are several files.</p>
  
  <p>The files of interest to me are named <code>alignment1</code> (no extension) in the <code>alignment1</code> folder and the file <code>alignment2</code> (no extension) in the <code>alignment2</code> folder.</p>
  
  <p>Please remember that in the folders <code>alignment1</code> and <code>alignment2</code> there are other files named <code>alignment</code>, <em>but</em> they have extensions (for example, <code>alignment1.backbone</code>, <code>alignment1.bbcol</code> and <code>alignment2.backbone</code>, <code>alignment2.bbcol</code> in respective <code>alignment1</code> and <code>alignment2</code> folders), but I am <em>not</em> interested in these files.</p>
  
  <pre><code>~/123/alignment1/alignment1
  ~/123/alignment2/alignment2
  ~/456/alignment1/alignment1
  ~/456/alignment2/alignment2
  etc...
  </code></pre>
  
  <p><strong>Question:</strong></p>
  
  <ol>
  <li><p>My struggle is to rename the folders <code>alignment1</code> and <code>alignment2</code> to <code>subdirectory_alignment1</code> and <code>subdirectory_alignment2</code> (for example, <code>123_alignment1</code> and <code>123_alignment2</code>).</p></li>
  <li><p>Then, in the folders <code>alignment1</code> and <code>alignment2</code>, the alignment files, named <code>alignment1</code> and <code>alignment2</code> respectively, need to be renamed to <code>subdirectory_1.aln</code> and <code>subdirectory_2.aln</code>.</p></li>
  <li><p>Move the <code>subdirectory_1.aln</code> and <code>subdirectory_2.aln</code> to home directory.</p></li>
  </ol>
  
  <p>I think it is closely related to <a href="https://stackoverflow.com/questions/14306117/rename-files-in-multiple-directories-to-the-name-of-the-directory">this</a> and <a href="https://stackoverflow.com/questions/16541582/finding-multiple-files-recursively-and-renaming-in-linux">this</a> question, but I have been trying to amend the answers in the above postings for last few hours with no success.</p>