Skip to main content

touch — Create Files & Update Timestamps Linux coreutils

touch is the fastest way to create an empty file or refresh a file’s timestamps (access time and modification time).
On a VPS, it’s commonly used to create log placeholders, trigger build/deploy systems that watch timestamps, and prevent scripts from failing with “file not found”.

The “why” in one line

Use touch when you need a file to exist, or when you need a timestamp change without changing file content.

touch does not erase files

Running touch existing-file does not delete or clear content. It only updates timestamps (unless you use special flags to target access/modify time).

What You’ll Learn​

  • Create new empty files (single or many)
  • Update access time and/or modification time
  • Set custom timestamps (-t, -d) and copy timestamps (-r)
  • Use touch in WordPress/VPS workflows (logs, placeholders, scripts)
  • Avoid common mistakes (wrong date format, missing directories)

Core Syntax​

touch [OPTION]... FILE...
  • You can pass multiple files in one command.
  • With wildcards, you can update many files at once.

Options That Matter​

OptionMeaningTypical Use
----
-cDo not create file if missingUpdate timestamps only (no new files)
-aChange access time onlySimulate “read” activity
-mChange modification time onlyTrigger rebuild/cache refresh
-t [[CC]YY]MMDDhhmm[.ss]Set a specific timestampTesting, compliance, reproducibility
-d DATEUse a human-readable date stringEasier than -t formatting
-r FILECopy timestamp from reference fileAlign times between related files
--helpShow helpOffline reference
--versionShow versionCompatibility checks
Choose your timestamp method
  • Prefer -d for readability ("2025-09-30 14:00:00")
  • Use -t when you want a strict numeric format (script-friendly)

Common Patterns​

touch test.txt
touch file1.txt file2.txt file3.txt

Best Practices​

  • Use -c when you want to avoid accidentally creating new files.

  • Verify changes with ls -l or stat after touching files.

  • Don’t confuse touch with mkdir (touch is for files, not directories).

  • If a path includes directories that might not exist, create them first:

    mkdir -p /var/www/html/wp-content/uploads
Wildcards can touch more than you expect

Preview first:

ls *.php

Then touch:

touch *.php

Troubleshooting​

ProblemLikely CauseFix
-
File not createdParent directory doesn’t existCreate directory first (mkdir -p ...)
Permission deniedNo rights in target pathUse sudo or fix permissions
Wrong timestampIncorrect -t formatUse -d "YYYY-MM-DD HH:MM:SS"
“Did touch overwrite my file?”Misunderstandingtouch does not erase content; only updates timestamps
Verification commands (HTML)
ls -l file.txt
stat file.txt

Cheat Sheet​

touch file.txt # Create file (or update timestamps)
touch file1 file2 file3 # Create/update multiple
touch -a file.txt # Update access time only
touch -m file.txt # Update modification time only
touch -c file.txt # Update time, don’t create if missing
touch -t 202510021200 file.txt # Set specific timestamp (YYYYMMDDhhmm)
touch -d "2025-10-02 12:00:00" f # Set timestamp using a date string
touch -r ref.txt target.txt # Copy timestamp from another file

Mini Quiz​

  1. What happens if you run touch file.txt on an existing file?
  2. Which option prevents new file creation?
  3. How do you set a custom timestamp with touch?
  4. How would you copy the timestamp from index.php to style.css?
Answers (peek after you try)
  1. Updates timestamps (doesn’t change content). 2) -c. 3) -t ... or -d "...". 4) touch -r index.php style.css.

Worked Examples (with expected output)

Create a new empty file​

touch test.txt

Expected output: (silent success)

Create multiple files​

touch file1.txt file2.txt file3.txt

Expected output: (silent success)

Update timestamp of an existing file​

touch index.php

Expected output: (silent success)

Update access time only (-a)​

touch -a debug.log

Expected output: (silent success)

Update modification time only (-m)​

touch -m style.css

Expected output: (silent success)

Do not create if missing (-c)​

touch -c not_exist.txt

Expected output: (silent success; no file created)

Set a specific timestamp (-t)​

touch -t 202510011230 wp-config.php

Expected output: (silent success)

Set timestamp with human-readable date (-d)​

touch -d "2025-09-30 14:00:00" error.log

Expected output: (silent success)

Copy timestamp from another file (-r)​

touch -r index.php style.css

Expected output: (silent success)

Verify file creation with ls -l​

ls -l test.txt

Expected output (example):

-rw-r--r-- 1 user user 0 Oct 2 09:45 test.txt

Create a hidden file​

touch .hiddenfile

Expected output: (silent success)

WordPress maintenance mode

A .maintenance file in WP root is sometimes used during updates (WordPress itself manages this, but touch is useful for testing).

Update multiple files at once (wildcard)​

touch *.php

Expected output: (silent success)

Ensure a daily log exists (cron-friendly)​

touch /var/log/wp-daily.log

Expected output: (silent success)

Update both access + modification time explicitly​

touch -am functions.php

Expected output: (silent success)

Create a file with today’s date in the name​

touch backup-$(date +%Y-%m-%d).sql

Expected output (example filename):

backup-2025-10-02.sql

Touch inside /tmp/​

touch /tmp/testfile

Expected output: (silent success)

Touch as sudo (root-owned path)​

sudo touch /var/log/secure.log

Expected output: (silent success)

Change timestamp backward (simulate history)​

touch -d "2020-01-01 00:00:00" oldfile.txt

Expected output: (silent success)

Create multiple backups with a loop​

for i in {1..3}; do touch backup$i.sql; done

Expected output: (silent success)

Verify:

ls backup*.sql

Example output:

backup1.sql
backup2.sql
backup3.sql

Fix “file not found” in scripts (ensure file exists)​

touch /var/www/html/wp-content/uploads/missing.log

Expected output: (silent success)