Renaming multiple files using a Shell Script

ぃ、小莉子 提交于 2019-12-08 14:33:59

问题


I have files named t1.txt, t2.txt, t3.txt ... t4.txt and I need a shell script to rename it like this:

file one: M.m.1.1.1.201108290000.ready

file two: M.m.1.1.1.201108290001.ready

etc, the sequence number in the last 4 digits changes.

I'd be grateful if someone helped me :)

Best Regards


回答1:


This might be what you need:

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

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.

I'd also advice you to avoid use the names of programs and builtins as variable names, such as seq in your case.



来源:https://stackoverflow.com/questions/7231248/renaming-multiple-files-using-a-shell-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!