0

我在一个文件夹中有不同的 HTML 文件。如何重命名文件,使其具有以下名称:

1.html
2.html
3.html
...

4

2 回答 2

2

这可以使它:

i=1
for file in /your/folder/*
do
   mv $file ${i}.html
   i=$((i+1)) #((i++)) was giving errors (see comments)
done

它遍历所有文件/your/folder并根据$i不断增加的数字重命名它们。

于 2013-06-18T09:59:08.520 回答
1

这是我的脚本

#!/bin/sh
#
#       batchrename - renames files like 01.ext, 02.ext ...
#   
#   format :  batchrename <list of files>
#        or:              -r <extension> <<list of files> or <dir>>
#   -r - recoursively
counter=0
extrec=""
if [ "$#" -lt "1" ]; then
    echo -e "\n\t\tUsage:\n\tbatchrename [opt]\nopt:"
    echo -e "-r <ext> <folder> (or file list) -- renames recoursively ALL files"
    echo -e "\tin folder <folder> (or by file list given)  with extension .<ext>"
    echo -e "<folder> -- renames ALL files in folder given"
    echo -e "<file list> -- renames ALL files of given filelist.\n\n"
    exit 0
fi
Name="$*"
if [ "$1" = "-r" ]; then
    extrec="$2"
    shift
    shift
    Name="$*"
    [ "$Name" = "" ] && Name="./"
fi
echo -e "\n\t\t\tRENAMING"

for file in $Name
do
file=`echo "$file" | sed "s/<>/ /g"`
    if [ -d "$file" ];then
    echo -e "\nDiving into \033[38m $file \033[39m"
    cd "$file"
    if [ "$extrec" != "" ]; then
        batchrename -r $extrec `ls -1 | sed "s/\ /<>/g"`
    else
        batchrename `ls -1 | sed "s/\ /<>/g"`   
    fi
    cd ../
    continue
    fi
    ext=`ext "$file"`
    if [ "$ext" = "ion" ]; then
    continue
    fi
    if [ "$extrec" = "" -o "$ext" = "$extrec" ];then
        counter=`expr $counter + 1`
    echo -e "Progress: $counter files\r\c"
        mv "$file" "rnmd$counter.$ext"
    fi
done
echo -e "\n\n\t\t\tENDING"
digits=`echo $counter|awk '{print length ($0)}'`
cnt=1
while [ $digits -gt $cnt ]
do
    f=`ls -S -1|grep "rnmd[0-9]\{$cnt\}\."`
    rename rnmd rnmd0 $f
    cnt=`expr $cnt + 1`
done
if [ "$counter" -gt "0" ]; then
    rename rnmd "" rnmd*
fi
echo -e "\n\t\t\tDone !!!\n"

重命名所有文件后,将看起来像 001.file、002.file、... 等等。前导零的数量取决于文件的数量。因此,重命名ls后将显示正确的文件顺序!

它使用中间脚本ext

#!/bin/sh
#
#       ext - returns file suffix (case-unsensitive)
#
File="$*"
if [ -d "$File" ]; then
    echo ""
    exit 0
fi
EXT=`echo $File|sed 's/.\{1,\}\.//g'`
if [ "$EXT" = "$File" ]; then
    EXT=""
fi
echo $EXT| tr '[:upper:]' '[:lower:]'
于 2013-06-18T10:05:12.627 回答