text: user16067: I have a lot of FASTA files that I recently scaffolded with RagTag. The output of RagTag includes a directory for each sample and the corresponding FASTA file written as "ragtag.scaffold.fasta"
  I would like to go through each file and rename it like so:
  Original Path with File name:
  directoryName_ID/ragtag.scaffold.fasta
  
  Changed File name:
  directoryName_ID/ID_ragtag.scaffold.fasta
  
  As you can see, the directory name includes the ID number I would like to use to rename the fasta file with (along with an underscore to space it out). Is there a way to loop through all of the directories, using that last number after the "_" in the directory name and to use that to rename my fasta file?
  Here is what I have tried so far:
  for filename in ./directory*/ragtag.scaffold.fasta; do 
      mv filename ${./directory%*_*}_ragtag.scaffold.fasta; 
  done
  
  It doesn't work at all but I know I need to use mv to rename the filename. However, when it comes to referencing the directory name the file is located in I get lost. I also have a .txt file that contains a list of IDs if that might be easier to reference. I'm still kinda new to the command line so if anyone could help that would be greatly appreciated!
  
  user14180: If you want to use Perl:
  #!/usr/bin/perl
  use 5.030;
  use warnings;
  
  use File::Find::Rule qw( );
  
  for my $file (File::Find::Rule->name("ragtag.scaffold.fasta")->in(".")){
    my $newfile = $file;
    $newfile =~ s{(.+_(\d+)/)(.+.fasta)}{$1$2_$3};
  
    if (-e $newfile) {
      warn "can't rename $file to $newfile: $newfile exists\n";
    }elsif (rename $file, $newfile) {
      # do nothing
    }else{
      warn "rename $file to $newfile failed: $!\n";
    }
  }
  
  
  user37: Perl one-liner:
  ls */filename | perl -lane 'print "mv $1/$3 $1/$2_$3" if /(\S+_([^\/_\s]+))\/(\S+)/'
  
  
  user3848: Just execute the following immediately above the directories of interest, (post edited to include OPs "constant term")
  for d in *; do 
    mv "$d"/ragtag.scaffold.fasta 
   "$d/${d/000mergedCluster_/}_ragtag.scaffold.fasta";
  done
  
  The key thing I assumed is that the directory name comprises a "constant term" and is suffixed by a "variable term".
  Just to note the $d here is directory not file name - because that is constant in each directory.
  
  Comments...
  $d/${d/000mergedCluster_/}
  
  The key part of the code is above.
  What its doing is saying from the variable $d delete the term 000mergedCluster_ (or whatever the "constant term" is)
  
  user10775: Assuming the output is only a list of directories and each directory includes a file called "ragtag.scaffold.fasta", you could use brace expansion to generate the SOURCE and DEST strings:
  for i in directoryName_* ; do
      echo mv "${i}"/{,"${i#directoryName_}"_}ragtag.scaffold.fasta
  done
  
  But the above will only work using Bash and Zsh, and maybe some other shells. Without Bash/Zsh, just use:
  fasta="ragtag.scaffold.fasta"
  for i in directoryName_* ; do
      echo mv "${i}/${fasta}" "${i}/${i#directoryName_}_${fasta}"
  done
  
  Remove the echo to perform the rename.
  
original_text: 16067: <p>I have a lot of FASTA files that I recently scaffolded with RagTag. The output of RagTag includes a directory for each sample and the corresponding FASTA file written as &quot;ragtag.scaffold.fasta&quot;</p>
  <p>I would like to go through each file and rename it like so:</p>
  <pre><code>Original Path with File name:
  directoryName_ID/ragtag.scaffold.fasta
  
  Changed File name:
  directoryName_ID/ID_ragtag.scaffold.fasta
  </code></pre>
  <p>As you can see, the directory name includes the ID number I would like to use to rename the fasta file with (along with an underscore to space it out). Is there a way to loop through all of the directories, using that last number after the &quot;_&quot; in the directory name and to use that to rename my fasta file?</p>
  <p>Here is what I have tried so far:</p>
  <pre><code>for filename in ./directory*/ragtag.scaffold.fasta; do 
      mv filename ${./directory%*_*}_ragtag.scaffold.fasta; 
  done
  </code></pre>
  <p>It doesn't work at all but I know I need to use mv to rename the filename. However, when it comes to referencing the directory name the file is located in I get lost. I also have a .txt file that contains a list of IDs if that might be easier to reference. I'm still kinda new to the command line so if anyone could help that would be greatly appreciated!</p>
   14180: <p>If you want to use Perl:</p>
  <pre class="lang-pl prettyprint-override"><code>#!/usr/bin/perl
  use 5.030;
  use warnings;
  
  use File::Find::Rule qw( );
  
  for my <span class="math-container">$file (File::Find::Rule-&gt;name("ragtag.scaffold.fasta")-&gt;in(".")){
    my $newfile = $file;
    $newfile =~ s{(.+_(\d+)/)(.+.fasta)}{$1$2_$</span>3};
  
    if (-e <span class="math-container">$newfile) {
      warn "can't rename $file to $newfile: $newfile exists\n";
    }elsif (rename $</span>file, <span class="math-container">$newfile) {
      # do nothing
    }else{
      warn "rename $file to $newfile failed: $</span>!\n&quot;;
    }
  }
  </code></pre>
   37: <p>Perl one-liner:</p>
  <pre class="lang-sh prettyprint-override"><code>ls */filename | perl -lane 'print &quot;mv $1/$3 $1/$2_$3&quot; if /(\S+_([^\/_\s]+))\/(\S+)/'
  </code></pre>
   3848: <p>Just execute the following immediately above the directories of interest, (<strong>post edited</strong> to include OPs &quot;constant term&quot;)</p>
  <pre><code>for d in *; do 
    mv &quot;<span class="math-container">$d"/ragtag.scaffold.fasta 
   "$</span>d/${d/000mergedCluster_/}_ragtag.scaffold.fasta&quot;;
  done
  </code></pre>
  <p>The key thing I assumed is that the directory name comprises a &quot;constant term&quot; and is suffixed by a &quot;variable term&quot;.</p>
  <p>Just to note the <code>$d</code> here is directory not file name - because that is constant in each directory.</p>
  <hr />
  <p>Comments...</p>
  <pre><code><span class="math-container">$d/$</span>{d/000mergedCluster_/}
  </code></pre>
  <p>The key part of the code is above.</p>
  <p>What its doing is saying from the variable <code>$d</code> delete the term <code>000mergedCluster_</code> (or whatever the &quot;constant term&quot; is)</p>
   10775: <p>Assuming the output is only a list of directories and each directory includes a file called &quot;ragtag.scaffold.fasta&quot;, you could use <a href="https://www.gnu.org/software/bash/manual/html_node/Brace-Expansion.html" rel="nofollow noreferrer">brace expansion</a> to generate the SOURCE and DEST strings:</p>
  <pre><code>for i in directoryName_* ; do
      echo mv &quot;${i}&quot;/{,&quot;${i#directoryName_}&quot;_}ragtag.scaffold.fasta
  done
  </code></pre>
  <p>But the above will only work using Bash and Zsh, and maybe some other shells. Without Bash/Zsh, just use:</p>
  <pre><code>fasta=&quot;ragtag.scaffold.fasta&quot;
  for i in directoryName_* ; do
      echo mv &quot;${i}/${fasta}&quot; &quot;${i}/${i#directoryName_}_${fasta}&quot;
  done
  </code></pre>
  <p>Remove the <code>echo</code> to perform the rename.</p>
  