How can I verify that file2 was last modified after file1?

In this example, perl was modified more recently than stack. Is there a bash or Linux command that can compare these files based on the modification time?

-rw-r--r--    1 root     root         1577 Sep  7 22:55 stack
-rwxr-xr-x    1 root     root          626 Sep  7 23:10 perl

7 Answers 7


Found it here

for f in /abcd/xyz* do
   [ "$f" -nt /abcd/test.txt ] && echo "file f$ found" done
  • 16
    Also -ot is "older than". Sep 13, 2010 at 19:41
  • 2
    I think I've just run into a problem with this. For files with the same modification time down to seconds, they are the "same" age by these tests; both -nt and -ot give the same result (false). In my use case I've got one program reading the others output, and I need to make sure they have run in the correct order. In this case it seems necessary to compare nanoseconds.
    – sjmc
    May 10, 2019 at 13:29
  • @sjmc Modification times in nanoseconds can be inaccurate or simply not precise enough for that.
    – xdevs23
    Apr 15, 2020 at 13:41
  • 2
    @sjmc Depending on the implementation of the file system, two files being modified at the same time might get "batched" and thus assigned the same modification time. It also depends on how the modification time is determined. This is a can situation, not a must situation so it really just "depends".
    – xdevs23
    Apr 17, 2020 at 9:00
  • 1
    Note that “right side doesn't exist” also qualifies as "newer" without error. (This is quite useful in many copy-over-scripts)
    – Frank N
    Nov 16, 2022 at 9:10
if [[ FILE1 -nt FILE2 ]]; then
  echo FILE1 is newer than FILE2

Taken from 'man test'. Excerpt:

  FILE1 is newer (modification date) than FILE2

Another way to do this:

find -name file2 -newer file1

will return null if file2 is older or the same age as file1. It will return the name (and directory) of file2 if it's newer.

Be aware that Linux doesn't keep track of when files were created. These tests will be for the most recent modification date and time.

  • 2
    Linux does keep track of creation time: It's called ctime. It also keeps track of the last access time: atime. It just uses mtime generally as this is the last modification time, which is most useful.
    – Coroos
    Aug 26, 2016 at 10:22
  • 3
    @Coroos: ctime isn't creation time. It is the inode change time and gets updated when the file's attributes such as owner or permissions or changed or when you modify the file. Some file systems do support birth time, but the kernel does not. Stat shows an empty birth time. See unix.stackexchange.com/a/91200 Note that OS X supports birth time stat -f %SB filename Aug 26, 2016 at 12:54
  • Also see htrp://unix.stackexchange.com/a/50184 on how to use debugfs to see creation (birth) time in Linux. Aug 26, 2016 at 13:36
echo $(($(date -r file1 +%s)-$(date -r file2 +%s)))

If the result is > 0, the first file is newer. (Newer in terms of last modification-, not creation-time, which is stored on linux).

  • 2
    -nt is generally better, but this is still a useful post...
    – BuvinJ
    Nov 16, 2015 at 18:04

If you want more detailed information you can use the stat command

<tbielawa>@(fridge)[~/SuperUser] 03:15:10
$ touch firstFile
<tbielawa>@(fridge)[~/SuperUser] 03:15:24
$ touch secondFile
<tbielawa>@(fridge)[~/SuperUser] 03:15:45
$ stat firstFile 
  File: `firstFile'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 805h/2053d  Inode: 151528      Links: 1
Access: (0644/-rw-r--r--)  Uid: (  500/tbielawa)   Gid: (  500/tbielawa)
Access: 2010-09-14 03:15:24.938721003 -0400
Modify: 2010-09-14 03:15:24.938721003 -0400
Change: 2010-09-14 03:15:24.938721003 -0400
<tbielawa>@(fridge)[~/SuperUser] 03:15:48
$ stat secondFile 
  File: `secondFile'
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 805h/2053d  Inode: 151529      Links: 1
Access: (0644/-rw-r--r--)  Uid: (  500/tbielawa)   Gid: (  500/tbielawa)
Access: 2010-09-14 03:15:45.074722792 -0400
Modify: 2010-09-14 03:15:45.074722792 -0400
Change: 2010-09-14 03:15:45.074722792 -0400

According to this link, unix doesn't store creation date. http://www.issociate.de/board/post/302628/How_to_check_file_creation_date.html

But does store last access.

for last access

 ls -t  # displays in order of date. So the first one is the 

ls displays each file on a new line.

so ls -t displays the latest file on the first line etc.

  • You can use yourself to pick the first line.
  • You can use sed to pick the first line.

    ls -t php.exe php.ini | sed -n '1p' php.ini

could do -lt though you'll see that if you don't specify any files.. and it does the directory.. then it gives the total on the first line, so you pick the second line like $ls -lt | sed -n '2p'

A good one would be

ls -t | head -n 1


ls -lt | head   

displays the first 10 lines in order first file modified first and you can see which it is

  • -t is modification time; -u is access time Sep 13, 2010 at 20:03
  • thanks.. I was wondering about -u.. the distinction.. and yes as you suggest.. -t and -u ordered by time.. as man has it. better than saying it orders by date.
    – barlop
    Sep 13, 2010 at 20:16
  • though perhaps not as good as saying ordered by date/time ;-)
    – barlop
    Sep 13, 2010 at 20:22

Here is a little script, I did:

mtf0=`stat -c %Y file0`    
mtf1=`stat -c %Y file1`    
dt=$(( mtf1 - mtf0 ))    
[[ $dt -gt 0 ]] && echo "File F1 is newer than file F0 "
  • You should explain with more detail what this is doing with some references for the command so people that potentially use it have an opportunity to read about it or hear your break it down a bit more than just proving the commands and syntax. Jul 3, 2017 at 21:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .