Rename many files to unique names in different directories
1
1
Entering edit mode
4.9 years ago
aebartl2 ▴ 10

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 • 1.7k views
ADD COMMENT
2
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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

ADD REPLY
3
Entering edit mode
4.9 years ago
JC 13k

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
ADD COMMENT

Login before adding your answer.

Traffic: 1825 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6