How to Rename Files in Linux


To rename the file On Linux, the mv (move) command is used. The mv command is a tool used to move the files from a source location to the destination. However, it is also used to rename the files or directories.

In this guide, I will cover how to use the mv command line utility to rename the files. Moreover, I will also mention methods to rename files using scripts and third-party utilities.

The guide will cover the following content:

Renaming a File on Linux using mv Command

The general syntax of using the mv command to rename the files on Linux is mentioned below:

mv [options] [original_file_name] [new_file_name]

Commonly used options while renaming the file are listed below:

–force -f To rename the file without a prompt before overwriting
–backup -b To backup the file before attempting to rename
-interactive -i To rename the file with a prompt before overwriting
–no-clobber -n To rename the file without overwriting the existing file
–verbose -v To get a brief output while renaming
–help -h To print help on the standard output

 

For example, to rename a file file.txt to newfile.txt use the mv command mentioning the original file name first and then the new name of the file.

mv file.txt newfile.txt

The above command would not give any output. To get a brief above rename, use the -v flag.

mv -v file.txt newfile.txt

To avoid unintended overwrite, the -i flag is used.

mv -i file.txt newfile.txt

The newfile.txt was already in the directory, therefore a prompt appeared to ask for overwriting the file.

To rename and move the file to another, use the source file name and path and destination path and new name.

mv -v dir1/file.txt dir2/newfile.txt

Another option is –no-clobber or -n which can be handy in shell scripting. This option will not rename the file or move it if it already exists in the destination directory. However, it will be done silently, no output will be printed.

mv -n dir1/file.txt dir2/newfile.txt

The –backup or -b flag is used to rename the file and if the file already exists in the destination directory it will be saved as a backup file with a different name.

mv -b file.txt dir2/newfile.txt

In the example, a file with the same name already existed in that directory dir2, so the file is backed up with the name newfile.txt~.

Renaming Multiple File using find and mv Command

The mv command can rename a single file at a time, however, it can also be employed to rename multiple files using the find command. The find command essentially searches the files and directories and to further process them, it allows executing the other commands with the -exec option.

For instance, in order to rename multiple files, we will utilize the find command and its options to search for files with specific names or extensions, and subsequently rename them by executing the mv command.

find . -type f -name "*.txt" -exec sh -c 'f="{}"; mv -- "$f" "${f%.txt}.html"' \; 

In the above command, the find command searches for the file types in the current working directory.

. indicates the current working directory

-type f is used to find only files

-name is used to mention the pattern for the file search

-exec is used to execute the shell command to process the find command results

sh -c is used to run the inline shell script

f={} is assigning the results to the f variable

double dashes are used to indicate the subsequent parameter must not be interpreted as mv command options

${f%.txt}.html is removing the .txt extension of the file and appending the .html extension.

Renaming Multiple Files using Bash Script

The bash script can also be useful to rename a batch of files. Let’s create a bash script that will ask the user to enter the directory name and the file extension of the files to be changed.

#!/bin/bash

set -e

echo "Type the Directory"

read dir

cd $dir

echo "Type the file extension"

read ext

echo "Type the new file extension"

read newext

for f in *.$ext; do 

 mv -- "$f" "${f%.$ext}.$newext"; 

done

In the above code, the user will be asked to type the directory name in which files are located. Next, the user will be asked to provide the file extension that needs to be changed and type the new extension. If any command fails to execute, the code will be terminated.

Note: The above two methods are only useful if you want to change the extension of multiple files. Use the rename or qmv command line utilities to rename multiple files.

Renaming Files Using the rename Command

The rename is a versatile tool used to change the file name by the specified pattern. The rename command does not come by default in Linux distributions. To install it on Linux, use the following commands.

#Ubuntu, Debian, and their derivatives 

sudo apt install rename 

#RHEL, CentOS, and their derivatives

sudo yum install rename

#Arch Linux

sudo pacman -S rename

Or 

yay -S perl-rename

The rename command is an abstraction of Perl expression to rename the files, which means it has a lot of possibilities. Moreover, in comparison with the mv command, the rename is a much more powerful utility when it comes to renaming files in bulk. It uses the substitute and translate mode of Perl expression to change the name of the files and directories.

The general syntax of renaming files using the rename command is given below:

rename [options] 's/[pattern]/[replacement]' [filename]

In the above command, the s indicates the Perl substitution mode. In Perl, the substitution mode is used with the s/// operator and substitutes the [pattern] with the [replacement].

For example, the files in the current working directories are file1.txt, file2.txt, and file3.txt. To rename them to newfile1.txt, newfile2.txt, and newfile3.txt use the following command.

rename 's/file/newfile/' file* 

The general syntax of using the rename command in translate mode is given below:

rename [options] 'tr/[pattern1]/[pattern2]' [filename]

