2013-06-18 59 views

回答

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進行重命名。

+0

'((我++))#或I = $((I + 1)),或讓I = I + 1' – devnull

+0

因此,我們有一個第三選擇。你知道哪一個最好? – fedorqui

+1

很難說哪個是最好的。全部執行算術運算。順便說一句,'讓我++'更可讀恕我直言。 – devnull

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:]'