Comparing files by date/time


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Comparing files by date/time
# 1  
Old 01-28-2015
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
# 2  
Old 01-28-2015
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).

Code:
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.
This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 01-28-2015
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.
# 4  
Old 01-28-2015
Use test -- the -nt comparison

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

Code:
# create file with names of duplicates
find /path -type f | xargs -l basename | awk '{arr[$0]++;next} END{for(i in arr){ if(arr[i]>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

Last edited by jim mcnamara; 01-28-2015 at 11:59 PM..
# 5  
Old 01-29-2015
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Files with date and time stamp

Hi Folks, Need a clarification on files with date and time stamp. Here is my requirement. There is a file created everyday with the following format "file.txt.YYYYMMDDHHMMSS". Now i need to check for this file and if it is available then i need to do some task to the file. I tried... (6 Replies)
Discussion started by: jayadanabalan
6 Replies

2. UNIX for Dummies Questions & Answers

Comparing two files with datestamp to current date

Hi, I am new to unix and I am stuck on how to compare two .zip file with date stamp in my directory. I need to compare out of the two file which is oldest to current date and unzip it after that done continue to unzip the second zip file. Thanks for your help. (5 Replies)
Discussion started by: lilvi3tboix1
5 Replies

3. Shell Programming and Scripting

How to catch date and time from log files?

Hi, Appli.log files contain the below data which updates continuously ================================================== =============== Tuple Created OrderEntry|66|39.0|ADML.L|16.89|GBP||GFD|000002889 41ORLO1|GB00B02J6398|80|XLON|UHORIZON|null|2011-05-09... (11 Replies)
Discussion started by: pspriyanka
11 Replies

4. AIX

find files with specific date and time

Hi, I wanna to find files with specific date and time. I know this command: ls -ltr /system1/*.505 | grep 'Jan 18 09:00'. I want the 'Jan 18 09:00' not hard coded, I want it coming from the system date and time. And I want the time varies from 09:00-09:05. Is this possible without heavy... (3 Replies)
Discussion started by: itik
3 Replies

5. UNIX for Dummies Questions & Answers

List files with date and time stamps only

Hi there, I'm using terminal on mac and using the ls -l command to list all the files in a directory. However, I only want to display the date and time stamp of each file rather than permissions, owner, group etc... Is this possible? Many thanks in advance Dave (2 Replies)
Discussion started by: davewg
2 Replies

6. Shell Programming and Scripting

Copying files created after a specified date/time

I need to write a script that copies files from one directory to another that were created after "today 6:30". This script will be NOT be ran at the same time each day. any help is appreciated. (2 Replies)
Discussion started by: jm6601
2 Replies

7. Shell Programming and Scripting

How do i collect date and time for list of files in a directory

How do i collect date and time for list of files in a directory using AWK Command (4 Replies)
Discussion started by: laknar
4 Replies

8. Shell Programming and Scripting

List files created between specific date and time

Hi I need to write a script to list files in a directory created within specific date and time for eg list files created between Apr 25 2007 11:00 to Apr 26 2007 18:00. and then i have to count them Any suggestions pls ? (3 Replies)
Discussion started by: jazjit
3 Replies

9. UNIX for Dummies Questions & Answers

Comparing files named by date/time

I've looked at several of the previous posts and can't seem to find any that pertain to my problem, I'd appreciate some help if possible. I have a directory with numerous logs of various names all named by heading and date ie. dog.20050529.log dog.20050530.log ... (2 Replies)
Discussion started by: gillr
2 Replies

10. UNIX for Dummies Questions & Answers

Renaming files to have date/time in filename

I have a program that will export my data to a single file, but it assigns a file name that is overridden every time I run the program. I need to change the file name to have a sequential number in the filename. How do I rename a file so that the filename contains the system date and time. I want... (5 Replies)
Discussion started by: wayneb
5 Replies
Login or Register to Ask a Question