Changing More Than One File’s name in Linux

Hi everyone,

Changing the name of the file is a normal situation in your daily life. It is easy, just right click then choose the rename then change it or you can use the command mv old-file-name new-file-name in the Linux environment.

What if I need to change the name of more than one file at the same time, add a postfix, or prefix them? At that point, it’s not going to be easy to change the name of the files.

Yes, if you are a Linux/Unix user, the situation we have might not be a problem.

Let’s see how to add a postfix or prefix in your files in Linux.

The first step to rename of the file is finding all files you want to change.

The find command is enough to find the files. Then, you need to change the name of the file that you find with the mv command.

The below command will search for the given name of the file, after searching, it will execute the mv command to change the current filenames to the new ones.

For example, assume that we have files of a name beginning with test-file like test-file1.c, test-file2.c, test-file3.c, test-file4.c . Then we would like to change the names of files from .c to .cpp .

find . -depth -name "*.c" -exec sh -c 'f="{}"; mv -- "$f" "${f%.c}.cpp"' \;

The below image contains the files, which is ending with .c that we would like to change.

After running the command, you will see the changes in the file extensions like the below image.

If you would like to see more information about find and mv command in Linux, please take a look into the man find or man mv in the Linux terminal.

I hope you enjoy reading.

Have a nice coding.