58

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
56

Found it here

for f in /abcd/xyz* do
   [ "$f" -nt /abcd/test.txt ] && echo "file f$ found" done
| improve this answer | |
  • 14
    Also -ot is "older than". – Paused until further notice. 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 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 at 19:43
  • 1
    @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 at 9:00
42
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
| improve this answer | |
8

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.

| improve this answer | |
  • 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
  • 2
    @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 – Paused until further notice. 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. – Paused until further notice. Aug 26 '16 at 13:36
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
| improve this answer | |
2
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).

| improve this answer | |
  • 1
    -nt is generally better, but this is still a useful post... – BuvinJ Nov 16 '15 at 18:04
0

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

| improve this answer | |
  • -t is modification time; -u is access time – Paused until further notice. Sep 13 '10 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 '10 at 20:16
  • though perhaps not as good as saying ordered by date/time ;-) – barlop Sep 13 '10 at 20:22
0

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 "
| improve this answer | |
  • 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. – Pimp Juice IT Jul 3 '17 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.