Linux rename command file renaming usage summary

  
                

The rename command in the Linux command is mainly used to rename files. It is similar to the mv command, but rename can rename batch files, while mv commands can only rename individual files. This section describes how to use the Linux rename command.

Linux's rename command has two versions, one version of the C language, is a version of the Perl language, the early Linux distributions basically using C language version, is now very It's hard to see the C language version. For historical reasons, when the Perl language is in full swing, Linux tool developers believe that Perl can replace C, so most of the tools that were originally C versions have been rewritten by Perl because The Perl version supports regular processing, so the function is more powerful, and the C language version is no longer needed.

How to distinguish which 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)

Then this is the C language version.

And if it appears:

RENAME(1) Perl Programmers Reference Guide RENAME(1)

This is the Perl version!

Syntax differences between the two versions:

In C language, according to the annotation above man,

The syntax of rename is:

rename fromtofile< Br>

This command has three parameters, which are: from: change the name, to: change to what name, file which file needs to be modified.

Example usage:

For example, there are a number of files that start with log, log001.txt, log002.txt ……. Until log100.txt

Now I want to replace all the logs of this batch of files with history

rename log history log*

The meaning of this command is very clear, Replace the log characters in all files starting with log with history

. The replaced file is: history001.txt, history002.txt …. . Until history100.txt

rename Another C example of the C language version is to modify the suffix name in bulk,

For example, we want to change all jpeg suffix image files to jpg files.

rename .jpeg.jpg*.jpeg

Thus, all extensions with .jpeg extensions are all modified to .jpg

Now summarize the rename C language version. What you can do: Modify the file name in batches. The result is that each file will be replaced with the same string! That is to say, it is impossible to implement such as loop and then rename by number!

Batch renaming of Perl versions, with the benefit of Perl, is that you can use regular expressions to do very fancy features.

Param version of the parameter format:

rename perlexprfiles

Note that the perl version of rename has only two parameters, the first parameter is perl regular expression, the second The parameter is the file to be processed

Help example for man rename:

1) There is a batch of files ending with .bak, and now I want to remove all these .bak.

rename ‘s/\\.bak$//’ *.bak

This command is very simple, because I have not systematically studied perl, I don't know the replacement string in perl Is it doing this, but sed is doing this, so if you have a sed or tr basis, it is easy to understand that this substitution is exactly the same as the regular syntax in sed.

2) Change all file names containing small and small letters to lowercase letters.

rename & lsquo;y/A-Z/a-z/& rsquo; *

Still the same as sed's replacement grammar, no need to explain more, if you can't read it, you can learn sed first.

There are several more practical examples:

Batch removal of spaces in file names

Linux file names originally do not support spaces, I don't know when it is allowed. Of course, when calling the file on the command line, the space is very problematic. For example, you can directly mv oldfile newfile but there is a space, you can add double quotes: mv “oldfile” “newfile” Slash transfer \\[], this is fine, but if you directly introduce the name of the picture containing the space into the Latex document, Latex will directly print out the file name when generating the pdf. This problem has been a long time for me. How about the pdf I generated? What is the name of the file? Later I discovered that it was a problem with spaces in the file name! The file name generated under windows system is born with spaces. Although it is very annoying, some images generated by HP scanners add spaces by default. There is no way to remove them. Before the system research rename command, I removed it with mv. Spaced.
Previous12Next Total 2 Pages

Copyright © Windows knowledge All Rights Reserved