Master the Linux touch Command: Change Timestamps and Control File Creation
This guide explains how to use the Linux touch command to modify file access and modification timestamps, prevent accidental file creation, set custom dates with various options, and even adjust time zones, providing clear examples and command outputs for each scenario.
Overview
The touch command is a standard Linux utility used to create an empty file or to modify a file's access and modification timestamps. It supports a range of options for fine‑grained control.
Basic usage
Change a file's timestamps.
Create a new empty file when the specified pathname does not exist.
Preventing file creation
To update timestamps without creating a new file when the target does not exist, use the -c (no‑create) option.
touch -c filenameChanging only the access time
Use the -a option (access) to modify only the access time while leaving the modification time unchanged. touch -a file Verify the result with stat file; the Access field changes, Modify remains the same.
Changing only the modification time
Use the -m option (modify) to update only the modification time. touch -m file Check with stat file; the Modify field changes, Access stays unchanged.
Setting a custom timestamp
Two methods allow explicit timestamp assignment:
Using -t with the format [[CC]YY]MMDDhhmm[.SS]. Example sets both timestamps to 2020‑01‑01 20:20:20: touch -t 202001012020.20 file Using -d with a date string. Example sets timestamps to 2008‑08‑08 00:00:00: touch -d '08-August-2008' file The -d option also accepts flexible expressions such as yesterday, 1 year ago, or combined strings like 'yesterday 08-August-2008'.
Changing time zone
When using -d, append a time‑zone identifier to interpret the date in that zone, e.g.:
touch -d '2020-01-01 12:00:00 UTC' fileViewing file details
The stat command displays full file metadata, confirming the effects of each touch operation.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
