Alfred’s Computing Weblog

Alfred Java-cored Computing Weblog

Linux | Shell Script : Compare File Last Modification Date

leave a comment »

test is a very useful command in Linux, esp when you’re writing a shell script.
There is some very useful function the test can do it for you easily.
Today, I would like to introduce the powerful tools in test to do file’s last modified time comparision.
The keyword is -nt and -ot. Both command required 2 inputs to be place before and after the keyword,
eg: FILE1 -nt FILE2.

-nt indicates that FILE1 is NEWER (modification date) than FILE2.
-ot indicates that FILE1 is OLDER (modification date) than FILE2.

Here is a sample yet complete shell script to describe it..

#!/bin/bash
file1=newer_file.txt
file2=older_file.txt

if test $file1 -nt $file2
then
    echo "$file1 is NEWER than $file2"
else
    echo "$file1 is OLDER than $file2"
fi

Written by Alfred

February 10, 2009 at 17:17

Posted in linux

Tagged with , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: