?? ren
字號(hào):
#!/bin/bash#@ This program came from: ftp://ftp.armory.com/pub/scripts/ren#@ Look there for the latest version.#@ If you don't find it, look through http://www.armory.com/~ftp/## @(#) ren 2.1.1 2002-03-17# 1990-06-01 John H. DuBois III (john@armory.com)# 1991-02-25 Improved help info# 1992-06-07 Remove quotes from around shell pattern as required by new ksh# 1994-05-10 Exit if no globbing chars given.# 1995-01-23 Allow filename set to be given on command line.# 1997-09-24 1.4 Let [] be used for globbing. Added x option.# 1997-11-26 1.4.1 Notice if the sequences of globbing chars aren't the same.# 1999-05-13 Changed name to ren to avoid conflict with /etc/rename# 2000-01-01 1.4.2 Let input patterns that contain whitespace be used.# 2001-02-14 1.5 Better test for whether old & new globbing seqs are identical.# 2001-02-20 1.6 Added pP options.# 2001-02-27 1.7 Added qf options. Improved interpretation of rename patterns.# 2001-05-10 1.8 Allow multiple pP options. Added Qr options.# 2001-07-25 2.0 Added mz options.# 2001-11-25 2.1 Allow segment ranges to be given with -m. Work under ksh93.# 2002-03-17 2.1.1 Fixed bug in test for legal expressions.# todo: It would be nice to be able to escape metacharacters with '\'# todo: Should enhance patterns to make ] in a pair of brackets work ([]])# todo: Allow use of all ksh globbing patterns.# todo: Allow use of extended regexps, with () to enumerate pieces and \num to# todo: select them.## Modifications for bash made by Chet Ramey <chet@po.cwru.edu>name=${0##*/}Usage="Usage:$name [-fhqtv] [-m<segstart[:segend]=operation>] [-z<len>] [-[pP]<pattern>] oldpattern [newpattern [filename ...]]or$name -r [same options as above] oldpattern newpattern directory ..."tell=falseverbose=falsewarn=truewarnNoFiles=truedebug=falserecurse=falseinclPat=exclPat=declare -i inclCt=0 exclCt=0check=truedeclare -i j op_end_seg# Begin bash additionsshopt -s extglob## ksh print emulation## print [-Rnprsu[n]] [-f format] [arg ...]## - end of options# -R BSD-style -- only accept -n, no escapes# -n do not add trailing newline# -p no-op (no coprocesses)# -r no escapes# -s print to the history file# -u n redirect output to fd n# -f format printf "$format" "$@"#print(){ local eflag=-e local nflag= fflag= c local fd=1 OPTIND=1 while getopts "fRnprsu:" c do case $c in R) eflag= ;; r) eflag= ;; n) nflag=-n ;; s) sflag=y ;; f) fflag=y ;; u) fd=$OPTARG ;; p) ;; esac done shift $(( $OPTIND - 1 )) if [ -n "$fflag" ]; then builtin printf "$@" >&$fd return fi case "$sflag" in y) builtin history -s "$*" ;; *) builtin echo $eflag $nflag "$@" >&$fd esac}# End bash additionswhile getopts :htvxp:P:fqQrm:z: opt; do case $opt in h) print -r -- \"$name: rename files by changing parts of filenames that match a pattern.$Usageoldpattern and newpattern are subsets of sh filename patterns; the onlyglobbing operators (wildcards) allowed are ?, *, and []. All filenames thatmatch oldpattern will be renamed with the filename characters that match theconstant (non-globbing) characters of oldpattern changed to the correspondingconstant characters of newpattern. The characters of the filename that matchthe globbing operators of oldpattern will be preserved. Globbing operatorsin oldpattern must occur in the same order in newpattern; for every globbingoperators in newpattern there must be an identical globbing operators inoldpattern in the same sequence. Both arguments should be quoted sinceglobbing operators are special to the shell. If filenames are given, onlythose named are acted on; if not, all filenames that match oldpattern are actedon. newpattern is required in all cases except when -m is given and no furtherarguments are given.If you are unsure whether a $name command will do what you intend, issue itwith the -t option first to be sure.Examples:$name \"/tmp/foo*.ba.?\" \"/tmp/new*x?\" All filenames in /tmp that match foo*.ba.? will have the \"foo\" part replaced by \"new\" and the \".ba.\" part replaced by \"x\". For example, /tmp/fooblah.ba.baz would be renamed to /tmp/newblahxbaz.$name \* \*- foo bar baz foo, bar, and baz will be renamed to foo-, bar-, and baz-.$name '????????' '????-??-??' All filenames that are 8 characters long will be changed such that dashes are inserted after the 4th and 6th characters.Options:-h: Print this help.-r: Recursive operation. Filenames given on the command line after oldpattern and newpattern are taken to be directories to traverse recursively. For each subdirectory found, the specified renaming is applied to any matching filenames. oldpattern and newpattern should not include any directory components.-p<pattern>, -P<pattern>: Act only on filenames that do (if -p is given) or do not (if -P is given) match the sh-style filename globbing pattern <pattern>. This further restricts the filenames that are acted on, beyond the filename selection produced by oldpattern and the filename list (if any). <pattern> must be quoted to prevent it from being interpreted by the shell. Multiple instances of these options may be given. In this case, filenames are acted on only if they match at least one of the patterns given with -p and do not match any of the patterns given with -P.-m<segstart[:segend]=operation>: For each file being renamed, perform a mathematical operation on the string that results from concatenating together the filename segments that matched globbing operator numbers segstart through segend, where operators are numbered in order of occurrence from the left. For example, in the pattern a?b*c[0-9]f, segment 1 consists of the character that matched ?, segment 2 consists of the character(s) that matched *, and segment 3 consists of the character that matched [0-9]. The selected segments are replaced with the result of the mathematical operation. The concatenated string must consist of characters that can be interpreted as a decimal integer; if it does not, the filename is not acted on. This number is assigned to the variable 'i', which can be referenced by the operation. The operations available are those understood by the ksh interpreter, which includes most of the operators and syntax of the C language. The original filename segment is replaced by the result of the operation. If -m is used, newpattern may be an empty string or not given at all (if no directory/file names are given). In this case, it is taken to be the same as oldpattern. If segend is given, any fixed text that occurs in the pattern between the starting and ending globbing segments is discarded. If there are fewer globbing segments than segend, no complaint is issued; the string is formed from segment segstart through the last segment that does exist. If segend is not given, the only segment acted on is startseg. Examples: $name -m3=i+6 '??*.ppm' This is equivalent to: $name -m3=i+6 '??*.ppm' '??*.ppm' Since the old pattern and new pattern are identical, this would normally be a no-op. But in this case, if a filename of ab079.ppm is given, it is changed to ab85.ppm. $name '-m1:2=i*2' 'foo??bar' This will change a file named foo12bar to foo24bar $name '-m1:2=i*2' 'foo?xyz?bar' This will also change a file named foo1xyz2bar to foo24bar-z<len>: Set the size of the number fields that result when -m is used. The field is truncated to the trailing <len> digits or filled out to <len> digits with leading zeroes. In the above example, if -z3 is given, the output filename will be ab085.ppm. -f: Force rename. By default, $name will not rename files if a file with the new filename already exists. If -f is given, $name will carry out the rename anyway.-q: Quiet operation. By default, if -f is given, $name will still notify the user if a rename results in replacement of an already-existing filename. If -q is given, no notification is issued.-Q: Suppress other warnings. By default, a warning is issued if no files are selected for acting upon. If -Q is given, no warning is issued.-v: Show the rename commands being executed.-t: Show what rename commands would be done, but do not carry them out." exit 0 ;; f) check=false ;; q) warn=false ;; Q) warnNoFiles=false ;; r) warnNoFiles=false recurse=true ;; t) tell=true ;; v) verbose=true ;; x) verbose=true debug=true ;; p) inclPats[inclCt]=$OPTARG ((inclCt+=1)) ;; P) exclPats[exclCt]=$OPTARG ((exclCt+=1)) ;; m) # Store operation for each segment number in ops[num] # Store ending segment number in op_end_seg[num] range=${OPTARG%%=*} op=${OPTARG#*=} start=${range%%:*} end=${range#*:} if [[ "$start" != +([0-9]) || "$start" -eq 0 ]]; then print -ru2 -- "$name: Bad starting segment number given with -m: $start" exit 1 fi if [[ "$end" != +([0-9]) || "$end" -eq 0 ]]; then print -ru2 -- "$name: Bad ending segment number given with -m: $end" exit 1 fi if [[ start -gt end ]]; then print -ru2 -- "$name: Ending segment ($end) is less than starting segment ($start)" exit 1 fi if [[ "$op" != @(|*[!_a-zA-Z0-9])i@(|[!_a-zA-Z0-9]*) ]]; then print -ru2 -- \ "$name: Operation given with -m does not reference 'i': $op" exit 1 fi # Test whether operation is legal. let returns 1 both for error # indication and when last expression evaluates to 0, so evaluate 1 # after test expression. i=1 let "$op" 1 2>/dev/null || { print -ru2 -- \ "$name: Bad operation given with -m: $op" exit 1 } ops[start]=$op op_end_seg[start]=$end ;; z) if [[ "$OPTARG" != +([0-9]) || "$OPTARG" -eq 0 ]]; then print -ru2 -- "$name: Bad length given with -z: $OPTARG" exit 1 fi typeset -Z$OPTARG j || exit 1 ;; +?) # no way to tell getopts to not treat +x as an option print -r -u2 "$name: Do not prefix options with '+'." exit 1 ;; :) print -r -u2 \"$name: Option -$OPTARG requires a value.$UsageUse -h for help." exit 1 ;; \?) print -r -u2 \"$name: -$OPTARG: no such option.$UsageUse -h for help." exit 1 ;; esacdone # remove args that were options
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -