Bash 脚本 — 确定文件是否被修改?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4696128/
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
Bash script — determine if file modified?
提问by Alan H.
I have a Bash script that repeatedly copies files every 5 seconds. But this is a touch overkill as usually there is no change.
我有一个 Bash 脚本,它每 5 秒重复复制一次文件。但这有点矫枉过正,因为通常没有变化。
I know about the Linux command watchbut as this script will be used on OS X computers (which don't have watch, and I don't want to make everyone install macports) I need to be able to check if a file is modified or not with straight Bash code.
我知道 Linux 命令,watch但由于此脚本将用于 OS X 计算机(没有watch,而且我不想让每个人都安装 macports)我需要能够检查文件是否被修改使用直接的 Bash 代码。
Should I be checking the file modified time? How can I do that?
我应该检查文件修改时间吗?我怎样才能做到这一点?
Edit:I was hoping to expand my script to do more than just copy the file, if it detected a change. So is there a pure-bash way to do this?
编辑:如果检测到更改,我希望扩展我的脚本以执行更多操作,而不仅仅是复制文件。那么有没有一种纯粹的 bash 方法来做到这一点?
回答by Glenn McAllister
I tend to agree with the rsyncanswer if you have big trees of files
to manage, but you can use the -u (--update) flag to cp to copy the
file(s) over only if the source is newer than the destination.
rsync如果您要管理大树文件,我倾向于同意答案,但是您可以使用 -u (--update) 标志到 cp 来复制文件,只有当源比目标新时。
cp -u
中央处理器
Edit
编辑
Since you've updated the question to indicate that you'd like to take
some additional actions, you'll want to use the -ntcheck
in the [(test) builtin command:
由于您已更新问题以表明您想采取一些额外的操作,因此您需要使用( ) 内置命令中的-nt检查:[test
#!/bin/bash
if [ -nt ]; then
echo "File 1 is newer than file 2"
else
echo "File 1 is older than file 2"
fi
From the man page:
从手册页:
file1 -nt file2
True if file1 is newer (according to modification date) than
file2, or if file1 exists and file2 does not.
Hope that helps.
希望有帮助。
回答by Paused until further notice.
回答by Jonathan Leffler
You might find it more reliable to detect changes in the file content by comparing the file size (if the sizes differ, the content does) and the hash of the contents. It probably doesn't matter much which hash you use for this purpose: SHA1 or even MD5 is probably adequate, and you might find that the cksumcommand is sufficient.
您可能会发现通过比较文件大小(如果大小不同,内容不同)和内容的散列来检测文件内容的变化更可靠。为此目的使用哪种散列可能并不重要:SHA1 甚至 MD5 可能就足够了,您可能会发现该cksum命令就足够了。
File modification times can change without changing the content (think touch file); file modification times can not change even when the content does (doing this is harder, but you could use touch -r ref-file fileto set the modification times of fileto match ref-fileafter editing the file).
文件修改次数可以在不改变内容的情况下改变(想想touch file);即使内容更改,文件修改时间也不能更改(这样做更难,但您可以在编辑文件后使用touch -r ref-file file来设置file匹配的修改时间ref-file)。
回答by Ignacio Vazquez-Abrams
No. You should be using rsyncor one of its frontends to copy the files, since it will detect if the files are different and only copy them if they are.
不。您应该使用rsync或其前端之一来复制文件,因为它会检测文件是否不同,并且仅在它们不同时才复制它们。

