1. Overview

In this tutorial, we’ll learn how we can rename multiple files in Linux by removing the extension. There are several commands that we can use to achieve this. We’ll first look at the mv command, and how we can use that to rename multiple files. We’ll then cover the rest of the commands mostly used for this purpose.

2. The mv Command

The mv (move) command renames SOURCE to DEST, or SOURCE to DIRECTORY. However, we can modify the command a little bit to rename multiple files at once. First, let’s look at this command’s format:


Now, let’s see some examples.

2.1. Using mv With for Loop

To rename the files by removing their extensions, we’ll use the mv command with for loop. The loop will iterate through the list of files present in the current directory. Also, if the loop condition is true, it’ll remove the extensions using the mv command:

$ for i in $( ls *.txt ); do mv $i ${i%.*}; done

The above command looks for all the files with the .txt extension and removes the extension with the mv command. Similarly, we can use the same method to add the extension to all the files present in the working directory:

$ for i in $( ls ); do mv $i $i.txt; done

Alternatively, we can use the this method to change the file extension. For this example, we’re searching for the files with the .log extension and renaming them to the .txt extension:

$ for i in $( ls *.log ); do mv $i $i.txt; done

2.2. Using mv With find

Alternatively, we can also rename multiple files by using the mv command in conjunction with the find command. For this, we’ll first search for the files using find. After that, we’ll use the exec argument to execute the mv command. This removes the extensions of the file and renames them:

find . -depth -name "[current file name]" -exec sh -c 'f="{}"; mv -- "$f" "${f%[current file name]}[new file name]"' \;

This command finds all files as listed in the search parameter. Next, the -exec executes mv on the search result to rename the files.

For instance, to change the file extensions of example1.txt, example2.txt and example3.txt to .pdf, we’ll replace the “[current file name]” and “[new file name]”parameters:

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

2.3. Rename Using a Bash Script

We can also execute the mv command in a Bash script to rename files by removing their extensions. For this script, we’ll use the for loop and mv together.

First, we open the text editor. Then, let’s add these lines of code in the script file:

for f in *.txt; do
mv -- "$f" "${f%.txt}.pdf"

We then press Ctrl + S and Ctrl + X to save and exit editor.

Lastly, we’ll execute the script:

$ ./rename_bash

In the Bash script, we can also use mv in combination with cut to achieve the same result. For this, we’ll add the following code in the Bash script:

for file in *.xls; do
mv $file `echo $file | cut -d. -f1`

After saving the file and exiting the editor, let’s run the script:

$ ./rename_bash

Lastly, we can use the ls command to check the filenames to ensure that the script has run successfully.

3. The rename Command

In addition to mv, there’s a rename command that can also be used to rename multiple files in Linux. However, this Linux utility is not installed by default.

To use this command, we’ll first install it:

# for APT and Debian
$ sudo apt update && sudo apt install rename
# for CentOS and Fedora
$ sudo yum update && sudo yum install prename
# for Arch Linux
$ sudo pacman -S rename

The general syntax for this command looks something like this:

rename [options] 's/[filename element]/[replacement]/' [filename]
  • [option]: The [options] indicate the optional flags for the command
  • s: the string to replace with
  • [filename element]: the string we want to replace in the filename
  • [replacement]: replacement for the current filename
  • [filename]: the file we want to rename

Now, we’ll use the rename command to rename multiple files in Linux.

3.1. Replacing a Filename

For instance, to replace a part of a filename using the rename command, we’ll use s/example/test/ as an argument:

$ rename -v 's/example/test/' *.txt
example1.txt replaced as test1.txt
example2.txt replaced as test2.txt
example3.txt replaced as test3.txt

This command will replace all the .txt files with example in their names. Hence, “example” will be replaced with the “test” string.

3.2. Replacing a File Extension

In addition, we can use the rename command to replace file extensions. For this, we’ll use the extension as a replacement string instead of the filename:

$ rename -v 's/.txt/.pdf/' *.txt
example1.txt replaced as example1.pdf
example2.txt replaced as example2.pdf
example3.txt replaced as example3.pdf

Alternatively, to remove the extension, we’ll leave the replacement argument empty:

$ rename ".XLS" " " *

This removes the extension of all .XLS files.

4. The mmv Utility

The mmv utility is most commonly used for renaming files in bulk. It uses wildcards to replace parts of the filename. It’s available in Debian repositories and requires installation. To install it in Debian-based Linux systems, we can use apt:

$ sudo apt update && sudo apt install mmv

Here’s the syntax for this utility:

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

The filename to be replaced is written in from part of the command, and the filename to replace with is written in the to part of the argument.

The from argument takes the wildcard * to match the number of occurrences of the character. On the other hand, the to argument takes # wildcard as an input. This symbol matches the wildcards present in the from part of the command.

For instance, if the from argument has example*.* then the to part would look like xyz#2.#1. Hence, #1 matches the * after the dot (.) and #2 matches the * before the dot (.).

4.1. Replace a File Extension

For instance, to replace file extensions in bulk, we’ll write the extension that we want to replace first. Then, we’ll write the new extension. Let’s remove the .jpeg extension from the filenames starting with “example”:

$ mmv '*.jpeg' '#1'

Here “.jpeg” is the from parameter and the empty parameter is the to parameter.

4.2. Replace the Filename

Similarly, we can replace the filenames in bulk using the mmv utility. To replace all files starting with” example” to “test”, we’d write *example* in from parameter and #1test#2 in the to parameter:

$ mmv '*example*' '#1test#2'

The # is a wildcard that matches the asterisk (*) in the from part of the argument.

5. Conclusion

In this article, we discussed how we can rename multiple files in Linux by using the mv command. After that, we looked at the rename utility used to batch rename the files. Lastly, we discussed how we can use the mmv command to rename the files in bulk.

We learned that the mv command requires the support of other commands to batch rename the files. On the other hand, the rename and mmv can handle this task more efficiently. Also, the mmv utility is more flexible in matching the filenames and extensions using wildcards.

Inline Feedbacks
View all comments