Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Names of Multiple Files Linux

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?

like image 444
Terry Li Avatar asked Aug 08 '11 17:08

Terry Li


3 Answers

without any extra software you can:

for FILE in *1.txt; do mv "$FILE" $(echo "$FILE" | sed 's/1/2/'); done
like image 194
Michał Šrajer Avatar answered Oct 19 '22 16:10

Michał Šrajer


for f in {a..c}1.txt; do echo "$f" "${f/1/2}"; done

replace 'echo' with 'mv' if the output looks correct.

and I want to replace "apple" with "linux"

for f in *apple*; do mv "$f" "${f/apple/linux}"; done

The curly brackets in line 1 should work with bash at least.

like image 4
user unknown Avatar answered Oct 19 '22 17:10

user unknown


The following command will rename the specified files by replacing the first occurrence of 1 in their name by 2:

rename 1 2 *1.txt
like image 2
dogbane Avatar answered Oct 19 '22 15:10

dogbane