shell批量重命名目录下的文件(修改为从某个数字开始的数字)
发布时间:2014-12-12 编辑:jiaochengji.com
分享一例shell脚本,实现将目录下的所有文件进行改名,修改为从某个数字开始的数字,对文件进行重命名,有需要的朋友参考下。
一例批量重命名文件名称的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 # www.jiaochengji.com
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
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 # www.jiaochengji.com
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批量重命名目录下的文件(修改为从某个数字开始的数字)
深入解析tcsh的初始化配置文件
for语句用法
xcopy命令批处理拷贝文件或文件夹
inux shell初级入门教程
批处理的字符串处理
简单批处理命令简介
shell批量修改文件后缀名
dos命令for用法详解
PHP传递数组格式参数到shell脚本中
[关闭]