Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question

6 Answers 6

up vote 8 down vote accepted

Found it here

for f in /abcd/xyz* do
   [ "$f" -nt /abcd/test.txt ] && echo "file f$ found" done
share|improve this answer
3  
Also -ot is "older than". –  Dennis Williamson Sep 13 '10 at 19:41
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
share|improve this answer

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

share|improve this answer
    
-t is modification time; -u is access time –  Dennis Williamson 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
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).

share|improve this answer

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
share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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