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:

  1. If the file does not exist → it creates an empty file
  2. 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)

Bash
  • If file didn’t exist → now it exists (size = 0 bytes)
  • If file existed → mtime & atime become current time

Check timestamps like this:

Bash

2. Only update modification time (most common need)

Bash

Only mtime changes, atime stays old.

3. Only update access time

Bash

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!):

Bash

Way B: -d → human readable date (more flexible, very nice)

You can write almost natural English dates:

Bash

-d is very smart — it understands lots of formats.

5. Copy timestamp from another file (very useful!)

Bash

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)

Bash

If file doesn’t exist → do nothing (no error, no creation).

Very useful in automation scripts.

7. Combine options (real-world examples)

Bash

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:

Bash

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! 😄

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *