← Full reference

touch

Create an empty file or change a file's access and modification times

touch does two different things. If the file doesn't exist, it creates an empty one. If it does, it sets its access time (atime) and modification time (mtime) to now or to a given time, leaving the content alone. The sandbox has GNU coreutils 9.4.

Syntax

touch [options] file…

A file's three timestamps

TimeMeaningHow to view itCan touch set it?
atime (access)last readstat -c %x, ls -luyes, -a
mtime (modify)last content changestat -c %y, ls -lyes, -m
ctime (change)last metadata change: permissions, owner, name, any timestampstat -c %z, ls -lcno — the kernel sets the current time on any touch

Options

What to change

OptionWhat it does
(no options)atime and mtime = now; no file — create an empty one
-achange only atime
-mchange only mtime
--time=atime / --time=mtimesame as -a / -m
-cdon't create the file if it doesn't exist (silently, exit code 0)
-hchange the time of the symbolic link itself, not its target

Which time to set

OptionFormat
-d 'string'free-form date, as in date -d: '2024-03-15 14:30', '2024-03-15T14:30:00Z', 'yesterday', '2 hours ago', '-1 day', 'next monday', '@1700000000' (Unix seconds)
-t [[CC]YY]MMDDhhmm[.ss]strict format: -t 202403151430.59 → 2024-03-15 14:30:59
-r filetake the time from another file
-r file -d '+1 hour'another file's time plus an offset

A time without a zone is interpreted in the current zone (TZ). In the sandbox that's UTC.

Examples

# create empty files
touch app.log error.log

# "refresh" a file so make or find consider it new
touch src/main.c

# set a date in the past
touch -d '2024-01-01 00:00' old.txt
touch -d '30 days ago' stale.log

# the same with the strict format
touch -t 202401010000 old.txt

# copy the time from one file to another
touch -r original.conf restored.conf

# change only the access time
touch -a -d 'yesterday' data.csv

# only update existing files, creating nothing
touch -c *.lock

# a marker for find: find everything changed after it
touch /tmp/mark
find /etc -newer /tmp/mark

Pitfalls

  • ctime can't be faked. After touch -d '2020-01-01', mtime and atime become 2020, but ctime becomes the current moment. ctime shows that the file was "touched".
  • By default both times change — atime and mtime. Only one — via -a or -m.
  • Symbolic links. Without -h, touch link changes the target's time, and if the target doesn't exist it creates it.
  • The destination directory must exist: touch /nodir/x — "No such file or directory". touch doesn't do mkdir -p.
  • -t is not -d. -t '2024-01-01' gives "invalid date format": -t understands only the digits YYYYMMDDhhmm.
  • touch doesn't empty a file. To truncate an existing file you need : > file or truncate -s 0.

See also

stat · ls · find · date · truncate · make