Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

touch Command Guide for Creating Files and Updating Timestamps

This guide helps you understand and use the touch command, which creates empty files or updates the timestamps of existing ones.


What is touch, and Why Use It?

The touch command serves two purposes: it creates a new empty file when the target does not exist, and it updates the access and modification timestamps of a file that does exist. Despite its name, touch never edits the contents of a file.

Why Learn touch?

  • Create files without an editor: One command is faster than opening nano just to save an empty file.
  • Set up placeholders: Build skeleton projects (touch index.html style.css) in a single line.
  • Update timestamps deliberately: Tools like make decide what to rebuild by comparing timestamps; touch lets you trigger that logic.
  • Scripting: Check scripts use touch to create lock files and marker files.

touch is part of the base system on Linux and macOS. There is nothing to install.


1. Create an Empty File

touch notes.txt
  • Creates notes.txt in the current directory if it does not already exist.

Example:

touch empty.txt
ls
empty.txt

2. Create Multiple Files at Once

touch index.html style.css script.js
  • Accepts any number of paths; each missing file is created.

3. Update Timestamps Without Creating Anything

touch existing.txt
  • When the file exists, touch only updates its modification time to “now”.
ls -l existing.txt
  • The timestamp column now shows the current time.

4. Create a File in a Specific Directory

touch ~/Downloads/backup.tar.gz
  • The parent directory must already exist; touch does not create directories. If it does not, use mkdir -p first or install -D.

5. Set a Specific Timestamp

touch -d "2026-12-04 09:00" notes.txt
  • -d (date) sets both timestamps to a human-readable date string.
touch -t 202612040900 notes.txt
  • -t uses the compact [[CC]YY]MMDDhhmm format.

6. Use Another File’s Timestamp

touch -r reference.txt notes.txt
  • -r (reference) copies the timestamps from reference.txt onto notes.txt.

7. Create a File Without Changing Its Timestamp

touch -a notes.txt
  • -a updates only the access time.
touch -m notes.txt
  • -m updates only the modification time (the default behaviour when the file exists).

Common Pitfalls

  • touch does not create directories. touch dir/file.txt fails with “No such file or directory” if dir is missing. Create it first with mkdir -p dir.
  • Existing files are not emptied. If the file exists, touch never truncates it — it only updates timestamps. To empty a file, use : > file or truncate -s 0 file.
  • Timestamps are the only effect. Beginners sometimes expect touch to “open” or “touch” the file’s contents; it does not.
  • Overwriting timestamps breaks build tools. Changing a file’s mtime can cause make or package managers to rebuild unnecessarily.

CommandDescription
mkdirCreate directories
lsList contents and check timestamps
statShow detailed timestamp information
truncateResize a file, e.g. empty it
installCopy files while setting attributes and directories

touch Cheatsheet

CommandDescription
touch file.txtCreate the file, or update its timestamps
touch a.txt b.txt c.txtCreate or update several files
touch -d "2026-12-04" fileSet a human-readable timestamp
touch -t 202612040900 fileSet a timestamp in compact format
touch -r ref.txt fileCopy timestamps from another file
touch -a fileUpdate only the access time
touch -m fileUpdate only the modification time
touch -c fileDo not create the file if missing