How to Rename a File in Linux?

Filed Under: UNIX/Linux
Rename A File

This tutorial will walk you through the simple steps to rename a file in Linux.

It’s going to be a very simple and straightforward tutorial even if you are a beginner.

Before we begin

This tutorial was made on a system running Ubuntu 18.04 on a user with sudo privileges. Most of what you will see in this tutorial can be replicated without any modifications on most of the Linux systems out there.

But in case you are unable to replicate it, you can try another method from the ones mentioned below.

4 Ways to Rename a File in Linux

It is easy to rename a file in Linux using a GUI file manager or using the command line when you are dealing with a single file. 

But if you wish to rename multiple files, using the command line is a better approach. In this tutorial, we will discuss 4 different ways in which we can rename a file in Linux.

1. Using the mv command

The function of the mv command is to move files. However, as a bonus functionality, it can be used to rename a file in Linux. 

mv [OPTIONS] sourcefile destinationfile

While the mv command can be used to move multiple files or directories, we can enter only one file name each for the source and destination. Hence to rename a file OldName.txt to NewName.txt, we use the following command.

mv OldName.txt NewName.txt

The mv command can be used as part of a bash loop or with the find command to rename multiple files at once. 

2. Using the rename command

When we wish to rename multiple files at the same time, we can use a utility designed to rename a file in Linux instead of something that can rename files as an unintended functionality.

This brings us to the rename utility. To use the rename command, we install it on our system using apt or whichever package manager your Linux distro has.

sudo apt install rename

Once the utility is installed, we can now use the rename command through the command-line. 

rename [Options] 's/oldname/newname/' *

This command requires basic knowledge of regular expressions. Here, ‘s’ stands for substitute.

The remaining expression is self-explanatory. Using * makes the command run for all files whose name contains oldname.

3. Using renameutils

The renameutils package provides us with a set of commands to rename a file in Linux. With a focus on batch renaming of files, renameutils consists of five commands.

However, we will focus on two – qmv (quick move) and imv (interactive move) for this tutorial.

To install renameutils on our system, we use the following command.

sudo apt install renameutils

Once the utility is installed, we can now use the qmv and imv commands through the command-line. 

Syntax (qmv):

qmv directoryname/

This will open a window where the left column displays the original names of the files. Whereas, the right column is for the name which you wish to set for your files. Edit the right column to set the names you wish for your file.

Syntax (imv):

imv directoryname/filename

This will display the address of the file. Simply edit the name to rename the file and press enter to save the change.

Note that imv only allows you to rename one file at a time.

4. Using a GUI file manager

We can use the bulk rename utility of the Thunar file manager by installing the GUI file manager Thunar with the below command.

sudo apt-get install thunar

Now you can launch the bulk rename utility using the following command.

thunar -B

You can click the + sign to add files. It allows the following criteria for renaming.

  • Insert Date or Time
  • Insert or Overwrite
  • Numbering
  • Remove Characters
  • Search & Replace
  • Uppercase / Lowercase

Once you choose the desired criteria, simply click Rename Files to proceed with the operation.

Wrapping up

Linux based systems offer a wide variety of options to rename a file in Linux. One can use any of these to suit their needs and preferences. We hope this tutorial was able to help you understand different ways to rename a file in Linux.

If you have any feedback, queries or corrections, feel free to leave them in the comments below.

Leave a Reply

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

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages