Home / Alt manpages / touch(1)

  • touch(1)
  • User command
  • linux

Set File Timestamps with touch Without Creating Surprises

touch looks harmless until it quietly creates an empty file where you expected an error, or overwrites a modification time your backup tool relied on. This guide gets you setting timestamps deliberately: a fixed date, one timestamp at a time, a copy from a reference file, and a check with stat so you can see what actually happened. It takes about five minutes. The examples use GNU coreutils 9.4, installed here as /usr/bin/touch.

You need a shell and permission to modify the files you name. None of this needs sudo when you work on files you own; only reach for it when the target is deliberately in a protected location, and check the path first.

1. Make a safe test file

Work in a scratch directory so nothing you do here touches a real file:

mkdir -p "$HOME/touch-demo"
cd "$HOME/touch-demo"
touch example.txt
stat -c 'path=%n size=%s access=%x modify=%y' example.txt

You should see one line: path example.txt, size 0, and access and modify times close to now.

  • This is the trap. A missing file is created empty by default. Handy for placeholders, a menace when a path is misspelt and you meant to check something, not create it.
  • Existing files are untouched in content. If example.txt already existed, touch only changes its timestamps and leaves the contents alone.

2. Stop touch from creating anything

When you're checking or refreshing a path that must already exist, add --no-create so a typo can't silently invent a file:

touch --no-create example.txt
touch --no-create missing.txt
test ! -e missing.txt && echo 'missing.txt was not created'

The second command does nothing, because missing.txt is absent, and --no-create refuses to make it. That's the point: a failed or unchanged touch normally produces no output at all, so the explicit test above is your only real proof.

Tip

Put --no-create in any script that touches paths it did not itself create. It turns a silent file-creation bug into nothing happening, which is much easier to notice and debug.

3. Set both timestamps to a known date

Pass a human-readable date to --date. Unless you also select one with -a or -m, this sets both the access and modification times:

touch --date='2020-01-02 03:04:05' example.txt
stat -c 'access=%x modify=%y' example.txt

Both values should start with 2020-01-02 03:04:05, precision depending on the filesystem. The date is read in your local time zone unless you include a zone such as UTC.

--date also understands relative expressions like "yesterday" or "2 hours ago", which is great at a prompt and a liability in a script. When another process depends on a reproducible result, always give it a complete, fixed date and time.

4. Change only one timestamp

Use -m for modification time or -a for access time, and the other one is left exactly as it was:

touch -m --date='2021-02-03 04:05:06' example.txt
stat -c 'after modify-only: access=%x modify=%y' example.txt

touch -a --date='2022-03-04 05:06:07' example.txt
stat -c 'after access-only: access=%x modify=%y' example.txt

Checkpoint

After the first command, modify shows 2021 while access still shows the earlier value. After the second, access shows 2022 while modify stays at 2021. Filesystems record timestamps at different precisions, so compare the displayed values rather than expecting nanosecond exactness everywhere.

5. Copy a timestamp from another file

When the time you want already lives on another file, --reference saves you retyping it:

touch --date='2023-04-05 06:07:08' reference.txt
touch --reference=reference.txt example.txt
stat -c '%n modify=%y' reference.txt example.txt

The two modification times should match. Only the timestamp is read from the reference file, not its content. By default both access and modification times are copied; add -a or -m if you want just one of them.

6. Use the compact numeric format when a tool demands it

-t takes [[CC]YY]MMDDhhmm[.ss], its own date shorthand. This sets 2 January 2024 at 03:04:05:

touch -t 202401020304.05 numeric.txt
stat -c 'modify=%y' numeric.txt

Warning

--date and -t deliberately accept different formats. Don't paste an ISO-looking string into -t and assume it works, and don't assume a short numeric value means the same thing to both options.

Common traps

  • Creating a file by mistake. The default creates a missing path. Use -c or --no-create when creation isn't part of the job.
  • Thinking it changes content. touch never writes file data, only metadata, but tools watching modification times can still react to that.
  • Assuming you need root. You generally just need write access to the file or its directory. A permission error is not fixed safely by bolting on sudo without checking the path first.
  • Symbolic links. Without -h, touch follows a link and changes the file it points to. -h changes the link itself, but only works on systems that support symlink timestamps.
  • Using - as a filename. GNU touch treats a bare - as the file attached to standard output, not a literal filename. Avoid it unless you mean exactly that.

Recovery

Recovery

There's no universal undo for a timestamp change. If you know the previous value, set it again with --date, or pull it from a trustworthy reference file with --reference. Running touch again just sets the current time on both timestamps unless you pick one.

# Restore a known value; replace the placeholder with the real timestamp.
touch --date='YYYY-MM-DD HH:MM:SS' example.txt
stat -c 'path=%n access=%x modify=%y' example.txt

Never invent a plausible-looking recovery date when timestamps are evidence for an audit, build, backup or sync process. Track down the real value in the relevant log, reference file or backup metadata first.

Done means

  • You know whether the target can be created, and used --no-create when it must not be.
  • You picked the right scope: both timestamps, access only, modification only, a reference file, or a fixed date.
  • You verified with stat and accounted for filesystem precision.
  • You skipped sudo unless the protected target and path were confirmed first.