📄 renamere.txt
字号:
1999-10-31 Emil Brink About gentoo's RenameRE command1. INTRODUCTIONThis document contains a few notes, hints and tips on howto use the RenameRE command in gentoo. This command wasintroduced in the 0.11.10 release, which became availableearly November, 1999.2. PURPOSE OF RenameREThe purpose of the RenameRE command is to support flexiblerenaming. For example, you can use RenameRE to change theextension on a bunch of files with a single command, orreplace some other portion in a set of filenames. It's nota command that sees usage every day, but when you need ityou tend to need it badly. :) Hopefully, the RenameRE command will "save" youfrom having to throw together a script or a fancy shellcommand line. If you really enjoy writing those scripts,feel free not to use the command.3. OPERATING MODESFor maximum ease of use, RenameRE provides not only aregular expression-based replacement algorithm, but alsoa simpler replacement algorithm. Before executing the RenameRE command, selectone or more files. It often makes more sense to use thecommand when you have more than one file selected, butof course it will work on a single file as well. If youhave no files selected when envoking the command, nothinghappens (just as with most other file operation commandsin gentoo). When there is a selection, the command pops up asimple dialog letting you chose one of the two operatingmodes, and then start the renaming operation by clickingthe dialog's "OK" button. If you change your mind, justhit "Cancel" or the Escape key. You select operating mode by clicking the tabsin the top of the dialog; this also changes the dialog'scontents to show that mode's controlling widgets. When you hit "OK", RenameRE will operate in themode whose controls are visible.3.1 ReplacementThe first mode is called "simple". It operates like thetext replacement found in most editors, or like everybody'sfavorite Perl operator, s//. The simple replacement mode takes the followingparameters:"Replace" The string you to search for."With" The string you to replace any found occurances of the "Replace" string with."Ignore Case?" If checked, the search will ignore any differences in case between the "Replace" string and the filenames. Perl people, think of this as the /i option character."Replace All?" This causes the command to continue searching through the input filename even after having found and performed a replacement. In Perl, this looks like a trailing /g option.For example, if you had a filename of "abcabcabc" andspecified the parameters as replace="abc", with="ABC",and leave the two options at their default values offalse, the file would be renamed to "ABCabcabc". Checkthe "Replace All?" button, and the result is "ABCABCABC". The primary use for the Simple replacement modeis perhaps to change filename extensions. Doing thisrequires some care, however, since you cannot be surethat the string you find is indeed the extension, i.e.that it is last in the filename. For such control, youneed to use the Regular Expression mode.3.2 Regular ExpressionsThe second mode is a lot more complicated, but also alot more powerful and flexible. The basic idea is to match a regular expressionagainst each of the filenames. The expression can con-tain subexpressions (enclosed in round parens). If theexpression matches against an incoming filename, a secondstring is used to generate the new filename. In thissecond string, you can use $N to refer to the text inthe incoming filename that matched the N:th subexpressionin the regular expression. N should be a digit in therange 1..9. Any other text in the second string will becopied verbatim. To get a single dollar sign in thenew filename, use $$. Any other character following adollar sign will cause a parse error, and the renamewill be aborted. The parameters you can provide in the dialogare as follows:"From" This is where you enter the regular expression. You can use parenthesis to indicate subexpression, and use the matching text in the new filename."To" Here, you specify a string that forms the new filename. You can use $N to refer to the text that matched the N:th subexpression in the From string."Ignore Case?" If checked, the regular expression matcher will disregard differences in case.Let's use a huge example to make the operation of theregular expression RenameRE a bit clearer. Let's assumeyou have a set of HTML documents whose names have thegeneral form "gentoo-TOPIC(CATEGORY).htm", like this: "gentoo-commands(internals).htm", "gentoo-selecting(usage).htm", "gentoo-quitting(usage).htm", "gentoo-config(usage).htm", "gentoo-gui(internals).htm", "gentoo-re(misc).htm".We want to rename these so that the category (the wordin parenthesis) is moved to the front, the word gentoois removed, the actual content label is last before thedot, and that the extension is "html". To do this, wespecify the following expression in the "From" box: "^gentoo-([^(]+)\(([^)]+)\)\.htm$" 1 2 3 4 5 6 7 8This expression can be broken down:1. The caret (^) anchors the expression to the beginning of the incoming string, thus making sure that we begin at the very start. Without this, there could be any prefix before the "gentoo", and we would produce an incorrect result.2. The word "gentoo-" matches itself.3. The construct ([^(]+) matches one more more characters that are not opening parenthesis. This should collect the letters of the topic part of the incoming names.4. The backslash followed by opening paren "\(" matches exactly one opening parenthesis, which is the one between the topic and category parts of the filenames.5. ([^)]+) matches the category, up to (but not including) the closing parenthesis.6. Then follows a backslash-escaped closing parenthesis.7. The sequence "\.htm" matches a dot followed by the three letters "htm".8. The dollar sign anchors the expression to the end of the string, so that the ".htm" is really the final part of the filename.OK. That should take care of the incoming filenames, makesure they have the desired format, while at the same timestoring away the information we want to conserve. Now, wespecify a "To" string like this: "$2-$1.html"That's all there is! The $2 will insert the text thatmatched the second parenthesized subexpression, i.e. theone labeled 5 in the above breakdown. This will make thecategory appear first in the output. The dash goes straightthrough into the output name, followed by the topic asreferenced by the $1 operator. The dot and the "html" suffixgo straight through as well. Of course, we could have used a subexpression tomatch the "htm" extension as well, and entered "$3l" in the"To" string to add the final 'l', but that seemed like over-kill to me... :) The point of this example was to show how you cando fairly non-trivial permutations of the input filenames.A good understanding of regular expressions helps.4. CLOSING WORDSWell, that should introduce the RenameRE command sufficientlyand allow you to use it. The Simple mode is probably simpleenough to just use once you know what its parameters mean,while the full-blown Regular Expression mode might warrantsome practice. Anway, enjoy!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -