How to rename multiple files in Linux

The following bash script renames all the files matching src_name.* with dst_name.*:

unset $1
cp $src_name.* ~/temp/
cd ~/temp/
rename "s/$src_name\.([a-z]+)/$dst_name\.\$1/" *
cd -
mv ~/temp/$dst_name.* .

If you have some files like src1.h and src1.cpp, you can save this script into file, for example, and then rename those files with src2.h and src2.cpp using the following command: src1 src2

The script requires ~/temp directory to exist and be writable.

Leave a Reply

Your email address will not be published. Required fields are marked *