How to compare timestamp with files in bash scripts

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

thanks

+4
source share
3 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 .... 
+5
source
 man cp | grep -C1 update -u, --update copy only when the SOURCE file is newer than the destination file or when the destination file is missing 
+2
source

You should be able to just execute cp.

 cp -Ru /Your/original/path/ /destination/path/location/ 
+1
source

All Articles