touch has two purposes: creating empty files and updating file timestamps. Most people use it to quickly create new files.

Create an Empty File

touch newfile.txt

Creates an empty file called newfile.txt. If the file exists, it updates the timestamp instead.

Create Multiple Files

touch file1.txt file2.txt file3.txt

Or with brace expansion:

touch test{1,2,3,4,5}.txt

Creates: test1.txt, test2.txt, test3.txt, test4.txt, test5.txt

Create Files with Extensions

touch index.html style.css app.js
touch README.md .gitignore

The file is empty, but ready for editing.

Update Timestamp

If a file exists, touch updates its "last modified" time to now:

touch existingfile.txt

Check with:

ls -l existingfile.txt

The modification time is now current.

Why Update Timestamps?

Common reasons:

  • Trigger build systems - Make watches for file changes
  • Mark files as "touched" - Audit trails
  • Reset modification time - Various scripting purposes
  • Force backup systems - Some backups check modification times

Touch vs Echo/Cat for Creating Files

Method Result
touch file.txt Empty file
echo "text" > file.txt File with content
cat > file.txt File with typed content

touch is cleanest for empty files.

Create a File with Specific Time (-t)

touch -t 202412151030 file.txt

Format: YYYYMMDDhhmm

Sets timestamp to December 15, 2024, 10:30 AM.

Use Another File's Timestamp (-r)

touch -r reference.txt newfile.txt

newfile.txt gets the same timestamp as reference.txt.

Don't Create New Files (-c)

Only update existing files, don't create new ones:

touch -c maybeexists.txt

No error if it doesn't exist - just does nothing.

Access Time vs Modification Time

Flag Updates
-a Access time only
-m Modification time only
(none) Both
touch -a file.txt  # Update access time
touch -m file.txt  # Update modification time

Practical Examples

Create project files:

mkdir myproject && cd myproject
touch index.html style.css app.js README.md

Create config file:

touch ~/.myapprc

Prepare multiple test files:

touch testfile_{a..z}.txt

Creates testfile_a.txt through testfile_z.txt.

Mark all files as modified now:

touch *

Hidden Files

Create hidden files (starting with dot):

touch .hidden
touch .env
touch .gitignore

Common Errors

"Permission denied":

  • You don't have write permission in this directory
  • Try a different location or check permissions

"No such file or directory":

  • Parent directory doesn't exist
  • Create it first with mkdir -p

touch vs mkdir

Command Creates
touch name Empty file
mkdir name Directory (folder)

Quick Reference

Command Result
touch file.txt Create empty file
touch a.txt b.txt Create multiple files
touch -c file.txt Update only if exists
touch -t 202412151030 file.txt Set specific time
touch -r ref.txt new.txt Copy timestamp

Keep Learning

File creation is a building block. The free course covers essential Terminal commands.

Check it out at Mac Terminal for Humans.