How to rename file name in Linux using the command "rename"?

50 views Asked by At

I have some files that names like this:

2311110-1_1.umi_bc1.fastp.fq.gz
2311110-1_1.umi.fastp.fq.gz

Now i want to change the names to:

231110-1_1.umi_bc1.fastp.fq.gz
231110-1_1.umi.fastp.fq.gz

After searching in google, i try to use the command "rename":

rename 's/2311110/231110/' 2311110*

However, after i do this, it doesn't work. The file name doesn't change. So is there any error with the command? By the way, there are so much files that is's impossible change the name using "mv".

2

There are 2 answers

2
Mathieu On

If you use bash, you can use variable substitution:

for i in 2311110*
do
    # just remove the echo when you tested that proposed mv command seems good
    echo mv "$i" "${i/2311110/231110}"
done

See devhints.io/bash to learn more about variable manipulation.

0
pts On

On Ubuntu Linux, with the rename package installed (sudo apt-get install rename), your rename command works for me:

$ touch 2311110-1_1.umi_bc1.fastp.fq.gz 2311110-1_1.umi.fastp.fq.gz
$ ls 23111*.gz
2311110-1_1.umi.fastp.fq.gz  2311110-1_1.umi_bc1.fastp.fq.gz
$ rename 's/2311110/231110/' 2311110*
$ ls 23111*.gz
231110-1_1.umi.fastp.fq.gz  231110-1_1.umi_bc1.fastp.fq.gz

If it doesn't work for you, then use rename -v ..., and check the error message it prints.

FYI It's possible to do automated renames likes this without the rename command (see comments and other answers), with the advantage of working on systems where rename is not installed. Most Linux systems don't have rename installed by default.