Rename Multiple Files on Linux

Rename folder on Linux

Rename folder on Linux, it’s an easy process, and there is more than one command for this goal. We will discuss this article. Also, rename folder in Linux follows the same process for any file.

So, we will learn how to use the command ‘mv‘ (short of move) to rename or move a folder. In addition, we will go through the command ‘rename’ that also we can rename folder in Linux.

Also, It is vital to conserve the file system structured to facilitate access to the data. 

Rename folder on Linux

First, let’s see how we can make it using the mv command and how it works.

The syntax of the mv command for moving folders is as bellow:

mv [OPTIONS] source destination

For example, to rename the directory bitslovers as bitslovers-temp can run:

mv bitslovers bitslovers-temp

When renaming folders, you must define precisely two parameters to the mv command. The first parameter is the target folder name that you want to rename, and the second one is the new name.

Move directory in Linux

It is noteworthy that if the bitslovers-temp already exists, the bitslovers folder is moved to the bitslovers-temp directory.

Rename folder in Linux using the command “mv” and “find”

In some scenarios, you may not know right where your folders are placed on your system.

Fortunately for you, a command supports you finding and discovering folders on a Linux system, the find command.

So, to find and rename folders on Linux, use the “find” command with the “type” option to scan for folders. You can then transfer your folders by performing the “mv” command with the “-execdir” option.

Let’s see how easy to find and rename folders:

find . -depth -type d -name <source_folder> -execdir mv {} <target_folder> \;

In addition, let’s assume that you need to rename folder starting with “backup” on your filesystem to “old-backup.”

The first section of the command will find where your folder is placed.

find / -depth -type d -name "backup"

/home/bitslovers/backup

As a result, you know where your folder is. You can rename it by utilizing the “execdir” option with the “mv” command.

find / -depth -type d -name backup -execdir mv {} old-backup \;

How to rename Many Folders

Renaming a single folder is an uncomplicated task, but renaming multiple folders at once can be a difficulty, particularly for new Linux users.

Also, renaming multiple folders at once is unusually wanted.

To rename multiple folders on Linux, generate a new script file and use the “mv” command in a “for” loop to repeat over folders.

bash find and rename folders:

for a in *; 
do if [ -d "$a" ]; then
 mv -- "$a" "${a}_$(date +%Y%m%d)"
fi
done

Let’s explain the script steps:

  • The first line generates a loop and iterates over a list of all files.
  • The second line from our script analyses if the file is a folder.
  • The third line adds the current date to a specific folder.

Rename command for Linux

Renaming multiple directories with rename

First, let’s install the rename package:

How to install rename package on Ubuntu or equivalent:

sudo apt-get install -y rename

Instead of utilizing the “mv” command, you can adopt a dedicated built-in command. But, this command may not be immediately available on your Linux version.

The rename command is applied to rename many files and folders in Linux. This command is more unconventional than mv as it requires a fundamental knowledge of regular expressions.

To rename a folder in Linux, use “rename” with how you need the files to be renamed and the target folder.

$ rename <expression> <folder>

For example, let’s suppose that you need to rename all your folders that start with “photos-BACKUP” to “photo-backup.” In other words, replacing the uppercase to lowercase.

rename 'y/A-Z/a-z/' *

Selecting folders to be renamed

For example, you may want to rename only a few folders using the rename command.

So, you basically have two choices :

  • Employ wildcards to filter folders to be renamed.

First, if you need to rename folders ending with a given characters sequence, you would reach that by using the following command.

rename 'y/_config/_backup/' *

The syntax above by the rename command is identical to the sed command.

So, you can use input redirection to filter folders to be renamed.

ls -d *_config | rename 'y/*_config/*_backup/'

When applying one of those two options, your folders will be renamed to have a “_backup” in the end.

How to Rename Multiple Files In Linux

There are many commands and utilities to a rename bunch of files. However, let’s focus on the most used command for that proposal. And also how to rename files with regex.

mmv Linux Command

The mmv command is applied to copy folders, move, add and rename files in bulk using conventional wildcards in Linux/Unix-like operating systems. To install it on Ubuntu, Mint, Debian, you can run the command below:

sudo apt-get install mmv

For example, you have the following files in your current directory.

 ls
picture-year-1.png picture-year-2.png picture-year-3.png ... picture-year-100.png

And you need to rename all files that start with the word “year” to “2021”. 

I have sure that you don’t want to do it manually because we have 100 pictures in that folder. With the mmv command, it’s easy and straightforward.

Reame file append date on Linux

To rename all files starting with the word “year” to “2021”, just run:

mmv picture-year\* picture-2021\#1

To verify if the files have been renamed or not.

ls
picture-2021-1.png picture-2021-2.png picture-2021-3.png ...

Clarification

Let’s go deep, what this command did —the first argument (picture-year\*) is the pattern that we are looking for.

The second parameter is an argument that we would like to replace ( picture-2021\#1 ). 

So, mmv will scan for any filenames starting with the word ‘picture-year’ and rename the matched files according to the second argument we are trying to replace. Also, the wildcards, such as ‘*,’ ‘?’ and ‘[]’, were used to meet one or more random characters. Please be careful that you must escape the wildcard characters; oppositely, they will be extended by the shell, and mmv won’t recognize them.

Regarding the ‘#1′ in the is a wildcard index. It meets the first wildcard located in the first argument pattern. For example, if we have ‘#2′, that would match with the second wildcard and so on. In other words, if we have only one wildcard (*), because of that, we use #1. 

Furthermore, the hash sign should be escaped too. 

Another example, it’s possible to rename all files with a specific extension to another extension. Like, to rename all .data files to .json file format in the current directory, simply run:

mmv \*.data \#1.json

Let’s see a second example. Let us assume you have the following files.

ls
new1.png new2.png new3.png

And you need to replace the first appearance of new with old in every file in the current folder. To accomplish that, it’s super easy:

mmv '*new*' '#1old#2'
ls
old1.png old2.png old3.png

Conclusion

So, you learned several ways of renaming folders on Linux, the common with “mv” command. All new users that are starting on Linux should know at least the mv command.

Also, like many commands on Linux, we can combine multiples commands to achieve a single goal. To rename the folder, it’s possible to use the “mv” with “find” to give us more versatility and power.

Leave a Comment

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

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.