【发布时间】:2020-10-04 14:56:45
【问题描述】:
我需要在 Linux 中使用 bash 脚本将部分文件从 UTC 更改为 UTC+7
将 7 添加到小时字段。但是,如果这让我们过了午夜就会遇到麻烦,在这种情况下,必须增加日期,可能还有月份和年份。
文件位于目录和子目录中。
原始文件名示例
Video_learning_(incl._book)_443323_772233232_2020-03-19_10-32-36.mp4 Video_learning_(incl._book)_777323_226611115_2020-03-29_17-56-24.mp4
我已成功从 UTC 转换为 UTC+7 并且文件名变为
Video_learning_(incl._book)_443323_772233232_2020-03-19_17-32-36.mp4 Video_learning_(incl._book)_777323_226611115_2020-03-30_00-56-24.mp4
我已使用以下脚本成功地使用 Advance Renamer 转换了这些文件
match = item.name.match(/^(.*_)(\d{4}-\d{2}-\d{2})_(\d{2})-(\d{2})-(\d{2})$/);
epoch = Date.parse(match[2] + 'T' + match[3] + ':' + match[4] + ':' + match[5]);
date = new Date(epoch + (7* 3600000));
return match[1] + date.getFullYear()+ "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "_" + ("0" + date.getHours()).slice(-2) + "-" + ("0" + date.getMinutes()).slice(-2) + "-" + ("0" + date.getSeconds()).slice(-2);
但是需要将脚本修改成bash脚本在Linux中使用。如果创建任何新文件或文件夹,该脚本将运行。
我尝试将脚本修改如下并在 Centos 上运行,但它不起作用
for file in *.mp4; do
if [ -e "$file" ]; then
match = item.name.match(/^(.*_)(\d{4}-\d{2}-\d{2})_(\d{2})-(\d{2})-(\d{2})$/);
epoch = Date.parse(match[2] + 'T' + match[3] + ':' + match[4] + ':' + match[5]);
date = new Date(epoch + (7* 3600000));
newfilename = match[1] + date.getFullYear()+ "-" + ("0" + (date.getMonth() + 1)).slice(-2) + "-" + ("0" + date.getDate()).slice(-2) + "_" + ("0" + date.getHours()).slice(-2) + "-" + ("0" + date.getMinutes()).slice(-2) + "-" + ("0" + date.getSeconds()).slice(-2);
mv "$file" "$newfilename"
fi
done
【问题讨论】:
-
那是什么语言?你不能在脚本中间从 bash 切换到其他东西。
-
@choroba 显然,它是 Javascript。 @Agus,您应该将脚本保存在单独的
.js中并使用 bash 脚本中的node运行它,或者使用正确的 bash 语法而不是 JS 完全重写它。 -
"
The script will run if any new file [...] creation." 然后您还必须识别新文件(而不是使用*.mp4),否则您将再次转换已经转换的日期,从而导致UTC +14 甚至 UTC+21、+28 等等。
标签: javascript regex linux bash