1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

画像ファイルの作成日時をExifの撮影日時で更新するワンライナー

Posted at

はじめに

iPhoneからMacに画像をjpeg形式で保存すると、ファイルの作成日時がこの保存操作日時となるため、Exifの写真撮影日で上書きする。

環境

  • Mac OX
  • bash

準備

homebrewでexiftoolをインストール。

$ brew install exiftool

CommandLineToolsをインストールしていない場合(setfileコマンド実行時に'xcrun: error: invalid active developer path'などが表示される)、こちらもインストール。

$ xcode-select --install

ワンライナー

$ for file in `ls *.jpeg`; do date_str=`exiftool -DateTimeOriginal $file`; date_str=`date -j -f "%Y:%m:%d %H:%M:%S" "${date_str#*: }" +"%m/%d/%Y %H:%M:%S"`; setfile -m "$date_str" $file; echo -e "$file\t$date_str"; done

-lsコマンドのファイル拡張子は、適宜jpgなどに変更。
-日付のフォーマット変換(dateコマンド)は、使用環境により不要な可能性あり。

 解説

  • exiftool -DateTimeOriginal $file
    • 撮影日時を取得
  • ${data_str#*: }
    • 左端から最初に': 'にヒットする部分までを削除
  • date -j -f "%Y:%m:%d %H:%M:%S" "${date_str#*: }" +"%m/%d/%Y %H:%M:%S"
    • 日付のフォーマットを変換
  • setfile -m "$date_str" $file
    • ファイルの変更日を上書き(変更日が作成日より古い場合、作成日も変更日で上書きされる)
    • 作成日を変更したい場合は-dオプション
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?