[英]How to find the timestamp of the latest modified file in a directory (recursively)?
I'm working on a process that needs to be restarted upon any change to any file in a specified directory, recursively.我正在处理一个需要在对指定目录中的任何文件进行任何更改时以递归方式重新启动的进程。
I want to avoid using anything heavy, like inotify
.我想避免使用任何沉重的东西,比如
inotify
。 I don't need to know which files were updated, but rather only whether or not files were updated at all.我不需要知道哪些文件被更新,而只需要知道文件是否被更新。 Moreover, I don't need to be notified of every change, but rather only to know if any changes have happened at a specific interval, dynamically determined by the process.
此外,我不需要收到每次更改的通知,而只需要知道在特定时间间隔内是否发生了任何更改,由流程动态确定。
There has to be a way to do this with a fairly simple bash command.必须有一种方法可以使用相当简单的 bash 命令来做到这一点。 I don't mind having to execute the command multiple times;
我不介意多次执行命令; performance is not my primary concern for this use case.
性能不是我对这个用例的主要关注点。 However, it would be preferable for the command to be as fast as possible.
但是,命令越快越好。
The only output I need is the timestamp of the last change, so I can compare it to the timestamp that I have stored in memory.我需要的唯一输出是上次更改的时间戳,因此我可以将其与存储在内存中的时间戳进行比较。
I'm also open to better solutions.我也愿意接受更好的解决方案。
I actually found a good answer from another closely related question .我实际上从另一个密切相关的问题中找到了一个很好的答案。
I've only modified the command a little to adapt it to my needs:我只是稍微修改了命令以使其适应我的需要:
find . -type f -printf '%T@\\n' | sort -n | tail -1
%T@
returns the modification time as a unix timestamp, which is just what I need. %T@
将修改时间作为 unix 时间戳返回,这正是我所需要的。sort -n
sorts the timestamps numerically. sort -n
按数字对时间戳进行sort -n
。tail -1
only keeps the last/highest timestamp. tail -1
只保留最后/最高时间戳。 It runs fairly quickly;它运行得相当快; ~400ms on my entire home directory, and ~30ms on the intended directory (measured using
time [command]
).在我的整个主目录上约 400 毫秒,在预期目录上约 30 毫秒(使用
time [command]
测量)。
I just thought of an even better solution than the previous one , which also allows me to know about deleted files.我刚刚想到了一个比前一个更好的解决方案,它也让我知道删除的文件。
The idea is to use a checksum, but not a checksum of all files;这个想法是使用校验和,而不是所有文件的校验和; rather, we can only do a checksum of the timestamps.
相反,我们只能对时间戳进行校验和。 If anything changes at all (new files, deleted files, modified files), then the checksum will change also!
如果有任何变化(新文件、删除的文件、修改的文件),那么校验和也会发生变化!
find . -type f -printf '%T@,' | cksum
'%T@,'
returns the modification time of each file as a unix timestamp, all on the same line. '%T@,'
将每个文件的修改时间作为 unix 时间戳返回,所有这些都在同一行上。cksum
calculates the checksum of the timestamps. cksum
计算时间戳的校验和。 It's actually even faster than the previous solution (by ~20%), because we don't need to sort (which is one of the slowest operations).它实际上比之前的解决方案还要快(大约 20%),因为我们不需要排序(这是最慢的操作之一)。 Even a checksum will be much faster, especially on such a small amount of data (22 bytes per timestamp), instead of doing a checksum on each file.
即使是校验和也会快得多,尤其是在如此少量的数据(每个时间戳 22 字节)上,而不是对每个文件进行校验和。
Instead of remembering the timestamp of the last change, you could remember the last file that changed and find newer files using您可以记住上次更改的文件并使用以下命令查找更新的文件,而不是记住上次更改的时间戳
find . -type f -newer "$lastfilethatchanged"
This does not work, however, if the same file changes again.但是,如果同一文件再次更改,这将不起作用。 Thus, you might need to create a temporary file with
touch
first:因此,您可能需要先使用
touch
创建一个临时文件:
touch --date="$previoustimestamp" "$tempfile"
find . -type f -newer "$tempfile"
where "$tempfile"
could be, for example, in the memory at /dev/shm/
.例如,其中
"$tempfile"
可以位于/dev/shm/
的内存中。
$ find ./ -name "*.sqlite" -ls 在这里您可以使用此命令获取文件信息。使用过滤器获取时间戳
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.