我需要一些命令行功能。
我有一堆文件,以 4 个数字开头,然后是破折号,然后是各种字母,然后是扩展名,例如。
0851_blahblah_p.dbf
0754_asdf_l.dbf
我想要的是将四个数字移动到文件名的末尾(保持扩展名不变)并删除下划线。因此上面的例子将被重命名:
blahblah_p0851.dbf
asdf_l0754.dbf
感谢所有帮助。
我正在运行 ubuntu。
谢谢DJ
最佳答案
这是一个纯 bash
的解决方案:
for file in *.dbf; do
ext=${file##*.};num=${file%%_*};name=${file%.*};name=${name#*_}
mv $file $name$num"."$ext;
done
分解评论:
for file in *.dbf
do
ext=${file##*.} # Capture the extension
num=${file%%_*} # Capture the number
name=${file%.*} # Step 1: Capture the name
name=${name#*_} # Step 2: Capture the name
mv "$file" "$name$num.$ext" # move the files to new name
done
关于linux - 在ubuntu中批量重命名文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22242570/