Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a requirement like following:

I have two dir: DIR1 and DIR2. Files of DIR1 are updated on a regular basis based on requirement. Then updated files are copied to DIR2. So I need to write a shell script which will tell us whether all the files in DIR2 are the updated files or not. This is to make sure that all required files were copied properly from DIR1 to DIR2.

Can anyone please help me how I'll compare the timestamp of a file between the 2 directories.

/home/user/DIR1]ls -l
-rwxrwxr-x   1 user  group          509 Jan 08 2009 file_1.dat 
-rwxrwxr-x   1 user  group           11 Sep 29 12:19 file_2.dat

/home/user/DIR2]ls -l
-rwxrwxr-x   1 user  group          509 Jan 08 2008 file_1.dat 
-rwxrwxr-x   1 user  group           40 Oct 21 12:19 file_2.dat

Thanks & Regards, Prasenjit

share|improve this question

I think the best approach here is to look at how you are actually updating the files in DIR2. Rather than comparing them afterwards, use a tool like rsync to do the copy. For example, in rsync you can run this command:

rsync -auv --delete /path/to/dir1/* /path/to/dir2/

This will copy all files that are newer in dir1 to dir2 and delete any files which no longer exist in dir1. Basically it's a reliable folder "update" without wasting the time of copying files which are already up to date.

share|improve this answer

Use stat -c %Y /some/filename to get the modification time of a file, which you can then compare in the usual fashions. Also consider using cp -u, which only copies files with a newer mtime, if you want to save a bit of time.

share|improve this answer

Your Answer


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.