F6我们可以在麦克。例如,我知道如何使用此菜单中的某些正则表达式标记子集进行搜索和替换。但如果我需要通过在新文件名中添加数字计数器来重命名一组文件,该怎么办?例如,给定选择:
a.sh
b.sh
c.sh
我想将文件重命名为:
01.sh
02.sh
03.sh
有没有什么反制令牌?
答案1
没有反制令牌这样做。但 Midnight-Commander 太强大了,不可能轻易被击败!在您的用户菜单中创建此新条目(Command > Edit menu file > User
或~/.config/mc/menu
):
x Rename with 00->99 counter preserving extension
i=1
for file in %s; do
extension=.${file##*.}
[ "$extension" = ".$file" ] && extension=""
cnt=$(printf '%%02d' "$i")
mv -- "$file" "$cnt$extension"
i=$((i+1))
done
然后选择要重命名的文件,用 调出用户菜单F2,用 选择新操作x。
示例执行:
$ ls
a.sh b.sh c.sh 'h 10' 'zr.X&*!#@.f90'
选择所有文件并应用操作后:
$ ls
01.sh 02.sh 03.sh 04 05.f90
shell 脚本解释。
i=1
# %s expands to all selected files and we loop through each
for file in %s; do
#This gets the extension of the file by removing everything up to the last dot
extension=.${file##*.}
#If the file had no extension, set extension to null
[ "$extension" = ".$file" ] && extension=""
#Format the counter appropriatedly, zero padding 1 to 9
#A double % is needed because MC interprets % especially, as noticed above with %s
cnt=$(printf '%%02d' "$i")
#Perform the renaming in a file, preserving extension (if existing)
mv -- "$file" "$cnt$extension"
i=$((i+1)) #Increment counter
done