2013-02-03 34 views
4

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

感谢

回答

5

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 
+0

假设GNU'cp'。 –

1

你应该能够只是做一个CP。

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

你应该解释不寻常的选项 - '-u'选项。它是GNU'cp'的一部分,但不是我知道的任何其他版本(无论如何,都不在POSIX中,也不在BSD中 - 或BSD中,由Mac OS X代表)。 –