【问题标题】:Bash script to convert part of filename from UTC to UTC+7将部分文件名从 UTC 转换为 UTC+7 的 Bash 脚本
【发布时间】: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


【解决方案1】:

假设您的文件的时间戳根据您的命名约定与嵌入文件名的时间相匹配,您可以使用ls --time-style 中提供的时区转换功能结合环境变量TZ 来复制或重命名您的文件:

#!/bin/bash
TZ=${1:-"UTC-7"}
find . -name "*.mp4" | grep -v "TIMEZONEADJUSTED" \
| while IFS= read -r filepath; do
        # In POSIX systems, a user can specify the time zone by means of the TZ environment variable:
        TZ=$TZ ls -g --time-style '+%Y-%m-%d_%H-%M-%S' $filepath | cut -d' ' -f5-6 \
        | while IFS=' ' read newtime filename; do
                filenamebase=${filename%_*}; filenamebase=${filenamebase%_*}
                fileextension=${filename##*\.};
                cp --verbose $filename ${filenamebase}_${newtime}_TIMEZONEADJUSTED.${fileextension}
        done
done

这是在我的系统上测试运行期间脚本的输出:

~/PROJECTS/stackoverflow/tmp$ ./convert-tz.sh 
'./Video_learning_(incl._book)_443323_772233232_2020-03-19_10-32-36.mp4' -> './Video_learning_(incl._book)_443323_772233232_2020-03-19_17-32-36_TIMEZONEADJUSTED.mp4'
'./Video_learning_(incl._book)_777323_226611115_2020-03-29_17-56-24.mp4' -> './Video_learning_(incl._book)_777323_226611115_2020-03-30_00-56-24_TIMEZONEADJUSTED.mp4'
'./tmp2/Video_learning_(incl._book)_443323_772233232_2020-03-19_10-32-36.mp4' -> './tmp2/Video_learning_(incl._book)_443323_772233232_2020-03-19_17-32-36_TIMEZONEADJUSTED.mp4'

注意:TIMEZONEADJUSTED 用作文件名已被调整的标记,因此@Socowi 在上面的评论中描述的情况不会发生。
我在脚本中添加了cp 而不是mv,因此您可以先测试该脚本是否适合您,而无需修改您的原始文件。如果是这样,您仍然可以用移动替换副本。

【讨论】:

    猜你喜欢
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2021-09-07
    • 1970-01-01
    • 1970-01-01
    • 2011-07-31
    • 1970-01-01
    相关资源
    最近更新 更多