shell实现把一个目录下的文件名字改为从某个数字开始的数字

 

#!/bin/bash
print_error_info(){
	cat <<ERROR_EOF
error !
use option h to get help infomation 
ERROR_EOF
}
st=0
while getopts "s:p:h" OPT
do
	case $OPT in 
	s)
	st=$OPTARG
	;;
	p)
	path=$OPTARG
	;;
	h)
	cat <<HELP_EOF
DESCRIPTION	
	This script changes the name of files under directory specified by option p to continuous number which starts from 	
a number specified by option s. The extension name of the file won't be changed

OPTIONS
	-s 
		specify the initiating number 
	-p 
		specify the directory in which files that will be changed reside
	-h 
		output this help document
HELP_EOF
	exit 0
	;;
	esac
done
if [ -z "$path" ] ; then 
	print_error_info
	exit 1
fi	
cnt=0
find "$path" -maxdepth 1 -type f  | while read line 
do
	name=$(basename $line)	
	echo $line
	if [ $(echo $name | awk '{ print match($0, "\."); }') -eq "0" ]; then 
		name="$cnt"	
	else 
		name="$cnt"."$(echo $name | awk -F . '{ print $NF }')"
	fi 
	name="$(dirname $line)"/$name
	mv $line $name
	let "cnt=$cnt+1"
done

 

拍摄了大量的图片而不想给图片标上标题就可以用这个脚本来修改文件名了!

 

你可能感兴趣的:(shell)