Here, the tr indicates the Perl translate mode. The Perl translate mode is used for character translation within the string and used with the tr/// or y/// operators. It replaces the characters given in the [pattern2] with the characters in [pattern1].

For example, to replace the r characters of the file names filr1.txt, filr2.txt, and filr3.txt with the e use the following command.

rename 'tr/r/e/' file*

Other options that are used with the rename commands are given below:

–symlink -s This option is used to omit the symbolic links while renaming
–no-act -n This option is used with –verbose or -v for dry run, and it does not make any changes
–no-overwrite -o This option is used to avoid the over-writing of the existing files
–all -a This option is used to replace all the occurrences of the pattern rather than only first
–last -l This option is used to replace the last occurrence
–interactive -i This option is used to generate prompt before overwriting
–verbose -v To option is used to print the file names that are renamed

Note: It is a good practice to rename files by using protective options because this action is irreversible. Try using options like –interactive, –no-act, and –no-overwrite for protection.

To rename the single file using the rename utility, use the command given below:

rename -v 's/file1/MyFile1/' file1.txt

To rename the file extensions, mention the new extension in the [replacement] part of the syntax mentioned above.

rename -v 's/.txt/.html/' *.txt

To replace a specific part of the file names.

rename -v 's/file/test/' file*

The rename command can also be used to delete a part of the file name. It can be achieved by keeping the [replacement] part of the syntax empty. For example, to delete new_ from the files new_file1.txt, new_file2.txt, and new_file3.txt, use the following command.

rename -v 's/new_//' file*

On Linux, the files or directory names with spaces are a bit complex to handle. Those spaces can be filled with another character such as an underscore (_) or dash (-) with the rename command. For example, to replace the space in the file name new file.txt with a dash (-) use the following command.

rename -v 'tr/ /-/' "new file"*

To change the character cases of the file names, the /a-z/ and /A-Z/ expressions can also be used. For example, to change the lower-case file names to upper-case, use:

rename -v 'tr/a-z/A-Z/' *.txt

Similarly, to change the upper-case file names to lower-case, change the [replacement] with /a-z/ and [pattern] with /A-Z/.

Renaming Files Using the qmv Command

The qmv command is another utility used to rename a single or batch of files. It is a part of renameutils package. It does not come by default in Linux, to install it use the following commands.

#Ubuntu, Debian, and their derivatives 

sudo apt install renameutils 

#Arch Linux and Manjaro

sudo pacman -S renameutils

#OpenSUSE

sudo zypper install renameutils

Steps to rename the multiple files using qmv are given below:

Execute the qmv command with the directory path that contains files.

sudo qmv /directory_path

A text file will open with all the file’s names. The filenames on the left side are original filenames while the filenames on the right side are needed to be modified.

Rename the files. In this example, I am renaming the html files to txt files.

Save the file and you will see a prompt for the filenames that are modified.

The renameutils package also comes with other utilities like qcp and qcmd. The qcp works exactly like qmv but instead of moving it copies the files, however, the qcmd is used to execute commands on multiple files.

Renaming Files Using the mmv Command

The mmv is another Linux utility that can be used to rename one or multiple files. It has to be installed on Linux.

#Ubuntu, Debian, and their derivatives 

sudo apt install mmv

#Arch Linux and Manjaro

sudo pacman -S mmv

#CentOS

sudo yum install mmv

The syntax to rename files using mmv is given below:

mmv "source_name" "target_name"

For example, to rename files file1.txt, and file2.txt to myfile1.txt and myfile2.txt, use the following command.

mmv "file*.txt" "myfile#1.txt"

The #1 will replace the first wildcard *, and if you are using multiple wildcards then you have to use #2, #3, and so on.

Renaming Files with find, perl, and xargs Commands

In many Linux distributions, the rename command cannot be fully utilized. In this case, other approaches can be used. For example, a combination of utilities can also be helpful to rename files.

find . -type f | perl -pe 'print $_; s/[original-name]/[new-name]/' | xargs -d "\n" -n2 mv

In the above command, the find command searches the file in the current working directory. The perl command implements the expression; -p is used to tell perl to loop over each input line coming from the find command, while -e is used to execute the inline script. The print $_ is printing the input, while the next expression is substituting the old file names with the new file names. In the end, the xargs command executes the mv command on the input, while -d is used to define the delimiter, which is a new line \n in this case. This means the xargs will treat each input line as a separate item. The -n2 is used to define the maximum number of arguments to be passed to the mv command.

Conclusion

The mv command is used to rename the files on Linux. It can only rename one file name at a time. The mv command with the combination of find command tools and bash script can be used to rename multiple files. However, these approaches are only limited to changing the extension of the files. The rename, qmv, and mmv command line utilities are used to effectively rename multiple files. Note that in many Linux distributions, you may encounter errors while using the rename utility in that case I have mentioned another approach of using the find command with perl and xargs.

Categories