Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One liner to rename bunch of files

Tags:

file

linux

rename

I was looking for a linux command line one-liner to rename a bunch of files in one shot.

pattern1.a  pattern1.b pattern1.c ...

Once the command is executed I should get

pattern2.a  pattern2.b pattern2.c ...
like image 915
Jean Avatar asked Mar 26 '12 17:03

Jean


People also ask

Is there a way to rename a bunch of files at once?

To rename multiple files from File Explorer, select all the files you wish to rename, then press the F2 key. The name of the last file will become highlighted. Type the new name you wish to give to every file, then press Enter.

What is the fastest way to rename a bunch of files?

You can press and hold the Ctrl key and then click each file to rename. Or you can choose the first file, press and hold the Shift key, and then click the last file to select a group.


2 Answers

for i in pattern1.*; do mv -- "$i" "${i/pattern1/pattern2}"; done

Before you run it, stick an echo in front of the mv to see what it would do.

like image 200
Kerrek SB Avatar answered Sep 21 '22 16:09

Kerrek SB


If you happen to be using Linux, you may also have a perl script at /usr/bin/rename (sometimes installed as "prename") which can rename files based on more complex patterns than shell globbing permits.

The /usr/bin/rename on one of my systems is documented here. It could be used like this:

rename "s/pattern1/pattern2/" pattern1.*

A number of other Linux environments seem to have a different rename that might be used like this:

rename pattern1 pattern2 pattern1.*

Check man rename on your system for details.

like image 31
ghoti Avatar answered Sep 21 '22 16:09

ghoti