What does the touch command do besides creating empty files?
touch updates a file's access and modification timestamps to now, and creates the file if it does not exist. You can also set a specific time with -t or -d, or copy another file's time with -r.
Explain it simply
Touch is like stamping a library card with today's date. If the card does not exist, a blank one is created. If it does, only the date changes and the contents stay as they were.
The creation of empty files is a side effect, not the main purpose. The real job is changing timestamps, which matters for tools that compare times, such as make, rsync, backup scripts and find -mtime.
Useful options: -d "2 days ago" sets a custom date, -t 202610011530 sets an exact time, -r other.txt copies the time from another file, and -c avoids creating a file that does not exist.
Example
touch notes.txt # create or refresh
touch -d "2 days ago" old.log # pretend it is old
find . -name "*.log" -mtime +1 # now old.log matches
# ./old.logWhat to say in the interview
"touch sets the access and modification times to now, creating the file if missing. I use it to create placeholder files and to adjust timestamps when testing things like find -mtime or build tools that compare dates."
Common mistakes
- Thinking touch changes file contents; it never does.