Change Date and Time of Creation and Modification of file based on filename in MacOS folder

后端 未结 3 1932
感动是毒
感动是毒 2021-01-15 21:50

I have a lot of files in folder with filenames like

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

I need to change creati

3条回答
  •  半阙折子戏
    2021-01-15 22:20

    To bystanders reading this: Please note that this question and answer are for Mac OS. In Linux there is no file creation time in most cases.


    The touch command can change the creation date of your files. From man touch

    -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time

    So we only have to convert your format YYYYMMDD_hhmmss into touch's format. This can be done with bash's parameter substitution. We exttract the substrings YYYYMMDD (${x:0:8}), hhmm (${x:9:4}), and ss (${x:13:2}) and put them together.

    for i in *.mp4; do
        touch -t "${x:0:8}${x:9:4}.${x:13:2}" "$i"
    done
    

    More options for touch that might be of interest to you:

    -c Do not create the file if it does not exist.
    -a Change [only] the access time of the file.
    -m Change [only] the modification time of the file.

    However, this answer claimed that this would only change the creation date if the specified date was before the current creation date. If that's the case and you want to change the time to something after the original creation date use ...

    for i in *.mp4; do
       SetFile -d "${x:4:2}/${x:6:2}/${x:0:4} ${x:9:2}:${x:11:2}:${x:13:2}" "$i"
    done
    

    SetFile may have to be installed first.

提交回复
热议问题