Linux 比较文件的日期 bash

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14802807/
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-08-06 18:59:27  来源:igfitidea点击:

compare file's date bash

linuxbashcentos

提问by Wael hamada

I'm working on a tiny dropbox-like bash script, how can I compare the dates of 2 files and replace the old one(s) with the new one without using rsyncis there any simple way to process this? can the SHA1help me with knowing the newer?

我正在开发一个类似于 dropbox 的 bash 脚本,如何比较 2 个文件的日期并将旧文件替换为新文件而不使用rsync有没有简单的方法来处理这个问题?SHA1可以帮助我了解更新吗?

采纳答案by Anton Kovalenko

You can compare file modification times with test, using -nt(newer than) and -ot(older than) operators:

您可以test使用-nt(newer than) 和-ot(older than) 运算符将文件修改时间与, 进行比较:

if [ "$file1" -ot "$file2" ]; then
    cp -f "$file2" "$file1"
fi

回答by Kent

how about

怎么样

 stat file|awk -F': ' '/Modify: /{print }'

回答by whatever4711

Or even shorter and nicer, look at man stat:

或者甚至更短更好,看看man stat

stat -c %y file

回答by Steven Penny

Here is a POSIX solution:

这是一个POSIX解决方案:

find -name file2 -newer file1