rename batch modify file name

From:https://www.cnblogs.com/lly001/p/10221021.html

Mv rename the difference is that mv can only rename a single file, and rename batches can modify the file name

The rename linux there are two versions, one version of the C language, one is the Perl version of the system is how to distinguish which version of rename, you can look at ways:

Enter the man rename the first row contains Linux Programmer's Manual see, this is the C language version, if the first line contains Perl Programmers Reference Guide, it is the Perl version.

Perl language version of rename usage

perl versions rename sed with the similar syntax, rename perl regular expression to be processed files

Alternatively rename 's / .rtv.txt.kml / .kml /' * .kml or rename 's / foo0 / foo /' foo0 [2] * at the beginning of the file name in foo0 foo02 replace foo

Delete some characters rename 's / gps _ //' * .kml or rename 's / .bak $ //' * .bak

Wherein,? * May be single character Alternatively multiple characters, all three may be combined with a wildcard.

Example:

The size of the letters contained in all file names, modify lowercase: rename 'y / AZ / az /' * .txt 

When allowed linux file name contains spaces, but there are spaces in the file name, the command line when calling the file you want the file name in double quotation marks or spaces to escape to the line, remove the file name has spaces in order to achieve the following two ways:

    1. find . -type f -name "* *" -print |while read name; do mv "${name}" `echo "${name}"|sed "s/[ ]\+/_/g"`;done
    2. rename "s/[ ]+/_/g" *
    • Wherein [] + indicates one or more occurrences space, space can be used [: space:] in place of, the order may be replaced [[: space:]] +
    • + You need to escape, but could not escape in the rename in sed because rename uses standard perl regular grammar
    • Command can be seen from the difference between the rename sed, the rename process is that all the files can be matched to the * to the directory, all file names it as the object of processing, the processing logic sed command file name, process sed the content of the document, it can only echo "filename" | sed "s / [] \ + / _ / g way to deal with the file name

  rename 's / ^ / hello /' * uniform in the file header is added on hello 

  rename 's / .html $ /. htm /' * unity to modify the .htm .html extension

  rename 's / $ / zip /.' * .zip suffix is ​​added at the end of unity:

  rename 's / .zip $ //' * .zip suffix removed unity

  Rules of the numbered names, such as 1.jpg, 2.jpg ..... 100.jpg, and now all three to make the file name that is 1.jpg .... 001.jpg, run two commands:

  1. rename 's / ^ / 00 /' [0-9] .jpg # This step is changing the 1.jpg ..... 9.jpg 001.jpg .... 009.jpg
  2. rename 's / ^ / 0 /' [0-9] [0-9] .jpg # This step of changing the 10.jpg ..... 99.jpg 010.jpg ..... 090.jpg

C language version rename usage

     rename the original string into the string needs to be modified files

  rename 'test' 'tast' * .txt, the file name was changed to tast test

  Extension rename .jpeg.jpg * .jpeg file modification

Overall: rename C language version can realize the function: batch modify the file name, the result is that each file will be replaced the same string! In other words, can not be achieved, such as cycling and press a number to rename, etc.

Guess you like

Origin www.cnblogs.com/mianbaoshu/p/11772876.html