使用 bash 在连字符之间批量重命名文件名
Batch renaming filenames between hyphens using bash
我是一名学生,我对 bash 还很陌生,非常感谢您的帮助!
我正在尝试重命名一批文件,如下所示:local_date_1415+6556_0001.txt 和 local_date_1415+6556_0002.txt。
示例文件名:uuw_07052006_1415+6556_0001.txt
我需要每个文件名的“1415+6556”部分前面有一个2M,比如“2M1415+6556”。文件夹中一半左右的文件已经有2M了,所以我不能只搜索字符串并替换。
有没有办法使用“_”作为分隔符来重命名这批文件,以便我可以用正确的字符串完全替换所有第三部分?
我的机器上有重命名命令,我只是不确定如何在这里使用它。
使用您的 rename
版本:
rename _ % *_????+*.txt # replace the first underscore with a percent
rename _ _2M *_????+*.txt # add 2M after the second underscore
rename % _ *_2M????+*.txt # return the first underscore back
仅当您的文件名不包含 %
时才有效。如果是,请选择其他角色。
你也可以自己写循环:
#! /bin/bash
for f in *_????+*.txt ; do
before=${f%[0-9][0-9][0-9][0-9]+*}
after=${f#*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]}
mv "$f" "$before"2M"$after"
done
我是一名学生,我对 bash 还很陌生,非常感谢您的帮助!
我正在尝试重命名一批文件,如下所示:local_date_1415+6556_0001.txt 和 local_date_1415+6556_0002.txt。
示例文件名:uuw_07052006_1415+6556_0001.txt
我需要每个文件名的“1415+6556”部分前面有一个2M,比如“2M1415+6556”。文件夹中一半左右的文件已经有2M了,所以我不能只搜索字符串并替换。
有没有办法使用“_”作为分隔符来重命名这批文件,以便我可以用正确的字符串完全替换所有第三部分?
我的机器上有重命名命令,我只是不确定如何在这里使用它。
使用您的 rename
版本:
rename _ % *_????+*.txt # replace the first underscore with a percent
rename _ _2M *_????+*.txt # add 2M after the second underscore
rename % _ *_2M????+*.txt # return the first underscore back
仅当您的文件名不包含 %
时才有效。如果是,请选择其他角色。
你也可以自己写循环:
#! /bin/bash
for f in *_????+*.txt ; do
before=${f%[0-9][0-9][0-9][0-9]+*}
after=${f#*_[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]}
mv "$f" "$before"2M"$after"
done