How To Rename File In Linux?

Renaming files is one of the most popular daily operations in Linux administration. Files can be renamed using different methods and tools like command-line interface, GUI, File Manager, etc. A renaming file changes a file name into a new one where the old file name becomes absolute after the file rename operation. In this tutorial, we examine file rename operation in Linux using different commands like mv, rename, and GUI method.

Rename File with mv Command

The mv command is created to move files. But mv command can be also used to rename files as moving a file with a different name at the same path can be called renaming. The mv command comes from all Linux distributions by default. The mv command has the following simple syntax.

mv OPTION OLD_NAME NEW_NAME
  • OPTION is single or more options related with the renaming.
  • OLD_NAME is the old or current name which converted to the NEW_NAME.
  • NEW_NAME is the new name which will be set.

In the following example, we rename the file named “cities.txt” into the “mycities.txt”.

$ mv cities.txt mycities.txt

Alternatively, we can rename a file by changing its directory or current path. In the following example, we change the renamed file path into the “/home/ismail/Downloads”.

$ mv cities.txt /home/ismail/Downloads/cities.txt

Rename File with rename Command

The most popular and easy way to rename a file is using the rename command. The rename command is created to rename files and directories. The rename command is not installed by default for most of the Linux distributions. First, we should install the rename command like below.

Install rename Command In Ubuntu, Debian and Mint:

$ sudo apt install rename

Install rename Command In Fedora, RHEL and CentOS:

$ sudo dnf install rename

The rename command is more complex than the mv command. So the rename command provides more features than mv command too. The syntax of the rename command is as below. The rename command works with regular expressions in order to select and change file names.

rename REGEX FILE
  • REGEX is the regular expression which is used to specify old and new name for the file.
  • FILE is the file we want to change its name.

In the following example, we change the file “cities.txt” into the “mycities.txt”

$ rename 's/cities.txt/mycities.txt/' \*

Alternatively, we can use the rename command in order to change the file extension. In the following example, we change the file extension from “*.txt” to “*.db”.

$ rename 's/*.txt/m*.db/' cities.txt

Convert Filenames To Lowercase:

$ rename 'y/A-Z/a-z/' \*

Convert Filenames To Uppercase

$ rename 'y/a-z/A-Z/' \*

Rename File with File Manager (GUI)

Most Desktop Managers like GNOME, KDE, LXDE, etc. provides a File Manager. File Manager can be used to rename a file. First, open the file manager and then right-click on the file we want to change its name. Then select Rename... or press the F2 key.

Rename File via File Manager

We can see a file name edit screen where we can put the new name for the file.

Rename File via File Manager

Rename File with Python Script

Python is a popular scripting language that can e used to rename files. The os module prıvides the rename() method which can be used to rename files. The rename() mehtod accepts two parameters where the first parameter is the file current name and the second parameter is the new name of the file.

import os

os.rename("cities.txt","mycities.txt")

Alternatively, we can use the absolute paths for the file like below.

import os 

os.rename("/home/ismail/cities.txt","/home/ismail/mycities.txt")

Leave a Comment