command line - how to change name of multiple files at the same time
4
0
Entering edit mode
14 months ago
tiziana • 0

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 • 1.2k views
ADD COMMENT
4
Entering edit mode
14 months ago
Lian ▴ 40

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

ADD COMMENT
0
Entering edit mode

thank you so much, it worked!!

ADD REPLY
1
Entering edit mode
14 months ago
barslmn ★ 2.1k

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

This is a more complete list.

ADD COMMENT
0
Entering edit mode

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

ADD REPLY
1
Entering edit mode
14 months ago
cfos4698 ★ 1.1k

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_/" *
ADD COMMENT
0
Entering edit mode
14 months ago

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

Login before adding your answer.

Traffic: 2931 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