How to compare timestamp on files in bash scriptin

2019-06-23 18:36发布

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

标签: bash shell
3条回答
We Are One
2楼-- · 2019-06-23 19:05

man test

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

FILE1 -ot FILE2
          FILE1 is older than FILE2

...
-e FILE
          FILE exists
....
查看更多
霸刀☆藐视天下
3楼-- · 2019-06-23 19:10
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
查看更多
smile是对你的礼貌
4楼-- · 2019-06-23 19:22

You should be able to just do a cp.

cp -Ru /Your/original/path/ /destination/path/location/
查看更多
登录 后发表回答