This is a test version of Biostars. For the public version, visit https://www.biostars.org.
Rename many files to unique names in different directories

I have a large number of directories with unique names. Inside each directory is a contigs.fasta file. I need to rename the contigs.fasta file to include the unique directory name so that if I combine all of the contigs.fasta files I will be able to determine where each originated. For example some of the directories are listed below:

Cage1.out_bin.100_Reassembly.Spades  Cage1.out_bin.125_Reassembly.Spades  Cage1.out_bin.27_Reassembly.Spades  Cage1.out_bin.51_Reassembly.Spades  Cage1.out_bin.76_Reassembly.Spades
Cage1.out_bin.101_Reassembly.Spades  Cage1.out_bin.126_Reassembly.Spades  Cage1.out_bin.28_Reassembly.Spades  Cage1.out_bin.52_Reassembly.Spades  Cage1.out_bin.77_Reassembly.Spades
Cage1.out_bin.102_Reassembly.Spades  Cage1.out_bin.127_Reassembly.Spades  Cage1.out_bin.29_Reassembly.Spades  Cage1.out_bin.53_Reassembly.Spades  Cage1.out_bin.78_Reassembly.Spades

The contents of one directory is listed below: assembly_graph.fastg before_rr.fasta contigs.paths dataset.info K33 K77 misc scaffolds.fasta spades.log assembly_graph_with_scaffolds.gfa contigs.fasta corrected input_dataset.yaml K55 K99 params.txt scaffolds.paths tmp

How do I rename the contigs.fasta file for all the directories?

assembly genome sequencing linux

For questions such as this which are more unix focused you should always check StackExchange/Stackoverflow. Here is one potential answer from Stackoverflow.

with gnu-parallel:

 find . -type f -name "contigs.fasta" -printf '%P\n' | parallel --dry-run cp {//}/contigs.fasta {//}/{//}_contigs.fa

This would make a copy of the existing file and the new file will have .fa extension. Remove --dry-run to execute the command.

You can also try this. Go to parent directory and run this command:

 rename -nv  's/(.*)\/(.*)/$1\/$1_$2/' */contigs.fasta

Remember this would rename all the files. Take a backup of your files. This function would dry-run the code. Remove -nv before you run the code.

1 answer

you can use Bash for this:

#!/bin/bash

for DIR in .
do
    if [ -e "$DIR/contigs.fasta" ]
    then
        mv "$DIR/contigs.fasta" "$DIR/$DIR.contigs.fasta"
    fi
done

Log in to answer this question.