0

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

Dennis Williamson
  • 60,515
  • 14
  • 113
  • 148
Prasenjit Patra
  • 61
  • 2
  • 3
  • 4

2 Answers2

1

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.

Kyle Smith
  • 9,563
  • 1
  • 30
  • 32
0

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.

womble
  • 95,029
  • 29
  • 173
  • 228