使用Shell脚本重命名多个文件 [英] Renaming multiple files using a Shell Script
问题描述
我有一个名为t1.txt
,t2.tx
t,t3.txt
... t4.txt
的文件,我需要一个shell脚本来像这样重命名它:
I have files named t1.txt
, t2.tx
t, t3.txt
... t4.txt
and I need a shell script to rename it like this:
文件一:M.m.1.1.1.201108290000.ready
文件二:M.m.1.1.1.201108290001.ready
等等,后4位的序列号会更改.
etc, the sequence number in the last 4 digits changes.
如果有人帮助我,我将不胜感激:)
I'd be grateful if someone helped me :)
最好的问候
推荐答案
这可能是您需要的:
cd /home/me/Desktop/files/renam/
n=201108290000
for file in *.txt; do
echo $file
prefix=M.m.1.1.1.
file_name=M.m.1.1.1.$n.ready
echo $file_name
n=$(( $n+1 ))
mv $file $file_name
done
它与您自己编写的内容很接近,只是您错过了一些bash语法.请注意,您可能想更改n
的初始值,否则对于您提到的t1.txt
文件将变为M.m.1.1.1.201108290000.ready
.根据您的用途,这可能会造成混淆.
It's close to what you'd written yourself, you just missed some bash syntax. Note that you might want to change the initial value of n
, otherwise for the files you mentioned t1.txt
would become M.m.1.1.1.201108290000.ready
. Depending on what your use is, that might be confusing.
我还建议您避免将程序和内建函数的名称用作变量名,例如您的情况下为seq
.
I'd also advice you to avoid use the names of programs and builtins as variable names, such as seq
in your case.
这篇关于使用Shell脚本重命名多个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!