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
| Time | Meaning | How to view it | Can touch set it? |
|---|---|---|---|
| atime (access) | last read | stat -c %x, ls -lu | yes, -a |
| mtime (modify) | last content change | stat -c %y, ls -l | yes, -m |
| ctime (change) | last metadata change: permissions, owner, name, any timestamp | stat -c %z, ls -lc | no — the kernel sets the current time on any touch |
Options
What to change
| Option | What it does |
|---|---|
| (no options) | atime and mtime = now; no file — create an empty one |
-a | change only atime |
-m | change only mtime |
--time=atime / --time=mtime | same as -a / -m |
-c | don't create the file if it doesn't exist (silently, exit code 0) |
-h | change the time of the symbolic link itself, not its target |
Which time to set
| Option | Format |
|---|---|
-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 file | take 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
-aor-m. - Symbolic links. Without
-h,touch linkchanges 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".touchdoesn't domkdir -p. -tis not-d.-t '2024-01-01'gives "invalid date format":-tunderstands only the digitsYYYYMMDDhhmm.touchdoesn't empty a file. To truncate an existing file you need: > fileortruncate -s 0.