touch
Create empty files or update file timestamps
By CMD Script Team · 4 min read · Last updated
touch [OPTIONS] FILE...Options
| Flag | Description |
|---|---|
-t TIMESTAMP | Set access and modification times to a specific value, format [[CC]YY]MMDDhhmm[.ss] |
-d STRING | Set the timestamp using a flexible, human-readable date string |
-a | Change only the access time, leaving modification time untouched |
-m | Change only the modification time, leaving access time untouched |
-c | Do not create the file if it doesn't already exist |
-r FILE | Use another file's timestamps instead of the current time |
Distribution compatibility
- Ubuntu
- Debian
- Fedora
- Arch
- macOS
What it does
touch updates a file's access and modification timestamps to the current time. If the
named file doesn't already exist, it creates a new, empty (zero-byte) file at that path
instead. It never modifies existing file content — its only job is timestamps (and, as
a side effect, creation of missing files).
Beginner examples
touch newfile.txt— create a new, empty filetouch existing_file.txt— update an existing file's timestamps to right now, without changing its contenttouch file1.txt file2.txt file3.txt— create or update several files in one commandls -l --time-style=full-iso file.txt— inspect a file's current modification timestamp before and aftertouch
touch newfile.txt
Advanced examples
- Set a precise, specific timestamp instead of "now":
touch -d "2026-01-15 10:30:00" file.txtusing a flexible human-readable date string. - Use the more rigid but scriptable numeric format:
touch -t 202601151030 file.txt([[CC]YY]MMDDhhmm[.ss]). - Copy another file's exact timestamp for consistency (e.g. in testing or when
restoring metadata):
touch -r reference.txt target.txt. - Update only the access time or only the modification time independently:
touch -a file.txt(access time only) versustouch -m file.txt(modification time only).
touch -d "2026-01-15 10:30:00" -m file.txt
Common mistakes
- Assuming
touchon an existing file changes its content — it never does; it only updates timestamps, leaving the file's bytes completely untouched. - Forgetting that
touchcreates a new empty file if the target doesn't exist, which can be surprising in scripts that expected an error instead (use-cto suppress creation and fail silently on missing files instead). - Misformatting the
-ttimestamp argument — it expects a specific numeric format ([[CC]YY]MMDDhhmm[.ss]) with no separators, unlike the much more forgiving-doption which accepts natural-language-ish date strings. - Using
touchexpecting it to "reset" or clear a file's contents — for that you want> file.txt(truncate) or: > file.txt, nottouch.
Tips
- Use
touch -dwhen you need a human-readable, flexible date string; usetouch -twhen you need the compact numeric format for scripting consistency. touch -r reference_file target_fileis a quick way to copy just the timestamp metadata from one file to another without touching content.- Combine
touchwith build systems (likemake) deliberately when you want to force a target to be considered "changed" and trigger a rebuild, since many build tools key off modification time.
Best practices
- Use
touch -cin scripts where you specifically don't want to accidentally create a file that doesn't already exist — for example, when you only intend to refresh timestamps on files you expect to already be present. - Prefer
-dwith an explicit, unambiguous date/time string in scripts over relying on locale-dependent date parsing quirks. - Don't rely on
touchfor anything beyond timestamps and empty-file creation — for truncating or clearing file content, use explicit redirection (> file) so the intent is unambiguous to anyone reading the script.
Try it yourself
A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.
Real-world use cases
- Creating placeholder files quickly, such as
.gitkeepfiles to make Git track an otherwise-empty directory. - Forcing a
makeor other build tool to reprocess a file by updating its modification time without changing its content:touch source.c && make. - Setting a consistent timestamp on generated files during testing, so test output is reproducible regardless of when the test actually ran.
Common interview questions
- What does
touchdo if the target file already exists? It only updates the access and modification timestamps to the current time (or a specified time); it never alters the file's existing content. - What happens if you
toucha file that doesn't exist? By default,touchcreates a new, empty (zero-byte) file at that path; passing-csuppresses this and causestouchto silently skip files that don't exist instead. - Why might a developer use
touchon a source file during a build? To force a build tool likemake, which decides what to rebuild based on modification timestamps, to consider that file changed and trigger a rebuild of anything that depends on it.
Frequently Asked Questions
What does touch do if the file already exists?
It doesn't modify the file's content at all — it just updates the access and modification timestamps to the current time (or to whatever time you specify with -t or -d). No data is changed.
What does touch do if the file doesn't exist?
By default it creates a new, empty (zero-byte) file at that path with the current timestamp. Pass -c if you want touch to skip creating the file when it's missing instead.
How do I set a specific date on a file instead of the current time?
Use touch -d "2026-01-15 10:30:00" file.txt for a flexible, human-readable date string, or touch -t 202601151030 file.txt for the more rigid [[CC]YY]MMDDhhmm[.ss] format.
Why would someone use touch on an already-existing file?
Common reasons: forcing a build tool to consider a file 'changed' (updating its mtime) so it gets rebuilt or reprocessed, or copying another file's timestamp with -r for testing or consistency purposes.
Cheat sheet
Download a quick-reference cheat sheet for touch.