如何在bash脚本中比较文件的时间戳 [英] How to compare timestamp on files in bash scripting

查看:72
本文介绍了如何在bash脚本中比较文件的时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

谢谢

推荐答案

人工测试

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

FILE1 -ot FILE2
          FILE1 is older than FILE2

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

这篇关于如何在bash脚本中比较文件的时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