How do you rename file in linux?

Renaming files is one of the most basic tasks you often need to perform on a Linux system. You can rename files using a GUI file manager or via the command-line terminal.

mv is a Unix command that moves one or more files or directories from one place to another. If both filenames are on the same file system, this results in a simple file rename; otherwise the file content is copied to the new location and the old file is removed

Renaming a single file is easy, but renaming multiple files at once can be a challenge, especially for users who are new to Linux.

Renaming files is not a particularly advanced operation; as long as it’s done on a small number of files, it usually doesn’t require special tools. However, when there’s an entire folder of photos from last year’s vacation waiting to be renamed, it might be wise to consider some time-saving tricks or apps.

There are two general approaches to batch file renaming: it can be done either via the command-line interface or by using a standalone application. Linux users already know how powerful the CLI can be, so it shouldn’t be surprising that there are several commands for file renaming.

Renaming Files with the mv Command

The mv command (short of move) is used to rename or move files from one location to another. The syntax for the mv command is as follows:

mv [OPTIONS] source destination

Copy

The source can be one or more files, or directories and destination can be a single file or directory.

Note that the mv command requires write permission for the folder containing the files. In the case of system files and folders, the user needs to obtain root permissions to rename files by prepending mv with sudo or su. An extra layer of protection is provided by the -i (interactive) option which asks the user to confirm the file rename before it’s actually applied.

There’s also the -v (verbose) option which lists all changes that have been made by mv. Options are written after mv but before the filenames.

If you specify multiple files as source, the destination must be a directory. In this case, the source files are moved to the target directory.

If you specify a single file as source, and the destination target is an existing directory, then the file is moved to the specified directory.

To rename a file, you need to specify a single file as a source and a single file as a destination target.

For example, to rename the file file1.txt as file2.txt you would run:

mv file1.txt file2.txt

The mv command can only rename one file, but it can be used with other commands to rename multiple files.

Let’s take the commands, find, for, or while loops and renaming multiple files.

For example, when trying to change all files in your current directory from .txt extension to .pdf extension, you will use the following command:

for f in *txt; do

   mv — “$f” “${f%.txt}.pdf”

done

Renaming Multiple Files with mv

Things get trickier when you want to rename multiple files. mv has no capability to deal with renaming multiple files. You must resort to using some nifty Bash tricks. That’s fine if you know some medium-grade command-line fu, but the complexity of renaming multiple files with mv stands in stark contrast to the ease of using mv to rename a single file.

Things escalate quickly.

Let’s say we’ve got a directory with a variety of files in it, of differing types. Some of these files have a “.prog” extension. We want to rename them at the command line so that they have a “.prg” extension.

Rename Files on Linux Using the Rename Command

The mv command can rename only one file at a time, but it can be used in conjunction with other commands such as find or inside bash for or while loops to rename multiple files.

The following example shows how to use the Bash for loop to rename all .html files in the current directory by changing the .html extension to .php.

for f in *.html; do

    mv — “$f” “${f%.html}.php”

done

With the rename command, you will have a bit more control. Many Linux configurations include it by default. But, if you don’t have it installed, you can do it in just a minute with a simple command.

In the case of Debian, Ubuntu, Linux Mint, and derivatives:

sudo apt install rename

On the other hand, if you are using CentOS 7 or RHEL:

sudo yum install rename

And, if you are using Arch Linux:

yay perl-rename ## or yaourt -S perl-rename

Now, we can start using the rename command. In general, the basic syntax of the rename command looks like this:

rename ‘s/old-name/new-name/’ files

It may seem complex at first, but it’s a lot simpler than it might seem.

In this example, we will create a new folder called file rename, and using the touch command, we will create 5 files.

mkdir file rename

cd file rename

touch file{1..5}.txt

ls

With the last ls command, you can view the files that you created.

If we want to rename a single file called file1.txt, the sentence would be like this:

rename ‘s/file1/file1/’ file1.txt

If we wanted to change the extension to all files, for example, to .php. We could do it this way:

rename ‘s/.txt/.php/’ *.txt

ls

We can also specify another directory where the files you want to rename are.

rename ‘s/.txt/.php/’ FILE/PATH

We’d like to mention that the rename uses a regular expression of Perl, meaning this command has extensive possibilities.

Finally, it is a good idea to check all the command options. You can view them in the terminal by executing:

rename –help

Some common examples of how to use the rename command are:

Convert filenames to uppercase:

rename ‘y/a-z/A-Z/’ *

Convert filenames to lowercase:

rename ‘y/A-Z/a-z/’ *

Replace spaces in filenames with underscores:

rename ‘y/ /_/’ *

Remove Rename Command

The rename command is used to rename multiple files. This command is more advanced than mv as it requires some basic knowledge of regular expressions.

There are two versions of the rename command with different syntax. In this tutorial, we will be using the Perl version of the rename command. If you don’t have this version installed on your system, you can easily install it using the package manager of your distribution.

If you no longer wish to have rename installed on your system, remove it using the software manager. Or from the terminal.

For Debian, Ubuntu, Linux Mint and derivatives:

sudo apt remove rename

And for CentOS and RHEL:

sudo yum remove rename

That’s it, rename is removed from your Linux machine.

Below are a few more common examples of how to use the rename command:

Replace spaces in filenames with underscores

  • rename ‘y/ /\_/’ \*

            Copy

Convert filenames to lowercase

  • rename ‘y/A-Z/a-z/’ \*

Copy

Convert filenames to uppercase

  • rename ‘y/a-z/A-Z/’ \*

As we have seen, there are two commands that can do it. One is simpler than the other, but both accomplish the task.

Leave a Comment

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