首页 > 解决方案 > 根据 MacOS 文件夹中的文件名更改文件创建和修改的日期和时间

问题描述

我在文件夹中有很多文件,文件名如下

20190618_213557.mp4
20190620_231105.mp4
20190623_101654.mp4
..

我需要在终端中使用 bash 脚本根据文件名 20190618_213557=YYYYMMDD_HHMMSS 更改创建日期和时间

标签: bashmacosdatetimetouch

解决方案


MACOS Uses touch -mt to change file creation/modification time. Here is the script:

    #!/bin/bash
    FILES="/Users/shakirzareen/Desktop/untitledfolder/*"
    for f in $FILES         ## f = full path + filename
    do
        t="${f##*/}"            ##remove folder path from filename
        t2="${t//_}"            ##remove _ from filename
        t3="${t2%%.*}"          ##remove file extension part
        touch -mt "$t3" $f  ##set creation and modification time of file
    done

推荐阅读