touch Command Guide
The touch command creates empty files and updates file timestamps. Learn how to manage file times effectively.
4 min read•Last updated: 2024
Dai Aoki
CEO at init, Inc. / CTO at US & JP startups / Creator of WebTerm
Quick Reference
Basic
touch file.txtCreate/update filetouch f1 f2 f3Multiple filestouch -c fileNo create if missingTimestamp
-aAccess time only-mModify time only-r ref fileCopy from ref fileSet Time
-t 202401151030Set to YYYYMMDDhhmm-d "2024-01-15"Date string format-d "yesterday"Relative timeCommon
touch .gitkeepKeep empty dir in gittouch src/*.cUpdate timestampstouch -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.txtCommon Options
touch Options
| -a | Change only access time |
| -m | Change only modification time |
| -c | Don't create file if it doesn't exist |
| -t STAMP | Use specified time (YYYYMMDDhhmm) |
| -d DATE | Parse DATE string |
| -r FILE | Use 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.mdTip
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.txtCopying Timestamps
bash
# Copy timestamp from another file
touch -r reference.txt target.txtAccess 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.txtInfo
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 timestampPractical Examples
Create project files
bash
touch .gitignore README.md package.jsonCreate placeholder files
bash
touch src/{index,app,utils}.jsForce rebuild (make)
bash
# Update source file timestamp to trigger rebuild
touch src/main.c && makeCreate marker/flag files
bash
touch /tmp/processing_started
# ... do work ...
touch /tmp/processing_completedBackdate a file
bash
touch -d "2020-01-01" old_file.txtCreate .keep files for empty directories
bash
mkdir -p logs tmp cache
touch logs/.keep tmp/.keep cache/.keepAlternatives 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.txtSummary
touch is handy for file creation and timestamp management. Key takeaways:
- Use
touch fileto create empty files - Use
touch -tto set specific timestamp - Use
touch -rto copy timestamp from another file - Use
touch -cto avoid creating new files - Use
touch -a/-mfor access/modification time only