Batch Rename in Linux: An Easy Guide.(批量重命名linux)
Renaming multiple files at once might sound daunting, but in Linux it can be done with no more than three quick steps. This guide will show you how to use a simple command line program called “rename” to batch rename a collection of files and folders.
First, you’ll need to open a terminal window. To do that, open the launcher and type ‘terminal.’ This will bring up a command line interface where we can run our commands.
Once you’ve opened the terminal, navigate to the folder that contains all of your files and folders you want to rename by typing the ‘cd’ command with the path of the folder, for example, “cd /home/user/rename.”
Once you’re in the folder, type ‘rename’ followed by a few flags to control exactly which files and folders to rename, and then provide the pattern. Here’s a quick rundown of the flags:
1. The -n option lets you preview the changes before they’re made.
2. The -f flag tells the program to make all of the changes without asking for confirmation.
3. The -v flag will print out all of the files and folders that were renamed.
4. The -e option allows you to pass in a Perl expression to rename the files.
For example, let’s say you wanted to rename all of the files and folders in your folder with the prefix ‘file.’ You’d run this command:
rename -nvfe ‘s/^/file./’ *
This would rename every file and folder with the prefix ‘file.’ However, with the -n flag applied, no changes will actually be made. With the -f option, you can stop it from asking for confirmation for each file.
The ‘rename’ command can do a lot more than just rename individual files and folders. You can also use it to add numbers to the end of each file or folder name. This can be done by passing in a sequence number to the -e flag. For example:
rename -nvfe ‘s/$/sprintf(” %02d”,$N++)/e’ *
This will add a two-digit number to each file that starts with 0 and increments with each successive file.
Finally, you can also use regular expressions to control how your files and folders are named. Regular expressions allow you to match certain patterns and then replace them. For example:
rename -nvfe ‘s/([a-z0-9\-]*).jpg/file_$1.jpg/’ *
This command will match any file that ends in .jpg and add the prefix ‘file_’ to the beginning.
By following the simple steps in this guide, you can easily and quickly batch rename files and folders in Linux. All it takes is a few simple steps and a little knowledge of regular expressions.