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 1 year has passed since last update.

dataコマンドでファイルのタイムスタンプを取得する Mac&CentOS

Posted at

ファイルのタイムスタンプを変数に取得して、画面上に表示して、処理がすべて実行されたかを目視、将来的にはif文で確認したい

環境はMacOSとLinux(CentOS7)で同じことを実行したい

「dateコマンドでファイルのタイムスタンプ」と検索すると、

$ date '+%Y/%m/%d %H:%M' -r test.sh 
2022/11/11 11:27

と、わかった。

ふむふむ、これを検索したMac上で実行すると、エラーメッセージ

~$ date '+%Y/%m/%d %H:%M' -r test.sh 
date: illegal time format
usage: date [-jnRu] [-r seconds|file] [-v[+|-]val[ymwdHMS]]
            [-I[date | hours | minutes | seconds]]
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]

メッセージをよく読んでからオプションを変更して再実行

~$ date  -r test.sh  "+%Y%m%d"
20221111

Linux(CentOS)に戻ってMacOSで使った形式を実行すると、MacOSと同じ書き方でも実行できました。

[hoge ~]$ date  -r test.sh  "+%Y%m%d"
20221111
[hoge ~]$ date  "+%Y%m%d" -r test.sh  
20221111

ということで、MacOSでもLinuxでも、環境を意識せずに、
指定したファイルのタイムスタンプを取得して、変数に格納するにはこうすればいい、とわかりました

~$ filedate=`date -r test.sh  "+%Y%m%d"`
~$ echo $filedate
20221111
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?