rename

来自「android-w.song.android.widget」· 代码 · 共 123 行

TXT
123
字号
#! /bin/bash## original from:# @(#) rename.ksh 1.1 94/05/10# 90/06/01 John DuBois (spcecdt@armory.com)# 91/02/25 Improved help info# 92/06/07 remove quotes from around shell pattern as required by new ksh# 94/05/10 Exit if no globbing chars given.## conversion to bash v2 syntax by Chet Rameyphelp(){echo "$usageAll files that match oldpattern will be renamed with thefilename components that match the constant parts of oldpatternchanged to the corresponding constant parts of newpattern.The components of the filename that match variable parts ofoldpattern will be preserved.  Variable parts in oldpatternmust occur in the same order in newpattern.  Variables partscan be '?' and '*'.Example: rename \"/tmp/foo*.ba.?\" \"/tmp/new*x?\"All files in /tmp that match foo*.ba.? will have the \"foo\" partreplaced by \"new\" and the \".ba.\" part replaced by \"x\"."}usage="usage: $name [-htv] oldpattern newpattern"name=${0##/}while getopts "htv" opt; do    case "$opt" in    t) tell=true;;    v) verbose=true;;    h) phelp; exit 0;;    *) echo "$name: $usage" 1>&2; exit 2;;    esacdoneshift $((OPTIND - 1))if [ $# -lt 2 ]; then    phelp    exit 2fioldpat=$1newpat=$2set -- $1if [ ! -e "$1" ]; then    echo "$name: no files match $oldpat."    exit 1fitypeset -i i=1 j# Example oldpat: foo*.a# Example newpat: bar*.b# Examples given for first iteration (in the example, the only interation)while :; do    case "$oldpat" in    *[\*\?]*)	;;    *)		break;;    esac    # Get leftmost globbing pattern in oldpat    pat=${oldpat#*[\*\?]}	# pat=.a    pat=${oldpat%%"$pat"}	# pat=foo*    pat=${pat##*[!\?\*]}	# pat=*    # Find parts before & after pattern    oldpre[i]=${oldpat%%"$pat"*}	# oldpre[1]=foo    oldsuf[i]=${oldpat#*"$pat"}		# oldsuf[1]=.a    newpre[i]=${newpat%%"$pat"*}	# newpre[1]=bar    # Get rid of processed part of patterns    oldpat=${oldpat#${oldpre[i]}"$pat"}	# oldpat=.a    newpat=${newpat#${newpre[i]}"$pat"}	# newpat=.b    let i=i+1doneif [ $i -eq 1 ]; then    echo "No globbing chars in pattern." 1>&2    exit 1fioldpre[i]=${oldpat%%"$pat"*}	# oldpre[2]=.aoldsuf[i]=${oldpat#*"$pat"}	# oldsuf[2]=.anewpre[i]=${newpat%%"$pat"*}	# newpre[2]=.bif [ -n "$verbose" ]; then    j=1    while let "j < i"; do	echo \"Old prefix: ${oldpre[j]}   Old suffix: ${oldsuf[j]}   New prefix: ${newpre[j]}"	let j=j+1    donefi# Example file: foox.afor file; do    j=1    origname=$file	# origname=foox.a    newfile=    while let "j <= i"; do	# Peel off a prefix	interation	1		2	file=${file#${oldpre[j]}}		# file=x.a	file=	# Save the part of this prefix that is to be retained	const=${file%${oldsuf[j]}}		# const=x	const=	newfile=$newfile${newpre[j]}$const	# newfile=barx	newfile=barx.b	file=${file#$const}			# file=.a	file=.a	let j=j+1    done    if [ -n "$tell" ]; then	echo "Would move \"$origname\" to \"$newfile\"."    else	if [ -n "$verbose" ]; then	    echo "Moving \"$origname\" to \"$newfile\"."	fi	mv $origname $newfile    fidone

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?