touch
Overview
touch updates file timestamps and, by default, creates empty files that do not exist. Operators use it to create placeholders, nudge make rebuilds, normalize mtimes in tests, and clone timestamps from a reference file.
Linux files typically track at least atime (access), mtime (content modification), and ctime (metadata change). touch adjusts atime/mtime; ctime updates as a side effect of the metadata change.
Syntax
touch [options] file...Common Options
| Option | Description |
|---|---|
-a |
Change access time only |
-m |
Change modification time only |
-c, --no-create |
Do not create missing files |
-d STRING, --date=STRING |
Parse human-friendly time string |
-t [[CC]YY]MMDDhhmm[.ss] |
Set time in compact form |
-r REF, --reference=REF |
Copy timestamps from REF |
--time=WORD |
access/atime/use or modify/mtime |
-h |
Affect symlink itself (when supported) |
Key Use Cases
- Create empty files / placeholders
- Update mtime to trigger build systems
- Set explicit timestamps for tests and packages
- Copy times from a reference file
- Batch-create names (
touch file{1..5})
Examples with Explanations
Create or refresh
touch newfile
touch file1 file2 file3
touch file{1..5}
touch logs/.gitkeepDo not create
touch -c missing_file # no-op if absent; exit 0 typically
ls missing_fileatime / mtime only
touch -a file # access time
touch -m file # modification time
stat fileDate strings
touch -d '2020-01-01' file
touch -d '2 days ago' file
touch -d '2024-06-15 14:30:00' file
touch -d 'next Monday' fileGNU touch accepts many date-style strings.
Compact -t form
touch -t 202312201200 file
touch -t 202312201200.30 file
# [[CC]YY]MMDDhhmm[.ss]Reference file
touch -r /etc/hosts mycopy
stat -c '%y %n' /etc/hosts mycopyMake and empty targets
touch src/main.c # force rebuild consumers of main.c
makeScripts: ensure path exists as empty file
mkdir -p "$(dirname "$out")"
touch "$out"Clear mtime into the past for cleanup tests
touch -d '30 days ago' /tmp/old.log
find /tmp -name 'old.log' -mtime +7Understanding timestamps
stat file
stat -c 'mtime=%y atime=%x ctime=%z' file| Field | Meaning |
|---|---|
| atime | Last access (often lazy/relatime on modern mounts) |
| mtime | Last content modification |
| ctime | Last inode/metadata change (not “creation”) |
| birth | Creation time on some filesystems (stat %w) |
relatime / noatime mount options mean touch -a may not behave as you expect for real reads; the explicit touch -a still sets atime.
Notes / Pitfalls
- Creating files requires write permission on the parent directory.
touchon a directory updates the directory’s timestamps, not children.- Timestamp precision and timezone depend on filesystem and locale settings.
- NFS root_squash and permission quirks can make
touchfail with EACCES. touchis not a substitute for writing content — use redirection or editors.
2026-relevant notes
- Build caches (Bazel, Nix, container layer caches) care deeply about mtime vs content hashes; know which model your toolchain uses.
touch -ris handy when replaying reproducible package layouts.- For pure “ensure file exists” in bash,
: >>fileorumask+ redirect may be clearer thantouchwhen you also write content.
Additional Resources
man touch- GNU coreutils — touch