How to Rename Files in Linux using Terminal?


rename files in linux

This post may contain affiliate links/ads and I may earn a small commission when you click on the links/ads at no additional cost to you. As an Amazon Affiliate, I earn from qualifying purchases. Techsphinx also participates in the StationX Affiliate program. You can read my full disclaimer here.

In this tutorial, I will show you how to rename files in Linux using Terminal.

Renaming any single file is easy in Linux using a terminal but renaming multiple files at once can give you a headache.

Don’t worry, I will show you how to rename both single and multiple files easily using the mv and rename command.

Note: mv and rename commands can be used to rename both files and directories (folders). In my other post, I have shown how you can use them to rename directories in Linux.

Rename Files in Linux Using the mv Command

The mv command is officially made for moving files, but when it moves a file from one location to another, you can give the file a new name.

So, it also works as a utility to rename files.

Here’s the basic syntax of the mv command:  

mv <source> <destination>

mv: Name of the command.

source: Name of the source file/files. Yes! The source can be single or multiple files or even directories (folders).

destination: The destination can be a single file or directory. If you specify multiple files as source, then the destination must be a directory so that all the source files are moved into the destination directory.

Also, if you specify a single file as the source and the destination is an existing directory, then the source file is moved to that directory.

So, to rename a file, you need to specify a single file as a source and make sure no directory exists with the new name you want to give to your source file.

Now, let’s see mv in action.

Rename a Single file using the mv Command

To rename “file.jpg” to “hello.jpg” use the mv command followed by the source and destination file names separated by a space.

mv file.jpg hello.jpg

The above command will rename the file.jpg to hello.jpg.

If the file is in a different location, you can provide a full path in the mv command.

mv /home/user/Desktop/file.jpg  /home/user/Desktop/hello.jpg

Rename Multiple Files in Linux using mv Command

There is no way to rename multiple files using only the mv command. As I said it was not made as a renaming tool, to begin with.

If you know for loops, then you can use that in conjunction with the mv command to rename multiple files.

for i in *.jpg; do mv -- "$i" "${i%.jpg}.png"; done

for i in *.jpg: For every file that ends with “.jpg” extension in the current directory.

do mv — “$i” “${f%.jpg}.png;”: Run mv command and change the extension to “.png” for every “.jpg” file.

done: It indicates the end of the loop.

using for loop to rename files in linux

Basically, it doing:

mv file1.jpg file1.png

for each and every file in the current directory (with “.jpg” extension) in loop.

Rename Files in Linux using the rename Command

The mv command is a handy tool to rename single files, but if you want to rename multiple files, then you have to use a tool that is specifically made for this.

Rename command is used to rename multiple files. This command requires basic knowledge of regular expressions.

First, you have to install the rename command on your Linux distro.

Note: There are 2 versions of rename command available and they both have different syntax. I will demonstrate the Perl version of rename command.

Install Rename command in Linux

To Install Rename on Ubuntu

sudo apt install rename

Install Rename on CentOS / RHEL / Fedora

sudo dnf install prename

(Yes! its prename, where p stands for Perl.)

Install Rename on Arch Linux / Manjaro Linux

sudo pacman -Syu perl-rename

Now, the rename command is installed let’s have a look at its basic syntax.

rename <Options> <Perlexpr> <Files>

rename: Name of the command.

Options: The options you can use with rename command.

Perlexpr: Regular expression in Perl syntax.

Files: The files to rename.

If you want to know about Perl regular expressions, then you can check the perldoc.

Rename command usage with examples

Now, let’s see rename command in action.

Changing file extensions

rename 's/.jpg/.png/'  *.jpg

The above command will change the extensions of every “.jpg” file in the current location to “.png”.

Changing names of the files

Instead of changing extensions, you can also change part of filenames.

rename 's/file/hello/' *.jpg

The above command will change “file1.jpg” to “hello1.jpg” for all files with the “.jpg” extension in the current location.

renaming files in Linux using rename command

Print name of the files to be renamed, without actually renaming them.

