Comparing files by date/time

I am trying to compare identically named files in different directories and replace a file only with a newer version. Is there a way of doing this?

TIA

rsync is a great tool for this sort of job.

He we are saying copy any newer files from the structure below directoryA to an equivalent structure below directoryB without creating any new files or folders in directoryB (only existing files in directoryB are updated).

rsync -av --existing --update /path/to/directoryA/ /path/to/directoryB

There are many options that control how rsync works so you should be able to tailor it to your particular requirements.

1 Like

I'm afraid we don't have rsync in our shop, but I think I have a solution. A program I've been working on kicks off a l -tT file1 file2 command and reads it back in. Unless the date/time stamps are identical it checks to see which file is on the first line. That will give me something to check within a script to set return-code before exiting.

Thanks for looking at it. You are now up to an even 1000 thanks.

Use test -- the -nt comparison

assume you have a file tree /path and you have a text file that list files with multiple names:

# create file with names of duplicates
find /path -type f | xargs -l basename | awk '{arr[$0]++;next} END{for(i in arr){ if(arr>1){print i} }}' > namefile
while read fname
do
   newest=""
  for file in $(find /path -name "$fname")
  do
      [ -z "$newest" ]  && newest="$file"  && continue
      [ $file -nt $newest ] && newest=$file
  done
  echo "$newest"
done < namefile > newest_files

I wrote to a separate file - you can verify duplicates. Otherwise pipe the awk directly into the while read loop and lose the < namefile

I wasn't able to get our system to accept -nt in a test command. Found a reference to it in SCO Unix in a Nutshell but I got "test: unknown operator -nt" when I put it in a testing script.

Thanks for looking at it though.