Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recursively change file extensions in Bash

I want to recursively iterate through a directory and change the extension of all files of a certain extension, say .t1 to .t2. What is the bash command for doing this?

like image 635
Amal Antony Avatar asked Feb 24 '14 10:02

Amal Antony


People also ask

How do I change the extension of multiple files in Linux?

The most common way to change file extensions recursively in a directory is to use a bash for loop. We can prompt the user to enter the target directory, old extension, and the new extension to rename using a bash script.


1 Answers

Use:

find . -name "*.t1" -exec bash -c 'mv "$1" "${1%.t1}".t2' - '{}' + 

If you have rename available then use one of these:

find . -name '*.t1' -exec rename .t1 .t2 {} + 
find . -name "*.t1" -exec rename 's/\.t1$/.t2/' '{}' + 
like image 152
anubhava Avatar answered Sep 17 '22 13:09

anubhava