Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use sed to change file extensions?

Tags:

linux

bash

sed

I have to do a sed line (also using pipes in Linux) to change a file extension, so I can do some kind of mv *.1stextension *.2ndextension like mv *.txt *.c. The thing is that I can't use batch or a for loop, so I have to do it all with pipes and sed command.

like image 462
heythatsmekri Avatar asked Nov 28 '22 17:11

heythatsmekri


1 Answers

you can use string manipulation

filename="file.ext1"
mv "${filename}" "${filename/%ext1/ext2}"

Or if your system support, you can use rename.

Update

you can also do something like this

mv ${filename}{ext1,ext2}

which is called brace expansion

like image 171
CS Pei Avatar answered Dec 05 '22 16:12

CS Pei