FG

How to compare file timestamps in bash?

Fresh3 days ago
Mar 15, 202646151 views
Confidence Score0%
0%

Problem

How do I compare the timestamp of two files? I tried this but it doesn't work: I printed both the time stamps, in order and it gives me So basically if comparison is not working, I guess. Or if there is any other nice way than what I am doing, please let me know. What do I have to change?

Error Output

file1time=`stat -c %Y fil1.txt`
file2time=`stat -c %Y file2.txt`
if[$file1time -gt $file2time];
then
 doSomething
fi

Unverified for your environment

Select your OS to check compatibility.

1 Fix

Canonical Fix
Unverified Fix
New Fix – Awaiting Verification

Fix for: How to compare file timestamps in bash?

Low Risk

The operators for comparing time stamps are: The mnemonic is easy: 'newer than' and 'older than'.

Awaiting Verification

Be the first to verify this fix

Sign in to verify this fix

Environment