重命名文件夹中的文件(linux)

时间:2013-06-18 09:57:21

标签: linux terminal batch-rename

我在文件夹中有不同的HTML文件。如何重命名文件,使其名称为:

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

2 个答案:

答案 0 :(得分: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重命名。

答案 1 :(得分: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:]'