Customizing File Timestamps with touch
Setting Specific Timestamps
The touch
command allows you to set custom timestamps for files. This can be useful in various scenarios, such as simulating file modifications or synchronizing timestamps across different systems.
To set a specific timestamp, you can use the -t
option followed by the desired timestamp in the format [[CC]YY]MMDDhhmm[.ss]
. For example:
touch -t 202305011530 file.txt
This will set the timestamp of the file.txt
to May 1, 2023, at 3:30 PM.
Alternatively, you can use the -d
option to set the timestamp using a more human-readable date and time format:
touch -d "2023-05-01 15:30" file.txt
This will set the timestamp of the file.txt
to the same time as the previous example.
Updating Specific Timestamp Types
The touch
command also allows you to update specific timestamp types, such as access time (atime) or modification time (mtime).
To update the access time (atime) of a file, use the -a
option:
touch -a file.txt
To update the modification time (mtime) of a file, use the -m
option:
touch -m file.txt
You can also update both the access and modification times simultaneously by omitting the -a
or -m
options:
touch file.txt
Practical Examples
Here's an example of how you can use the touch
command to set a custom timestamp for a file:
## Create a new file with a custom timestamp
touch -t 202305011530 file.txt
## Update the access time of the file
touch -a file.txt
## Update the modification time of the file
touch -m file.txt
These commands will create a new file named file.txt
with a timestamp of May 1, 2023, at 3:30 PM, and then update the access and modification times separately.