This is a test version of Biostars. For the public version, visit https://www.biostars.org.
command line - how to change name of multiple files at the same time

Hi everyone, in a directory, I have several files named something like:

Ricxxx_genomic.fna_0.97_bbmap_stats.txt
Ricxxx_genomic.fna_0.97_bs.sh
Ricxxx_genomic.fna_0.97_mapped.sam
Ricxxx_genomic.fna_0.97_mapped_sorted.bam
Ricxxx_genomic.fna_0.97_mapped_sorted.bam.bai

and i would like to change their names to:

p100e_Ricxxx_genomic.fna_0.97_bbmap_stats.txt
p100e_Ricxxx_genomic.fna_0.97_bs.sh
p100e_Ricxxx_genomic.fna_0.97_mapped.sam
p100e_Ricxxx_genomic.fna_0.97_mapped_sorted.bam
p100e_Ricxxx_genomic.fna_0.97_mapped_sorted.bam.bai

What command line can i use? I have tried the mv command but i cannot find a way to make it work on all the files at the same time. i have found the below instructions but I am not sure i am doing it correctly - how does it work in my case?:

find . -depth -name "[current file name element]" -exec sh -c 'f="{}"; mv -- "$f" "${f%[current file name element]}[new file name element]"' \;

thank you for any suggestion you can give me

bash line command script

4 answers

ls Ricxxx_*|while read f;do mv $f "p100e_"$f;done. Before using the command, test it first, and then execute it after confirming that it is the desired function

thank you so much, it worked!!

TUI file browsers makes batch renaming quite easy. Here some of them:

This is a more complete list.

thank you, i will have a look. i need to learn about these!

I prefer the rename tool. I think you might need to install it with either something like apt (ubuntu), or conda (Linux, Mac, Windows), or homebrew (Mac) etc.

It's simple to use, and has a 'dry run' mode to make sure you know what it's doing. For your case:

cd /path/to/files
# dry run to make sure it's doing what you expect
rename -n "s/Ricxxx_/p100e_Ricxxx_/" *
# actual run to rename the files
rename "s/Ricxxx_/p100e_Ricxxx_/" *

Try brename, it's safe and powerful. also read this post: brename: safely batch renaming files/directories via regular expression

$ brename -p ^ -r p100e_
[INFO] main options:
[INFO]   ignore case: false
[INFO]   search pattern: ^
[INFO]      skip filters: ^\.
[INFO]   include filters: .
[INFO]   search paths: ./
[INFO] 
[INFO] checking: [ ok ] 'Ricxxx_genomic.fna_0.97_bbmap_stats.txt' -> 'p100e_Ricxxx_genomic.fna_0.97_bbmap_stats.txt'
[INFO] checking: [ ok ] 'Ricxxx_genomic.fna_0.97_bs.sh' -> 'p100e_Ricxxx_genomic.fna_0.97_bs.sh'
[INFO] checking: [ ok ] 'Ricxxx_genomic.fna_0.97_mapped.sam' -> 'p100e_Ricxxx_genomic.fna_0.97_mapped.sam'
[INFO] checking: [ ok ] 'Ricxxx_genomic.fna_0.97_mapped_sorted.bam' -> 'p100e_Ricxxx_genomic.fna_0.97_mapped_sorted.bam'
[INFO] checking: [ ok ] 'Ricxxx_genomic.fna_0.97_mapped_sorted.bam.bai' -> 'p100e_Ricxxx_genomic.fna_0.97_mapped_sorted.bam.bai'
[INFO] 5 path(s) to be renamed
[INFO] renamed: 'Ricxxx_genomic.fna_0.97_bbmap_stats.txt' -> 'p100e_Ricxxx_genomic.fna_0.97_bbmap_stats.txt'
[INFO] renamed: 'Ricxxx_genomic.fna_0.97_bs.sh' -> 'p100e_Ricxxx_genomic.fna_0.97_bs.sh'
[INFO] renamed: 'Ricxxx_genomic.fna_0.97_mapped.sam' -> 'p100e_Ricxxx_genomic.fna_0.97_mapped.sam'
[INFO] renamed: 'Ricxxx_genomic.fna_0.97_mapped_sorted.bam' -> 'p100e_Ricxxx_genomic.fna_0.97_mapped_sorted.bam'
[INFO] renamed: 'Ricxxx_genomic.fna_0.97_mapped_sorted.bam.bai' -> 'p100e_Ricxxx_genomic.fna_0.97_mapped_sorted.bam.bai'
[INFO] 5 path(s) renamed

Log in to answer this question.