How to compare timestamp on files in bash scriptin

2019-06-23 18:25发布

问题:

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

回答1:

man test

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

FILE1 -ot FILE2
          FILE1 is older than FILE2

...
-e FILE
          FILE exists
....


回答2:

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


回答3:

You should be able to just do a cp.

cp -Ru /Your/original/path/ /destination/path/location/


标签: bash shell