Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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

2 Answers 2

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.