如何於mac修改檔案的建立、修改時間?

陳孜穎
3 min readSep 21, 2019

--

原本是要回答askubuntu上的問題,但發現那邊是ubuntu不是mac,就把回答移到這裡了。先給英文答案,後面會有比較完整的中文說明。

The following result is tested on macOS Mojave(10.14.6). If the existing create time is earlier than the new time you want to set, than `touch -t` will not work. `SetFile` should be used to make the create time later. What’s more, if the new time will be set is different than the current modify time than `touch -t` will set the access time to the current time. To successfully change the access time, `touch -t` have to be executed twice. Here, a list of commands which must be executed in order, is the example for modify all of create time, modify time, and access time.

macOS上,每個檔案至少有四種記錄時間,分別是建立時間(create time)、修改時間(modify time)、存取時間(access time)和改變時間(change time)。透過 stat -x可以取得修改時間、存取時間和改變時間,而 GetFileInfo則可以取得建立時間和修改時間。我寫了一個簡單的指令檔可以一次執行兩個指令。

除了改變時間以外,其餘三個時間紀錄可以由以下兩個指令組合完成。 touch -t YYYYMMDDhhmm.SS filenameSetFile -d "MM/DD/YYYY hh:mm:ss" filename前者用來修改修改時間和存取時間,後者用來修改建立時間,兩者最大的差異在於後者不會改到存取時間,前者會。以下是各情境的組合。

  1. 只修改建立時間: SetFile -d
  2. 同時把修改時間和建立時間提早:touch -t(或是用第3條組合也可以)
  3. 延後建立時間並調整修改時間:SetFile -dtouch -t
  4. 修改三個:在修改時間與要設定的時間不同的情況下, touch -t只會改到修改時間,存取時間會被修改成指令執行時的時間,所以要多執行一次touch -t讓存取時間可以順利被調整。

指令說明文件

touch: https://ss64.com/osx/touch.html

SetFile: https://ss64.com/osx/setfile.html

--

--

陳孜穎
陳孜穎

Written by 陳孜穎

我是位軟體工程師。長期關注勞交通和性別平等等議題。期望社會中的不平等可以越來越少,達到「人生而平等」這崇高的終極目標。

No responses yet