bash 如何比较bash脚本中文件的时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14674969/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 04:26:26  来源:igfitidea点击:

How to compare timestamp on files in bash scripting

bashshell

提问by user466663

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.

我必须编写一个 bash shell 脚本来实现以下功能。从目录中获取所有文件的列表。在目标目录中,如果存在这些文件中的任何一个,则比较日期。仅当源文件比目标文件新时,才将文件直接复制到目标。必须为每个文件执行此操作。我创建了一个 for 循环来实现复制部分。但我需要帮助来比较文件的日期。

Thanks

谢谢

回答by michaelmeyer

man test

man test

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

FILE1 -ot FILE2
          FILE1 is older than FILE2

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

回答by Julien Palard

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

回答by user1453561

You should be able to just do a cp.

你应该可以只做一个cp。

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