linux - 在ubuntu中批量重命名文件

标签 linux command-line sed

我需要一些命令行功能。

我有一堆文件,以 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/

相关文章:

linux - 同时执行 awk 和 sed 命令

linux - 根据映射对字符串进行更改

linux - sed 的奇怪输出

windows - 如果程序通过批处理脚本运行,如何启动 wav 文件

python - 如何阻止URL被注释掉(C)

c - 时间服务器时间类型问题

c - 一种在不知道签名的情况下包装 C 函数调用的方法?

android - 如何从 shell 获取有关 Android shell 中可用命令的信息?

Android UI 测试 : "adb shell uiautomator" throws error, "uiautomator"在 "adb shell"工作

linux - 明确捕获和处理的异常是否会导致切换到内核模式?