How to Rename a File in Linux using mv, rename & mmv

This blog post teaches you how to rename a file in Linux using multiple methods and commands.
Renaming files is a widespread process in the workday of a system administrator, developer, or regular Linux user. Renaming helps us keep a copy of the files and temporarily use other names while working on the server. The Linux operating system offers renaming files or directories via GUI, but when we are using or working on a server where hosted websites are located, we need to know the most used commands for renaming.

In the next paragraphs we will explain with examples three different commands for renaming files: mv, rename, and mmv.
Prerequisites

A server running Ubuntu 22.04 or any Linux OS (CentOS, Debian, or AlmaLinux)
User privileges: root or non-root user with sudo privileges

Rename a file in Lunix using the mv command
The mv command is a shortcut of the “move” and is one of the most used commands for moving files and directories between different paths. It is also a simple method of how to rename a file in Linux, which is the purpose of this blog post. The syntax of the mv is the following:

mv [OPTION]… [-T] SOURCE DEST
mv [OPTION]… SOURCE… DIRECTORY
mv [OPTION]… -t DIRECTORY SOURCE.

Let’s rename a text file using the mv command. List the content of the current directory:
root@host:/var/www/html# ls -al
total 8
drwxr-xr-x 2 root root 4096 Mar 2 09:41 .
drwxr-xr-x 3 root root 4096 Mar 2 09:13 ..
-rw-r–r– 1 root root 0 Mar 2 09:41 test.txt

To rename the test.txt file, execute the following command:
mv test.txt test-renamed.txt
List the content of the directory again:
-rw-r–r– 1 root root 0 Mar 2 09:41 test-renamed.txt
If you go one level up you can rename the html directory as well:
mv html html.backup
The mv command will apply to files and folders:
drwxr-xr-x 2 root root 4096 Mar 2 05:43 html.backup
Next example is to move, the file or directory to a different location on your server. Let’s move the html.backup to a different location:
mv html.backup/ /opt/
List the content of the opt directory:
root@host:/var/www# ls -al /opt/
drwxr-xr-x 2 root root 4096 Mar 2 09:43 html.backup

There are plenty of options that you can use with the mv command, such as:
-i Before overwriting we get a prompt
-f Overwrite without warning and prompting
-v Shows a verbose output

For more information about the mv command, you can check by executing the following command:
man mv
You will get a very long description of the mv command as an output.
Rename a file in Lunix using the rename command
To use the rename command, we need to install the rename package first with the following command:
apt install rename
Once installed, you can check the version with the command below:
rename -V
You will get output similar to this:
root@host:# rename -V
/usr/bin/rename using File::Rename version 1.30, File::Rename::Options version 1.10

The syntax for the rename command is the following:
rename [options] ‘s/[pattern]/[replacement]/’ [file name]
Let’s say that we have three different text files:
root@host:/var/www# ls -al
-rw-r–r– 1 root root 0 Mar 2 10:38 file1.txt
-rw-r–r– 1 root root 0 Mar 2 10:38 file2.txt
-rw-r–r– 1 root root 0 Mar 2 10:38 file3.txt

To rename all these files with the rename you need to execute the following command:
rename ‘s/txt/backup/’ *.txt
If you list the content now, you will see the following output:
root@host:/var/www# ls -al
-rw-r–r– 1 root root 0 Mar 2 10:38 file1.backup
-rw-r–r– 1 root root 0 Mar 2 10:38 file2.backup
-rw-r–r– 1 root root 0 Mar 2 10:38 file3.backup

To know more about the rename command you can execute the man rename in your command line.
root@host:# man rename
RENAME(1p) User Contributed Perl Documentation RENAME(1p)

NAME
rename – renames multiple files

SYNOPSIS
rename [ -h|-m|-V ] [ -v ] [ -0 ] [ -n ] [ -f ] [ -d ] [ -u [enc]] [ -e|-E perlexpr]*|perlexpr [ files ]

DESCRIPTION
“rename” renames the filenames supplied according to the rule specified as the first argument. The perlexpr argument is a Perl expression which is expected
to modify the $_ string in Perl for at least some of the filenames specified. If a given filename is not modified by the expression, it will not be
renamed. If no filenames are given on the command line, filenames will be read via standard input.

Examples (Larry Wall, 1992)
For example, to rename all files matching “*.bak” to strip the extension, you might say

rename ‘s/\.bak$//’ *.bak

To translate uppercase names to lower, you’d use

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

Rename a file in Lunix using the mmv command
The mmv command is used for moving, copying, appending, and linking source files to the target file specified with the pattern.
If we want to use the mmv command we need to install the mmv packet first.
apt install mmv
The syntax of the mmv command is the following one:
mmv [-m|x|r|c|o|a|l|s] [-h] [-d|p] [-g|t] [-v|n] [–] [from to]
The usage is simple. To rename the file1.backup file back to file1.txt execute the following command:
mmv file1.backup file1.txt
If you want to know more information about this command, you can execute man mmv command in your prompt:
root@host:/var/www# man mmv
MMV(1) General Commands Manual MMV(1)

NAME
mmv – move/copy/append/link multiple files by wildcard patterns

SYNOPSIS
mmv [-m|x|r|c|o|a|l|s] [-h] [-d|p] [-g|t] [-v|n] [–] [from to]

EXAMPLES
Rename all *.jpeg files in the current directory to *.jpg:

mmv ‘*.jpeg’ ‘#1.jpg’

Replace the first occurrence of abc with xyz in all files in the current directory:

mmv ‘*abc*’ ‘#1xyz#2’

Rename files ending in .html.en, .html.de, etc. to ending in .en.html, .de.html, etc. in the current directory:

mmv ‘*.html.??’ ‘#1.#2#3.html’

Rename music files from – – .ogg to – – .ogg in the current directory:

mmv ‘* – * – *.ogg’ ‘#2 – #1 – #3.ogg’

That’s it. You successfully renamed files and directories using different methods and commands.
PS. If you liked this post on how to rename files in Linux, please share it with your friends on social networks or simply leave a comment in the comments section. Thank you.

Latest articles

Related articles