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
nanojust 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
makedecide what to rebuild by comparing timestamps;touchlets you trigger that logic. - Scripting: Check scripts use
touchto create lock files and marker files.
touchis part of the base system on Linux and macOS. There is nothing to install.
1. Create an Empty File
touch notes.txt
- Creates
notes.txtin 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,
touchonly 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;
touchdoes not create directories. If it does not, usemkdir -pfirst orinstall -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
-tuses the compact[[CC]YY]MMDDhhmmformat.
6. Use Another File’s Timestamp
touch -r reference.txt notes.txt
-r(reference) copies the timestamps fromreference.txtontonotes.txt.
7. Create a File Without Changing Its Timestamp
touch -a notes.txt
-aupdates only the access time.
touch -m notes.txt
-mupdates only the modification time (the default behaviour when the file exists).
Common Pitfalls
touchdoes not create directories.touch dir/file.txtfails with “No such file or directory” ifdiris missing. Create it first withmkdir -p dir.- Existing files are not emptied. If the file exists,
touchnever truncates it — it only updates timestamps. To empty a file, use: > fileortruncate -s 0 file. - Timestamps are the only effect. Beginners sometimes expect
touchto “open” or “touch” the file’s contents; it does not. - Overwriting timestamps breaks build tools. Changing a file’s mtime can cause
makeor package managers to rebuild unnecessarily.
Related Commands
| Command | Description |
|---|---|
mkdir | Create directories |
ls | List contents and check timestamps |
stat | Show detailed timestamp information |
truncate | Resize a file, e.g. empty it |
install | Copy files while setting attributes and directories |
touch Cheatsheet
| Command | Description |
|---|---|
touch file.txt | Create the file, or update its timestamps |
touch a.txt b.txt c.txt | Create or update several files |
touch -d "2026-12-04" file | Set a human-readable timestamp |
touch -t 202612040900 file | Set a timestamp in compact format |
touch -r ref.txt file | Copy timestamps from another file |
touch -a file | Update only the access time |
touch -m file | Update only the modification time |
touch -c file | Do not create the file if missing |