The Linux rename Command has two versions: one is the C language version and the other is the Perl language version. The earlier Linux releases basically use the C language version, now it is hard to see the C language version. due to historical reasons, when the Perl language is booming, Linux Tool developers believe that Perl can replace C, therefore, most of the tools in C have been rewritten by Perl. Because Perl supports regular expression processing, it is more powerful and no longer requires the C language version.
1. How do I differentiate the version of the rename command in the system?
Enter man rename to see the first line is
RENAME (1) Linux Programmer's Manual RENAME (1)
This is the C language version. [View the C language version on the system]
If:
RENAME (1) Perl Programmers Reference Guide RENAME (1)
This is the Perl version!
Syntax differences between the two versions:
C language, according to The man annotation above,
The syntax format of rename is:
Rename fromtofile
This command has three parameters: from: Modify the name, to: Change the name, and file to be modified.
Usage example:
For example, if a batch of files are started with logstore, log001.txt, log002.txt ...... go to log100.txt
Now I want to replace all the logs of these files with history.
Rename log history log * [C usage ~]
The meaning of this command is clear. Replace the log characters in all files starting with log with history.
The replaced file is history001.txt and history002.txt.
Another man example of the rename C language version is to modify the Suffix in batches,
For example, we want to change all jpeg image file extensions to jpg files.
Rename .w..jpg *. jpeg
In this example, all extensions with the suffix ".jpg" have been modified to. jpg.
To sum up:
Functions implemented by the rename C language version: Modify file names in batches. The result is that each file will be replaced with the same string! That is to say, we cannot implement loops and rename them by number!
2. Batch Rename of the Perl version. The benefit of Perl is that you can use regular expressions to complete very strange functions.
Perl parameter format:
Rename perlexprfiles
Note: In perl, rename has only two parameters. The first parameter is a perl regular expression, and the second parameter is the file to be processed.
Man rename help example:
1) There are a batch of files ending with. bak. Now I want to remove all these. bak files.
Rename's/\. bak $ // '*. bak
This command is very simple, because I have not studied perl in the system. I don't know if it is the same as replacing strings in perl, but sed does this, therefore, if you have sed or tr basics, it is easy to understand that this replacement is exactly the same as the regular syntax in sed.
2) Change the name of all files containing uppercase/lowercase letters to lowercase letters.
Rename 'y/A-Z/a-z /'*
It is still the same as the sed replacement syntax. You do not need to explain it more. If you cannot understand it, You can systematically learn sed first.
There are several more practical examples:
1) Remove spaces in file names in batches
Linux File names do not support spaces and do not know when they are allowed. Of course, when calling a file through the command line, spaces are very problematic, for example, you can directly use mv oldfile newfile, but it won't work if there are spaces. You have to add double quotation marks: mv "oldfile" "newfile" or use a backslash to transfer \ []. It's okay, however, if you directly introduce the image name containing spaces into the Latex document, the file name will be printed directly when Latex generates a pdf file. This problem has plagued me for a long time, why does my generated pdf always show file names? Then I found out that the file name contains spaces! The file name generated in windows system contains spaces. Although annoying, some pictures generated by the HP scanner are added with spaces by default. There is no way to remove them, before studying the rename command, I used music videos to remove spaces.
The following two versions of the online flow to remove spaces:
1) tr version:
Find.-type f-name "**"-print |
While read name; do
Na = $ (echo $ name | tr '''_')
If [[$ name! = $ Na]; then
Mv "$ name" $ na
Fi
Done
I used this version all the time. I don't know which Internet I searched for. At that time, I didn't have the system to learn the tr/sed/awk command.
It's easy to understand. find. type f-name "**"-print is used to search for all common files in the current directory with spaces in their names and print them out, in fact, the "find" function is redundant by default, and is then transmitted to the while loop for reading through the pipeline. The file name is put into the name variable, and the space is replaced by the underline with the tr command. If the names after execution are different, use the mv command to rename the name. However, this if judgment is dispensable, because find has queried all file names containing spaces, then after the tr command, the $ na variable must not be equal to the $ name variable.
Therefore, this code can be simplified:
Find.-type f-name "**" |
While read name; do
Na = $ (echo $ name | tr '''_')
Mv "$ name" "$ na"
Done
Tr can be viewed as a simplified version of sed. tr uses underscores to replace spaces.
There is also the sed version implementation:
For f in *; do mv "$ f" 'echo "$ f" | sed's/[] \ +/_/g''; done
The sed expression can also be written as follows:
Sed's/[[: space:] \ +/_/G'
Remember, the plus sign that appears once or multiple times in sed must be added with a backslash. That is, \ +.
Well, these two methods are too cool. Let's look at the rename implementation:
Rename's/[] +/_/G '*
OK is that simple.
The spaces in square brackets can be replaced by [: space,
That is, it can be written as 's/[[: space:] +/_/G'
Note that the rename uses the standard perl regular syntax, so you do not need to change the plus sign to the backslash plus sign.
That is, + cannot be changed to \ +. Otherwise, the replacement fails.
There are several interesting examples:
For example, add hello to the file header.
Rename's/^/hello /'*
..Htmlextension name changed to .htm
Rename's/. html $/. htm /'*
Add the suffix ". Zip" to the end of the system:
Rename's/$/. zip /'*
Unified remove. Zip Suffix:
Rename's/. zip $ //'*
Regular number name, such as 1.jpg, 2.jpg... 100.jpg. Now, the file name must be in the format of 1.jpg... 001.jpg.
Run the following two commands:
Rename's/^/00/'{0-9}.jpg # in this step, 1.jpg... 9.jpg is changed to 001.jpg... 009.jpg
Rename's/^/0/'{0-9}0-9}.jpg # This step changes 10.jpg... 99.jpg to 010.jpg... 090.jpg
Okay, rename has studied so much that we do not know how to introduce dynamic variables in rename for the moment, such as $ I ++
I tested I = 0; rename-n "s/^. * $/$ (+ + I)/"* after execution, I is incremented by 1, instead of adding one in each file as I imagined, this assumption may be due to the batch Implementation of rename, resulting in ++ I computing only once!
-N is used to test the rename process and does not run directly. You can view the test result and run it again.
Now, let's explain again. You must confirm the version of your language when using it. I am a C language version ~
RENAME (1) Linux Programmer's Manual RENAME (1)
Function:
Rename from to file...
Usage:
For example, given the files foo1,..., foo9, foo10,..., foo278, the commands
Rename foo foo0 foo?
Rename foo foo0 foo ??
Will turn them into foo001,..., foo009, foo010,..., foo278.
And
Rename. htm. html *. htm
Will fix the extension of your html files.
Here is an example:
Finally, let's look at the following figure for a question in practical application ~
Now we can see that we want to replace the ad letter in the image file name with big [Note: copy a copy and cannot replace it directly]. Then we can think about how to do this. By the way, we use rename ~
Cd/data/openshop_1028/IMG_SERVER/sources/goods/
Find./-name "* _ad.jpg"-exec cp "{}" {}. 1 \;
Find./-name "* _ad.jpg. 1"-exec renamead.jpg. 1 big.jpg {}\;
If it can be replaced directly, a command is provided:
Cd/data/openshop_1028/IMG_SERVER/sources/goods/
Find./-name "* _ad.jpg"-exec rename ad big {}\;
See the following tests ~
Author: zhuying_linux