如何在shell脚本(或python)中一次连续重命名多个文件?



我有这个目录与多个*.mp4文件。我想添加序列号。以序作为前缀,仍保持原名称不变。我很新的shell脚本,(也无法在谷歌上找到一个适当的答案),所以这将是很好的,如果有人解释给我。如果可以用python来完成,那就更好了!(i know little bit)

The shell code i wrote:
n=01
for f in *.mp4; do
mv -- "$f" "$n+01. {f%.mp4}"
done
What my dir looked like:
***.mp4
***.mp4
***.mp4
What it looks like now:
01+01. {f%.mp4}
What I expect to happen:
01. ***.mp4
02. ***.mp4
.
.
12. ***.mp4

提前感谢您。

在Python中,有几种方法可以做到这一点,例如:

import os
os.chdir(path)  # Enter your desired path like r"D:My ArticlesPhdACCRUFER"
allFiles = os.listdir()
mp4Files = [file for file in allfiles if file.endswith('.mp4')]
for i, file in enumerate(files, 1):
index = str(i).zfill(2)   # to make 1 -> '01' 
newName = index + '.' + file
os.rename(file, newName)
print(file, 'Renamed to', newName)

最新更新