Timestamps Change on File Copy | Generated by AI

Home 2026.04

Question: When copying a file to another file, if the content is the same, will mtime stay unchanged while ctime changes?


Answer:

Short answer: No — both mtime and ctime usually change when you use cp.

Let’s break it down carefully.


1. What cp actually does

When you run:

cp source.txt dest.txt

cp does not compare content. It simply:


2. What happens to timestamps

Case A: Destination file is overwritten

Even if the content is identical:

👉 So your assumption is incorrect:

mtime does NOT stay the same ✅ It will update


Case B: Destination file does NOT exist

A new file is created:


Case C: Using cp -p (preserve timestamps)

cp -p source.txt dest.txt

Now behavior changes:

Why?

👉 Important rule:

You cannot manually preserve ctime


3. Key Insight

Even if file content is identical:


4. When would mtime NOT change?

Only if:

Example tools that avoid rewriting:

rsync --ignore-existing
rsync --size-only
rsync --checksum

These can skip copying if files are identical.


5. Summary

Scenario mtime ctime
cp overwrite ✅ changes ✅ changes
cp new file ✅ new time ✅ new time
cp -p ❌ preserved ✅ changes
No write happens ❌ unchanged ❌ unchanged

6. Practical Tip

If you want to avoid unnecessary timestamp changes, use:

rsync -a --ignore-existing

or

rsync -a --checksum

If you want, I can explain why ctime is fundamentally unmodifiable at the filesystem level (this is a subtle but important Unix design decision).


Back Donate