80

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
Daniel Beck
  • 110,419
lidia
  • 919
  • 1
  • 9
  • 9

7 Answers7

84

Found it here

for f in /abcd/xyz* do
   [ "$f" -nt /abcd/test.txt ] && echo "file f$ found" done
Chealion
  • 25,777
subanki
  • 7,676
  • 16
    Also -ot is "older than". – Dennis Williamson Sep 13 '10 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 '19 at 13:29
  • @sjmc Modification times in nanoseconds can be inaccurate or simply not precise enough for that. – xdevs23 Apr 15 '20 at 13:41
  • @xdevs23 Could you give more details, or are you aware of a better way to test which file is older when both -nt and -ot return false? – sjmc Apr 16 '20 at 19:43
  • 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 '20 at 09: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 '22 at 09:10
48
if [[ FILE1 -nt FILE2 ]]; then
  echo FILE1 is newer than FILE2
fi

Taken from 'man test'. Excerpt:

FILE1 -nt FILE2
  FILE1 is newer (modification date) than FILE2
Eddie Parker
  • 2,914
9

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 '16 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 http://unix.stackexchange.com/a/91200 Note that OS X supports birth time stat -f %SB filename – Dennis Williamson Aug 26 '16 at 12:54
  • Also see htrp://unix.stackexchange.com/a/50184 on how to use debugfs to see creation (birth) time in Linux. – Dennis Williamson Aug 26 '16 at 13:36
3
echo $(($(date -r file1 +%s)-$(date -r file2 +%s)))
2208

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

user unknown
  • 1,852
2

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
Tim Bielawa
  • 1,290
1

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. – Vomit IT - Chunky Mess Style Jul 03 '17 at 21:02
1

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

or

ls -lt | head   

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

user unknown
  • 1,852
barlop
  • 23,849