0

I have 4000 files, and I need to add the nrs 1 to 4000 at the beginning of all filenames.

For example:

file_a.CEL
file_c.CEL
file_g.CEL
file_x.CEL
...
other_file.CEL

Should become:

1_file_a.CEL
2_file_c.CEL
3_file_g.CEL
4_file_x.CEL
...
4000_other_file.CEL

It is important that the underscore after the number also gets added. The filenames are all totally different (there is no system to the filenames), and it also doesn't really matter in what order they are numbered. Is there an easy way to do this using bash? Many thanks in advance!

4

3 回答 3

2

使用for循环mv应该会给你想要的效果。这不是一个特别有趣的解决方案,但它很简单。

COUNT=1
for file in ./*; do
    mv "$file" "${COUNT}_$file"
    let COUNT++
done
于 2013-07-31T12:18:24.733 回答
1
i=1
for f in *; do 
   echo Renaming file \"$f\" to \"${i}_${f}\"
   mv "$f" "${i}_${n}"
   i=$((i+1))
done
于 2013-07-31T12:17:44.403 回答
1

相关主题:使用 Shell 脚本重命名多个文件

在您的情况下,您可以执行以下操作:

n = 1
for file in *.CEL; do
   new_name=$n_$file
   n=$(($n+1))
   mv $file $new_name
done
于 2013-07-31T12:21:58.677 回答