简体   繁体   English

如何找到目录中最新修改文件的时间戳(递归)?

[英]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

  1. %T@ returns the modification time as a unix timestamp, which is just what I need. %T@将修改时间作为 unix 时间戳返回,这正是我所需要的。
  2. sort -n sorts the timestamps numerically. sort -n按数字对时间戳进行sort -n
  3. 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

  1. '%T@,' returns the modification time of each file as a unix timestamp, all on the same line. '%T@,'将每个文件的修改时间作为 unix 时间戳返回,所有这些都在同一行上。
  2. cksum calculates the checksum of the timestamps. cksum计算时间戳的校验和。
  3. ???? ???
  4. Profit!!!!利润!!!!

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.

相关问题 如何递归查找并列出具有子目录和时间的目录中的最新修改文件 - How to recursively find and list the latest modified files in a directory with subdirectories and times 在目录中查找最旧的文件(递归) - Find the oldest file (recursively) in a directory 如何按文件类型递归查找文件并将它们复制到目录? - How to find files recursively by file type and copy them to a directory? 如何使用wget下载具有最新时间戳的目录 - how to use wget to download directory with latest timestamp 在目录中按名称查找文件的重复项-Linux - Find duplicates of a file by name in a directory recursively - Linux 在 ansible 中找到最后一分钟内修改的最新文件? - find the latest file that was modified within last minute in ansible? 如何识别目录中修改过的文件名? - How to identify modified file names in a directory? 递归查找目录中的文件数 - Recursively find the number of files in a directory 如何找到目录中的最新文件夹,然后相应地创建完整路径? - How to find the latest folder in a directory and then make a full path accordingly? Bash脚本,用于使用find命令在目录中递归搜索外部文件中定义的文件(文件类型) - Bash script for searching of files (file types) which are defined in the external file recursively in directory using find command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM