Skip to content

The Linux Command Tutorial series provides rigorous, upstream-verified references for essential system commands across Linux distributions and UNIX-like environments. Each article focuses on a single executable, combining exhaustive option documentation, verified real-world examples, security boundaries, and best practices directly derived from official source documentation and POSIX standards.


1. Introduction

Upstream: GNU Coreutils 9.11 | POSIX: POSIX.1-2024 (with GNU extensions) | Safety Tier: unprivileged-filesystem-write | Scope: Zero-byte file creation & inode timestamp updates (atime/mtime)

touch modifies file timestamps (access time atime and modification time mtime) or creates empty files when specified targets do not exist. It invokes the utimensat(2) system call, enabling timestamp updates with nanosecond precision.

  • Upstream Project & Provenance: Distributed in GNU Coreutils (coreutils).
  • Portability & Standards Baseline: Standardized in IEEE Std 1003.1-2024 (POSIX.1-2024).
  • Target Research Implementation: Audited against GNU Coreutils 9.11 (touch(1)).
  • Applicability & Lifecycle: The standard utility for updating build timestamps, forcing Make rebuilds, and provisioning zero-byte sentinel files.

2. Syntax and Command Model

2.1 Canonical Synopsis

bash
touch [OPTION]... FILE...

2.2 Execution Model & Inode Timestamps

  • In UNIX filesystems, an inode stores three primary timestamps:
    1. atime: Last access (read) time.
    2. mtime: Last modification (data content change) time.
    3. ctime: Last inode metadata status change time.
  • touch can explicitly set atime and mtime.
  • ctime is updated automatically by the kernel to the current system time whenever atime or mtime is modified; ctime cannot be set arbitrarily by users.

3. Options

3.1 Primary Operational Flags

Short FlagLong FlagDescriptionPOSIX Defined
-aN/AChange only the access time (atime).Yes
-mN/AChange only the modification time (mtime).Yes
-c--no-createDo not create any files that do not already exist.Yes
-d--date=STRINGParse date string and use it instead of current time.No
-tN/AUse [[CC]YY]MMDDhhmm[.ss] instead of current time.Yes
-r--reference=FILEUse this reference file's timestamps instead of current time.Yes
-h--no-dereferenceAffect each symbolic link itself rather than the target.Yes

4. Basic Usage

4.1 Quick Reference & Common Invocations

Task / ScenarioCommandKey Flags / Behavior
Create empty file / update to nowtouch file.txtCreates file if missing or updates atime & mtime
Update timestamp without creatingtouch -c file.txt-c prevents creating file if it does not exist
Update modification time onlytouch -m file.txt-m updates mtime only, leaving atime intact
Update access time onlytouch -a file.txt-a updates atime only
Set explicit date/time stringtouch -d "2026-01-01 12:00:00" file.txt-d parses human date strings
Sync timestamps from referencetouch -r source.bin target.bin-r copies timestamps from reference file
Update symlink itselftouch -h -m symlink-h affects symlink instead of dereferencing

4.2 Creating an Empty File

bash
touch newfile.txt

4.3 Updating Timestamp to Current Time Without Modification

bash
touch existing_file.txt

5. Practical Operations

5.1 Updating Only Existing Files (Preventing Unintended Creations)

In deployment scripts that touch lockfiles or cache markers:

bash
touch -c /var/run/app.lock
  • If /var/run/app.lock exists, its timestamps update to now. If missing, touch silently does nothing and avoids creating an unwanted file.

5.2 Synchronizing Timestamps with a Reference File

Setting a file's timestamps to match an authoritative release binary:

bash
touch -r /opt/app/bin/server /opt/app/etc/server.conf
  • Both atime and mtime of server.conf now match server.

5.3 Setting an Explicit Timestamp for Testing

Simulating an old log file for logrotate testing:

bash
touch -d "2026-01-01 12:00:00" old_audit.log

Verifying with stat:

bash
stat -c "%y" old_audit.log

Sample terminal output:

text
2026-01-01 12:00:00.000000000 +0000

6. Advanced Usage

By default, touch dereferences symlinks and updates the target file. To update the timestamp of the symlink itself:

bash
touch -h -m current_symlink
  • Modifies the symlink's own modification timestamp without touching the target.

7. Exit Status, Environment, and Configuration

7.1 Exit Status Codes

Exit CodeMeaning
0Success: all specified file timestamps modified/created.
>0An error occurred (permission denied, invalid date string, read-only filesystem).

8. Safety, Security, and Portability

8.1 Permission Requirements

IMPORTANT

Privilege Boundary for Arbitrary Timestamps: Setting file timestamps to the current time requires write permission on the target file.

However, setting timestamps to an arbitrary past or future time (via -d or -t) requires that the calling process either owns the file or holds root privileges (CAP_FOWNER).

8.2 Inode Metadata ctime Invariance

NOTE

ctime (status change time) cannot be directly set by users or utilities. The Linux kernel automatically updates ctime to the current system clock whenever atime or mtime is modified.


9. Best Practices

  1. Use -c in Automation to Prevent Spurious File Creation:

    TIP

    Guidance: Add -c when updating timestamps on state files or triggering watchdog touch operations. Authoritative Justification: GNU and POSIX documentation confirm -c guarantees no empty file is created if the target path is absent.

  2. Use -r for Reproducible Artifact Builds:

    TIP

    Guidance: Set build output timestamps to match the source commit using touch -r. Authoritative Justification: Ensures deterministic build outputs across CI/CD pipelines.


References

  1. GNU Coreutils touch Manual: https://www.gnu.org/software/coreutils/manual/html_node/touch-invocation.html
  2. POSIX.1-2024 touch Specification: The Open Group Base Specifications Issue 8. https://pubs.opengroup.org/onlinepubs/9799919799/utilities/touch.html

Last updated:

Released under the MIT License.