Touch Command
Update File Timestamps
The touch command is a fundamental utility in Unix-like
operating systems used to change file timestamps. It can be used to
create new empty files or to update the access and modification times
of existing files. This tool is invaluable for developers and system
administrators who need precise control over file metadata.
Using Touch to Set Specific Timestamps
You can specify a particular date and time to set for a file's
modification timestamp. The -d option allows for flexible
time string parsing, making it easy to set timestamps relative to the
current time or to absolute dates.
# To change a file's modification time:
touch -d <time> <file>
touch -d 12am <file>
touch -d "yesterday 6am" <file>
touch -d "2 days ago 10:00" <file>
touch -d "tomorrow 04:00" <file>
Copying Timestamps with Reference Files
A powerful feature of the touch command is its ability to
copy the timestamp from one file to another. This is achieved using
the -r option, which is particularly useful for
synchronizing timestamps or for testing scenarios where exact time
replication is needed.
# To put the timestamp of a file on another:
touch -r <refrence-file> <target-file>
Understanding File Timestamps
File timestamps typically include access time (atime), modification
time (mtime), and change time (ctime). The touch command
primarily manipulates mtime and atime. Understanding these timestamps
is crucial for various tasks, including file synchronization, backup
strategies, and debugging.
Further Resources
- Linux touch man page
- MDN Web Docs: Date Object (for understanding time formats)