Batch Rename Files in Linux Easily

Introduction

Batch renaming files is a common task for Linux users. Renaming one or two files can be done manually, but what if you wanted to rename hundreds or even thousands of files at once? Doing it manually would be a tedious and time-consuming task. Fortunately, there are several tools in Linux that can simplify the process. In this article, we will discuss how to batch rename files easily in Linux using various methods.

Method 1: Using mv Command

The mv command is primarily used for moving files and directories, but it can also be used for renaming files. To rename a file using mv, simply specify the current filename and the new filename as arguments. For example, to rename a file named oldname.txt to newname.txt, use the following command:

mv oldname.txt newname.txt

This will rename the file oldname.txt to newname.txt.

Renaming Multiple Files

If you want to rename multiple files at once using mv, you can use wildcard characters. Wildcard characters are special characters that can match one or more characters in a filename. The most commonly used wildcard characters are * and ?.

The * character matches zero or more characters, while the ? character matches one character. For example, to rename all files with the .txt extension in the current directory, you can use the following command:

mv *.txt newextension

This will rename all files with the .txt extension to have the filename newextension, while retaining their original extension.

Method 2: Using mmv Command

The mmv command is a tool specifically designed for batch renaming files in Linux. It provides a simple and powerful way to rename multiple files at once using wildcard patterns. To use mmv, you need to specify the wildcard pattern for the original filenames and the new filename pattern.

For example, to rename all files in the current directory that have the extension .jpg to have the extension .png, you can use the following command:

mmv '*.jpg' '#1.png'

In this command, the '*.jpg' wildcard pattern matches all files with the .jpg extension, and the '#1.png' pattern specifies the new filename pattern, where #1 is replaced by the original filename without the extension, and the .png extension is added.

Method 3: Using rename Command

The rename command is another handy tool for batch renaming files in Linux. It is a Perl script that allows you to rename files using regular expressions.

To use rename, you need to specify a Perl expression that will be used to match and replace the filenames. The syntax for using rename is as follows:

rename 's/oldtext/newtext/' filenames

In this command, oldtext is the text you want to replace, newtext is the text you want to replace it with, and filenames are the names of the files you want to rename.

For example, to rename all files in the current directory that have the word old in them to have the word new, you can use the following command:

rename 's/old/new/' *

This will rename all filenames that have the word old in them to have the word new.

Conclusion

The ability to batch rename files is an essential skill for Linux users. Whether you need to rename hundreds or thousands of files, Linux provides several tools that can make the process quick and easy. In this article, we discussed three methods for batch renaming files in Linux: using the mv command, using the mmv command, and using the rename command. By using these tools, you can save time and avoid the tedious task of manually renaming files one by one.

操作系统标签