Chapter 14: Bash Timestamp (touch)
What Bash Timestamp (touch)
The name “touch” is very literal — it touches the file.
When you “touch” a file, two main things can happen:
- If the file does not exist → it creates an empty file
- If the file already exists → it updates two timestamps to current time:
- mtime = modification time (when file content was last changed)
- atime = access time (when file was last read/opened)
(There is also ctime = inode change time — but touch does not change ctime in normal usage.)
Most people use touch for one of these reasons:
- Quickly create empty placeholder files
- Trick scripts/make/systems that “if file is old → do something”
- Fake “recently modified” files for testing/backup/rsync/etc.
1. Most basic usage (create file or update to now)
|
0 1 2 3 4 5 6 7 |
touch photo.jpg touch notes.txt secret.key report2025.pdf |
- If file didn’t exist → now it exists (size = 0 bytes)
- If file existed → mtime & atime become current time
Check timestamps like this:
|
0 1 2 3 4 5 6 7 8 |
ls -l # shows mtime ls -l --time=atime # shows atime (access time) stat photo.jpg # shows all three times very clearly (best way) |
2. Only update modification time (most common need)
|
0 1 2 3 4 5 6 |
touch -m old-report.pdf |
Only mtime changes, atime stays old.
3. Only update access time
|
0 1 2 3 4 5 6 |
touch -a readme.md |
Rarely used, but sometimes needed for audit/security testing.
4. Most powerful → Set completely custom timestamp (this is what people mean by “touch timestamp”)
There are two nice ways to do this:
Way A: -t → numeric format (very exact, very popular)
Format: [[CC]YY]MMDDhhmm[.ss]
- CCYY = year (can skip century → uses current century)
- MM = month (01–12)
- DD = day (01–31)
- hh = hour (00–23)
- mm = minute (00–59)
- .ss = seconds (optional)
Real examples (try them!):
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# 1 January 2025 midnight touch -t 202501010000 important.txt # 15 August 2023 at 14:30 touch -t 202308151430 meeting-notes.txt # 31 October 2024 Halloween 23:59:59 touch -t 202410312359.59 scary.pdf # Just 2 days ago at same time as now (trick) touch -t $(date +%Y%m%d%H%M -d "2 days ago") backup.tar.gz |
Way B: -d → human readable date (more flexible, very nice)
You can write almost natural English dates:
|
0 1 2 3 4 5 6 7 8 9 10 11 12 |
touch -d "2025-02-14 08:30" valentine.txt touch -d "yesterday" temp.log touch -d "2 days ago" old-data.csv touch -d "next Friday" future-plan.txt touch -d "1 month ago" monthly-report.xlsx touch -d "2024-12-25 20:00:30" christmas-photo.jpg touch -d "@1735680000" unix-time-file.bin # Unix timestamp in seconds |
-d is very smart — it understands lots of formats.
5. Copy timestamp from another file (very useful!)
|
0 1 2 3 4 5 6 |
touch -r master-file.txt copy-of-master.txt |
Now copy-of-master.txt has exact same mtime & atime as master-file.txt.
Great for keeping same timestamp after editing/copying.
6. Do NOT create file if missing (very important in scripts)
|
0 1 2 3 4 5 6 |
touch -c must-already-exist.log |
If file doesn’t exist → do nothing (no error, no creation).
Very useful in automation scripts.
7. Combine options (real-world examples)
|
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# Only modification time + custom date touch -m -t 202506301200 half-year-report.pdf # Only access time + yesterday touch -a -d "yesterday" audit.log # Copy timestamp but only change modification time touch -m -r golden-file.bin new-version.bin # Halloween midnight but don't create if missing touch -c -t 202410310000 seasonal.txt |
Quick cheat-sheet table
| What you want | Command example |
|---|---|
| Create empty file | touch newfile.txt |
| Update to current time | touch existing.txt |
| Only update mtime | touch -m file.txt |
| Only update atime | touch -a file.txt |
| Set exact date/time (numeric) | touch -t 202512251200 gifts.txt |
| Set using English-like date | touch -d “last month” old-backup.tar.gz |
| Copy timestamp from other file | touch -r original.jpg duplicate.jpg |
| Don’t create if file missing | touch -c must-exist.log |
| Both custom time + only mtime | touch -m -d “2024-07-01 09:00” july-report.pdf |
Bonus tip: See ALL timestamps clearly
Always use this instead of ls -l when checking:
|
0 1 2 3 4 5 6 |
stat filename.txt |
It shows:
- Access: …
- Modify: …
- Change: …
- Birth: … (creation time — not all filesystems support it)
Now you should be able to use touch like a pro for timestamps!
Any specific situation you want to solve with touch? (example: “I want all photos to look like they were taken in 2023” or “I want to trick rsync”) — tell me and we’ll write the exact command together! 😄
