Getting started with Linux: Rename multiple files in Linux
Q: I know that I can use the mv command to rename a file. But what if I want to rename many files? It would be boring to do this for every file. Is there a way to rename multiple files at a time?
In Linux, you can use the mv command to change the file name. However, mv cannot rename multiple files with wildcards. You can use sed, awk, or xargs to process multiple files. However, these command lines are cumbersome and unfriendly, and are prone to errors if you are not careful. You do not want to cancel the error name of the 1000 files!
When you want to rename multiple files, the rename tool may be the simplest, safest, and most powerful command line tool. This rename Command is actually a Perl script that is pre-installed on all current Linux distributions.
The basic syntax of the RENAME Command is as follows.
- Rename [-v-n-f] <pcre> <files>
<Pcre> is a Perl-Compatible Regular Expression that indicates the file to be renamed and how to do it. The regular expression format is's/old-name/new-name /'.
The '-V' option displays the details of the file name change (for example, renaming XXX to YYY ).
The '-n' option tells the rename command to display the file rename without actually changing the name. This option is useful when you want to simulate changing the file name without changing the file name.
The '-F' option forcibly overwrites existing files.
Next, let's take a look at several real examples of the rename Command.
Change File Extension
Assume that you have many. JPEG image files. You want to change their names. Jpg. The following command willChange the. jpeg file to *. jpg.
- $ Rename's/\. jpeg $/\. jpg/'*. jpeg
Change uppercase to lowercase, and vice versa.
Sometimes you want to change the case sensitivity of the file name, you can use the following command.
Change all files to lowercase letters:
- # Rename 'y/A-Z/a-z /'*
Change all files to uppercase:
- # Rename 'y/a-z/A-Z /'*
Change file name Mode
Now let's consider more complex regular expressions that contain subpatterns. In PCRE, the sub-mode is included in parentheses, followed by a number (for example, $1, $2) after the $ sign ).
For example, the following command willChange NNNN.jpeg to 'DanNNNN.jpg '.
- # Rename-v's/img _ (\ d {4}) \. jpeg $/dan _ $1 \. jpg/'*. jpeg
- Img_5417.jpeg renamed as dan_5417.jpg
- Img_5418.jpeg renamed as dan_5418.jpg
- Img_5419.jpeg renamed as dan_5419.jpg
- Img_541_jpeg renamed as dan_541_jpg
- Img_5421.jpeg renamed as dan_5421.jpg
For example, the following command will convert 'img_000nnnn.w.'into 'dan_nnnn.jpg '.
- # Rename-v's/img _ \ d {3} (\ d {4}) \. jpeg $/dan _ $1 \. jpg/'* jpeg
- Img_0005417.jpeg renamed as dan_5417.jpg
- Img_0005418.jpeg renamed as dan_5418.jpg
- Img_0005419.jpeg renamed as dan_5419.jpg
- Img_000540000jpeg renamed as dan_540000jpg
- Img_0005421.jpeg renamed as dan_5421.jpg
In the preceding example, the sub-mode '\ d {4}' captures four consecutive numbers. The four captured numbers are $1, which will be used for the new file name.
This article permanently updates the link address: