DEV Community

dpm_bush
dpm_bush

Posted on Originally published at sshflow.com

Linux `touch`: Create Empty Files and Manage Timestamps

touch is often introduced as a way to create an empty file. That’s only half the story: it also updates file timestamps without changing the file’s contents. Knowing which behavior you’re invoking helps avoid surprises in scripts and on remote servers.

The safe mental model

Run touch with one or more paths:

touch report.txt draft.txt
Enter fullscreen mode Exit fullscreen mode

For each path, touch updates its access time (atime) and modification time (mtime) to the current time. If a path doesn’t exist, it creates a zero-byte file—provided you can write to the parent directory.

If the file already exists, its contents are not overwritten or truncated. The command changes metadata, not file data. That makes touch useful for creating placeholders or refreshing timestamps, but not for writing text.

A quick way to see the result is:

ls -l report.txt
Enter fullscreen mode Exit fullscreen mode

This displays the file’s modification time, not its contents. For a closer look at file listings, see this guide to reading ls output.

Update only one timestamp

By default, both atime and mtime are updated. Use -a to update only the access time, or -m to update only the modification time:

touch -a report.txt
touch -m report.txt
Enter fullscreen mode Exit fullscreen mode

Atime is associated with file access, but mount options and filesystem policies can limit when it changes. Mtime records changes to file data, though a command like touch can explicitly set it without editing that data.

You may also encounter ctime, the inode status-change time. It is not a file creation time, and touch does not let you set it directly. Changing metadata can itself update ctime.

Set a particular date and time

On GNU/Linux systems with GNU Coreutils, -d accepts a readable date string. This sets both atime and mtime:

touch -d '2026-10-12 14:30:00' report.txt
Enter fullscreen mode Exit fullscreen mode

Add -a or -m to set only one of those timestamps:

touch -a -d '2026-10-12 14:30:00' report.txt
touch -m -d '2026-10-12 14:30:00' report.txt
Enter fullscreen mode Exit fullscreen mode

The date is interpreted using the system’s date parsing and timezone settings. Verify it on GNU/Linux with:

stat -c 'Access: %x\nModify: %y\nChange: %z' report.txt
Enter fullscreen mode Exit fullscreen mode

Here, %x is atime, %y is mtime, and %z is ctime. The ctime shown reflects the metadata change; it won’t be set to the date supplied to touch.

For a numeric local date and time, GNU/Linux also accepts -t in this form:

touch -t 202610121430 report.txt
Enter fullscreen mode Exit fullscreen mode

The format is [[CC]YY]MMDDhhmm[.ss], using a 24-hour clock. The optional .ss supplies seconds. Options and date formats can differ across Unix-like systems, so check the local touch manual before putting these forms in a cross-platform script.

Reuse another file’s timestamps

Use -r to copy a reference file’s atime and mtime to a target:

touch -r reference.txt report.txt
Enter fullscreen mode Exit fullscreen mode

If report.txt doesn’t exist, it is created and receives the reference timestamps. To copy only the modification time, combine the options:

touch -m -r reference.txt report.txt
Enter fullscreen mode Exit fullscreen mode

Avoid creating a missing file

The default behavior can be inconvenient in a script: a typo or unexpected missing path may become a new empty file. Use -c (or --no-create on GNU/Linux) to update timestamps only if the file already exists:

touch -c report.txt
Enter fullscreen mode Exit fullscreen mode

If the target is missing, this leaves it missing. If it exists, its timestamps are updated as usual.

A note about symbolic links

By default, touch generally follows a symbolic link and updates the timestamps of its target. GNU/Linux Coreutils provides -h to request changing the link’s own timestamps instead:

touch -h link-to-report
Enter fullscreen mode Exit fullscreen mode

This behavior depends on the implementation and filesystem. Don’t assume -h works the same way across operating systems; check the platform’s manual if a script relies on it.

Troubleshoot the common surprises

  • A file appeared unexpectedly: Missing paths are created by default. Use -c when that isn’t wanted.
  • “Permission denied” while creating a file: Check write permission on the parent directory. Avoid broad permission changes to fix a single command.
  • The time looks unchanged: Confirm which timestamp you updated and inspect it with stat. Display precision, timezone settings, filesystem behavior, and atime policies can affect what you see.
  • You meant to add text: touch does not write content. For other ways to create files, see this Linux file-creation guide.

In short: use touch file to create a missing empty file or refresh timestamps on an existing one. Reach for -c when creation would be an unwanted side effect, and verify specific timestamp changes with the tools available on your system.

I originally published a more detailed version of this guide on the SSHFlow blog.

I'm also building SSHFlow — an SSH client where every server gets its own workspace for terminals, SFTP, code, and databases.

Top comments (0)