LINUX

LINUX TOUCH指令

介紹

touch指令用來變更檔案(夾)的時間戳。

語法

touch [options] files
Code language: plaintext (plaintext)

常用的options

-a Change the access time only. -m Change the modification time only. -c If the file doesn't exist, don't create it (normally, touch creates it). -d timestamp Set the file's timestamp(s). A tremendous number of timestamp formats are acceptable, from "12/31/2020 2pm" to "31-May"(the current year is assumed, and a time of midnight) to "next tuesday 14:00" to "0"(midnight today). -t timestamp A more explicit way to set the file's timestamp, using the format [[CC]YY]MMDDhhmm[.ss], where CC is the two-digit century, YY is the two-digit year, MM is the two-digit month, DD is the two-digit day, hh is the two-digit hour, mm is the two-digit minute, and ss is the two-digit second. For example, -t 202012131400.00 represents December 13, 2020, at 14:00:00.
Code language: plaintext (plaintext)

時間戳(Timestamps)

Linux檔案系統內有三種時間戳,Access time、Modify time及Change time。使用stat指令可以查看檔案(夾)的時間戳。

touch指令預設會變更其中的Access time及Modify time到目前的時間,使用-a-m可以指定要變更的時間戳,使用-d-t可以變更到指定的時間。

範例

你可以變更檔案的時間戳到目前的時間,若myfile不存在則會建立空檔案:

⤍ touch myfile
Code language: plaintext (plaintext)

你可以變更檔案的時間戳到指定的時間:

⤍ touch -d "December 13 2020" myfile ⤍ touch -t 202012131400 myfile ⤍ touch -t 202012131400.00 myfile
Code language: plaintext (plaintext)

若不希望建立任何空檔案,可以使用-c

⤍ touch -c myfile
Code language: plaintext (plaintext)

發佈留言