Change Names of Multiple Files Linux

Terry Li picture Terry Li · Aug 8, 2011 · Viewed 19.5k times · Source

I have a number of files with names a1.txt, b1.txt, c1,txt...on ubuntu machine.

Is there any quick way to change all file names to a2.txt, b2.txt, c2.txt...?

In particular, I'd like to replace part of the name string. For instance, every file name contains a string called "apple" and I want to replace "apple" with "pear" in all file names.

Any command or script?

Answer

Michał Šrajer picture Michał Šrajer · Aug 8, 2011

without any extra software you can:

for FILE in *1.txt; do mv "$FILE" $(echo "$FILE" | sed 's/1/2/'); done