>_cmd.script

touch

Create empty files or update file timestamps

Files

By CMD Script Team · 4 min read · Last updated

SYNTAX
touch [OPTIONS] FILE...

Options

Command options and flags
FlagDescription
-t TIMESTAMPSet access and modification times to a specific value, format [[CC]YY]MMDDhhmm[.ss]
-d STRINGSet the timestamp using a flexible, human-readable date string
-aChange only the access time, leaving modification time untouched
-mChange only the modification time, leaving access time untouched
-cDo not create the file if it doesn't already exist
-r FILEUse 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 file
  • touch existing_file.txt — update an existing file's timestamps to right now, without changing its content
  • touch file1.txt file2.txt file3.txt — create or update several files in one command
  • ls -l --time-style=full-iso file.txt — inspect a file's current modification timestamp before and after touch
touch newfile.txt

Advanced examples

  • Set a precise, specific timestamp instead of "now": touch -d "2026-01-15 10:30:00" file.txt using 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) versus touch -m file.txt (modification time only).
touch -d "2026-01-15 10:30:00" -m file.txt

Common mistakes

  • Assuming touch on an existing file changes its content — it never does; it only updates timestamps, leaving the file's bytes completely untouched.
  • Forgetting that touch creates a new empty file if the target doesn't exist, which can be surprising in scripts that expected an error instead (use -c to suppress creation and fail silently on missing files instead).
  • Misformatting the -t timestamp argument — it expects a specific numeric format ([[CC]YY]MMDDhhmm[.ss]) with no separators, unlike the much more forgiving -d option which accepts natural-language-ish date strings.
  • Using touch expecting it to "reset" or clear a file's contents — for that you want > file.txt (truncate) or : > file.txt, not touch.

Tips

  • Use touch -d when you need a human-readable, flexible date string; use touch -t when you need the compact numeric format for scripting consistency.
  • touch -r reference_file target_file is a quick way to copy just the timestamp metadata from one file to another without touching content.
  • Combine touch with build systems (like make) 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 -c in 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 -d with an explicit, unambiguous date/time string in scripts over relying on locale-dependent date parsing quirks.
  • Don't rely on touch for 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 .gitkeep files to make Git track an otherwise-empty directory.
  • Forcing a make or 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 touch do 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 touch a file that doesn't exist? By default, touch creates a new, empty (zero-byte) file at that path; passing -c suppresses this and causes touch to silently skip files that don't exist instead.
  • Why might a developer use touch on a source file during a build? To force a build tool like make, 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.