Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare timestamp on files in bash scripting

Tags:

bash

shell

I have to write a bash shell script to achieve the following. Get the list of all files from a directory. In the destination directory, if any of these files exist compare the dates. Copy the files to the destination directly only when the source file is newer than the destination file. Has to do this for each file. I have created a for loop to achieve the copy portion. But I need help in comparing the dates of the files.

Thanks

like image 538
user466663 Avatar asked Feb 18 '23 15:02

user466663


1 Answers

man test

FILE1 -nt FILE2
          FILE1 is newer (modification date) than FILE2

FILE1 -ot FILE2
          FILE1 is older than FILE2

...
-e FILE
          FILE exists
....
like image 104
michaelmeyer Avatar answered Mar 03 '23 03:03

michaelmeyer