stat: ファイルの修正タイムスタンプ

stat: ファイルの修正タイムスタンプ

私はそれをstat -f %m .bashrcosxで.bashrcの修正時間を取得するために使用します。しかし、Ubuntuで同じコマンドを実行するとエラーが発生します。

stat: cannot read file system information for %m': No such file or directory

これを達成するための互換性のある方法はありますか?

ベストアンサー1

UbuntuはGNU coreutilsを使用し、statOSXはBSDバリアントを使用します。したがって、Ubuntuではコマンドが少し異なります。

stat -c %Y .bashrc

からman stat

   -c  --format=FORMAT
          use the specified FORMAT instead of the default; output  a  new‐
          line after each use of FORMAT

そして:

   %Y     time of last data modification, seconds since Epoch

オペレーティングシステムに関係なく移植可能な方法が必要な場合は、いくつかの方法があります。私は変数を一度に1つずつ適切なパラメータに設定する必要があると思いました。

if uname | grep -q "Darwin"; then
    mod_time_fmt="-f %m"
else
    mod_time_fmt="-c %Y"
fi

次に、statコマンドで必要なときはいつでもその値を使用します。

stat $mod_time_fmt .bashrc

おすすめ記事