rename -n 's/.jpg/.png/' *.jpg

output:

rahul@techsphinx:~/Desktop/myfolder$ rename -n 's/.jpg/.png/' *.jpg
rename(file1.jpg, file1.png)
rename(file2.jpg, file2.png)
rename(file3.jpg, file3.png)

This a good way to check beforehand that everything is going to work as expected.

If everything is ok, then run the same command again without the “-n” option.

Changing other parts of the filename.

You can also change parts of file names using:

rename 's/file_/image_/' *.jpg

This will change “file_1.jpg” to “image_1.jpg” for every file with the “.jpg” extension in the current location.

changing parts of filename using rename command

Deleting Part of a Filename

You can also delete part of the filename using:

rename 's/image_//' *.jpg

The above command will remove the part mentioned for every file containing the “.jpg” extension. In this case, it will remove “image_” from “image_1.jpg” leaving only “1.jpg” as a new file name.

removing parts of filenames using rename command

Renaming and overwriting

By default, rename command doesn’t overwrite the files. For example, if you want to rename 1.png to 1.jpg and 1.jpg already exists then the rename command will output the following:

rahul@techsphinx:~/Desktop/myfolder$ rename 's/.png/.jpg/' *.png
1.png not renamed: 1.jpg already exists
2.png not renamed: 2.jpg already exists
3.png not renamed: 3.jpg already exists

You can use the “-f” option to rename and overwrite the existing files.

rename -f 's/.png/.jpg/' *.png
force overwriting when renaming files in Linux

Replace spaces in filenames with underscores or hyphens.

You can also remove spaces from file names and replace them with underscore or hyphens.

To replace space with underscores run:

rename 'y/ /\_/' *
replace space with underscores

To replace space with hyphens run:

rename 'y/ /\-/' *
replace space with hyphens

Note: The above commands will remove space and replace it with underscore or hyphens for every file and directory (folder) present in the current location.

If you don’t want that, then instead of doing it for all files (*), filter the results (*.jpg for example).

Convert filenames to uppercase

You can easily convert file names to uppercase using:

rename 'y/a-z/A-Z/' *
changing filenames to uppercase

Note: (*) means all files and directories present in the current location.

Convert filenames to lowercase

You can also convert file names to lowercase using:

rename 'y/A-Z/a-z/' *
changing filenames to lowercase

Note: (*) means all files and directories present in the current location.

Rename with Grouping

If you want to rename files that have similarity (but not identical) in their filenames, then instead of renaming them separately, you can rename them together using a technique called grouping.

Let’s take an example of two files – “fighter.jpg” and “might.jpg”.

Now, I want to rename them to “lighter.jpg” and “light.jpg”. This means I have to replace both “fight” and “might” with “light”. In this case, normal search substitution won’t work.

The words fight and might are similar to spell, the only difference is “f” and “m”

So, to rename these types of similar filenames, you can do something like this:

rename 's/(f|m)ight/light/' *.jpg
renaming files with grouping

Using grouping, you can rename similar filenames without the need of renaming them separately.  

There are many other options you can use with rename command. Check the manual of rename for the same.

man rename

Conclusion

Renaming multiple files in Linux using a terminal is not an easy feat.

There are many other tools available to rename files in Linux using terminal like – mmv, renameutils, vimv to name a few.

I recommend you also check my other guide, where I demonstrated renaming directories in Linux using the terminal.

I hope you got to learn something new today, if you have any suggestions or feedback, feel free to leave a comment.

If you like this post, then follow Techsphinx on Facebook and Twitter for more reviews, tricks, tips and tutorials.

This article needs update or correction? Report the issue here so I can update it.


Like it? Share with your friends!

Rahul R Nair

Rahul is obsessed with technology and electronic devices. He is also the founder of TechSphinx. Being a technophile, he is always busy doing some techy stuff or learning about the latest technologies. When not busy with his usual routine (staring at the computer screen) he likes to write and share his knowledge with the world.
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x