Terminal GuideTerminal Guide

touch Command Guide

The touch command creates empty files and updates file timestamps. Learn how to manage file times effectively.

4 min readLast updated: 2024
Dai Aoki

Dai Aoki

CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm

Quick Reference

Basic

touch file.txtCreate/update file
touch f1 f2 f3Multiple files
touch -c fileNo create if missing

Timestamp

-aAccess time only
-mModify time only
-r ref fileCopy from ref file

Set Time

-t 202401151030Set to YYYYMMDDhhmm
-d "2024-01-15"Date string format
-d "yesterday"Relative time

Common

touch .gitkeepKeep empty dir in git
touch src/*.cUpdate timestamps
touch -d @0 fileSet to epoch (1970)

Downloadable Image Preview

Failed to generate preview

Basic Usage

touch creates an empty file or updates the timestamp of an existing file.

bash
# Create empty file
touch newfile.txt

# Create multiple files
touch file1.txt file2.txt file3.txt

# Update timestamp of existing file
touch existing_file.txt

Common Options

touch Options

-aChange only access time
-mChange only modification time
-cDon't create file if it doesn't exist
-t STAMPUse specified time (YYYYMMDDhhmm)
-d DATEParse DATE string
-r FILEUse timestamp from reference FILE

Creating Files

bash
# Create single file
touch newfile.txt

# Create multiple files
touch file{1..10}.txt

# Create files with specific names
touch README.md LICENSE CHANGELOG.md
Tip
If the file already exists, touch updates its timestamp without modifying its contents.

Setting Specific Time

bash
# Set specific timestamp (YYYYMMDDhhmm.ss)
touch -t 202401151030.00 file.txt

# Using date string
touch -d "2024-01-15 10:30:00" file.txt

# Set to yesterday
touch -d "yesterday" file.txt

# Set to last week
touch -d "1 week ago" file.txt

Copying Timestamps

bash
# Copy timestamp from another file
touch -r reference.txt target.txt

Access vs Modification Time

bash
# Change only access time
touch -a file.txt

# Change only modification time
touch -m file.txt

# View times with stat
stat file.txt
Info
Access time (atime) is when the file was last read. Modification time (mtime) is when the content was last changed.

Don't Create New Files

bash
# Only update if file exists
touch -c nonexistent.txt  # Does nothing

touch -c existing.txt     # Updates timestamp

Practical Examples

Create project files

bash
touch .gitignore README.md package.json

Create placeholder files

bash
touch src/{index,app,utils}.js

Force rebuild (make)

bash
# Update source file timestamp to trigger rebuild
touch src/main.c && make

Create marker/flag files

bash
touch /tmp/processing_started
# ... do work ...
touch /tmp/processing_completed

Backdate a file

bash
touch -d "2020-01-01" old_file.txt

Create .keep files for empty directories

bash
mkdir -p logs tmp cache
touch logs/.keep tmp/.keep cache/.keep

Alternatives for Creating Files

bash
# Using output redirection
> newfile.txt

# Using echo
echo "" > newfile.txt

# Using cat
cat /dev/null > newfile.txt

# Create with content
echo "content" > file.txt

Summary

touch is handy for file creation and timestamp management. Key takeaways:

  • Use touch file to create empty files
  • Use touch -t to set specific timestamp
  • Use touch -r to copy timestamp from another file
  • Use touch -c to avoid creating new files
  • Use touch -a/-m for access/modification time only

Related Articles