Linux commands
Browse topics

Creating files and setting timestamps with touch

Version
unversioned
Review
Draft

Summary#

touch updates a file's access and modification times, and creates an empty file when the name does not exist.

Syntax#

sh
touch notes.txt
touch -c maybe-missing.txt
touch -d '2026-01-15 09:30:00 UTC' notes.txt
touch -r reference.txt notes.txt
touch -m notes.txt

-c does not create missing files. -d sets a given time, -r copies the times of another file and -m changes only the modification time.

Example#

sh
(
  set -eu
  dir=$(mktemp -d)
  trap 'rm -rf -- "$dir"' EXIT
  cd "$dir"

  touch -d '2026-01-15 09:30:00 UTC' reference
  touch -c missing
  touch -r reference copy

  TZ=UTC date -r copy '+%F %T'
  wc -c < copy | tr -d ' '
  test ! -e missing && echo 'missing was not created'
)

Expected output#

Text
2026-01-15 09:30:00
0
missing was not created

Common mistakes#

touch does not empty an existing file; it changes only timestamps. Build tools that compare modification times can rebuild or skip work after you change them. -d interprets a time without a zone in the local time zone. Changing the times also updates the file's status change time, which touch cannot set.

Version applicability#

This entry describes touch from GNU Coreutils. BSD and macOS accept a narrower set of date strings.

Sources#

Attribution
DevLookup contributors
License
CC-BY-4.0