On Unix/Linux, which command is used to create files and/or update only the access and modification timestamps without changing content?

Difficulty: Easy

Correct Answer: touch

Explanation:


Introduction / Context:
File timestamps (atime for last access and mtime for last modification) are frequently used by backup systems, build tools, and compliance workflows. Sometimes you need to update these times without editing the file's data, or create an empty file if it does not already exist. Unix provides a simple command for both tasks.


Given Data / Assumptions:

  • You need to refresh timestamps for scheduling or build triggers.
  • The file may or may not exist beforehand.
  • No change to the file's content is required.


Concept / Approach:
The touch command updates atime and mtime on existing files and creates zero-length files if they do not exist. Options like -a, -m, -t, and -r provide fine-grained control over which times are changed and which reference is used. This is preferred over modifying content merely to bump timestamps, which can corrupt data or interfere with checksums.


Step-by-Step Solution:

Update both times to now: touch report.logOnly access time: touch -a report.logOnly modification time: touch -m report.logSet an explicit time: touch -t 202501011230 file.txt (format YYYYMMDDhhmm[.ss])


Verification / Alternative check:
Run stat file.txt (or ls -l --time-style=full-iso) before and after touch to confirm atime/mtime changes without altering file size or checksum.


Why Other Options Are Wrong:

  • wc: Counts lines/words/bytes; does not change timestamps.
  • grep: Searches text; does not adjust file metadata.
  • cat: Concatenates or displays contents; writing through cat alters data, not just timestamps.
  • None of the above: Incorrect because touch performs the timestamp update.


Common Pitfalls:
Misunderstanding filesystem mount options like noatime/relatime that affect access time behavior, or using incorrect -t format leading to failed updates.


Final Answer:
touch

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion