title: Bash: Moving multiple files into subfolders
body: <p>I have a folder with a couple thousand files and I want to move them into subfolders according to a string in the filename. The files all have a structure like</p>
  
  <p>something-run1_001.txt</p>
  
  <p>something-run22_1243.txt</p>
  
  <p>So I tried the following script in order to move all files with "run1" in it into a subfolder r1 and all "run22" files in a subfolder r22 (and so on) but it does no work that way and I get a message "File X is the same as file X". </p>
  
  <pre><code>#!bin/bash
  
  for i in {1..39}
  do
  foldername=r$i
  #echo "$foldername"
  mkdir $foldername
  find . -type f -name "*run$i_*" | xargs -i mv {} $foldername/ 
  done
  </code></pre>
  
  <p>How to solve this?</p>