Batch Rename Files in Linux(批量重命名linux)

Most of us know that in Linux, the ‘rename’ command can be used to rename individual files. But batch renaming of files is not possible with rename command.

Luckily, there are several other methods to batch rename files in Linux. In this article we’ll explore various methods to batch rename files using the command line, GUI File Managers and file managers with advanced batch rename capabilities in Linux distributions such as Debian, Ubuntu, Linux Mint and others.

Method 1: Using Nautilus File Manager

When working on files and directories, Nautilus is often the preferred file manager for the majority of users. It is used by default in most Debian-based distributions such as Ubuntu and Linux Mint . Even in some Red Hat based distributions such as Fedora and CentOS you will find Nautilus installed by default.

To begin with batch renaming files through Nautilus, we first need to open the Nautilus file manager window and go to the directory in which our files are stored.

Now right click on the first file and select ‘Rename

‘ option.

Type the new name for the file in the edit box that appears.

Press the Tab key to move to the next file and give it a new name.

Keep repeating the steps until all the files are renamed.

Method 2: Using the mv command

The ‘mv’ command is used mainly to move and rename files. It can also be used to batch rename the files.To achieve our goal of batch renaming files, we need to combine this command with the ‘for’ loop.

The basic syntax of the command is:

$ mv old_file_name new_file_name

To demonstrate the batch renaming of files, assume there are a series of files as follows:

File1.txt

File2.txt

File3.txt

To rename them all in one shot, issue the following command:

$ for filename in File1.txt File2.txt File3.txt;

do mv $filename new_$filename;

done

The above command will rename all three files to new_File1.txt, new_File2.txt and new_File3.txt respectively.

Method 3: Using the find command

The ‘find’ command is a very powerful tool which searches for files based on various conditions and allows us to modify them in several ways. We can combine it with the ‘rename’ command to batch rename files in Linux.

The basic syntax of the find command is as follows;

$ find directory -exec command {} \;

To demonstrate the batch renaming of files with find, assume that there is a series of files as follows;

File1.doc

File2.doc

File3.doc

To rename them all in one shot, issue the following command;

$ find . -name “*.doc” -exec rename ‘s/\.doc$/.txt/’ ‘{}’ \;

The above command will rename all three files as File1.txt, File2.txt and File3.txt respectively.

In this article, we’ve explored various methods to batch rename files in Linux. Even though the methods are easy to use, Renaming files should be done with carefulness as it could cause loss of data if not done properly.


数据运维技术 » Batch Rename Files in Linux(批量重命名linux)