如何找到目录中最新修改文件的时间戳(递归)?
How to find the timestamp of the latest modified file in a directory (recursively)?
我正在处理一个需要在对指定目录中的任何文件进行任何更改时递归地重新启动的进程。
我想避免使用任何重物,例如 inotify
。我不需要知道哪些文件已更新,而只需要知道文件是否已更新。此外,我不需要在每次更改时都收到通知,而只是知道是否在特定时间间隔内发生了任何更改,由流程动态确定。
必须有一种方法可以使用相当简单的 bash 命令来完成此操作。我不介意必须多次执行命令;性能不是我对这个用例的主要关注点。但是,命令越快越好。
我唯一需要的输出是上次更改的时间戳,因此我可以将它与我存储在内存中的时间戳进行比较。
我也乐于接受更好的解决方案。
我实际上从 another closely related question 找到了一个很好的答案。
我只是稍微修改了命令以适应我的需要:
find . -type f -printf '%T@\n' | sort -n | tail -1
%T@
returns修改时间为unix时间戳,这正是我需要的。
sort -n
按数字对时间戳进行排序。
tail -1
只保留 last/highest 时间戳。
运行速度相当快;在我的整个主目录上大约 400 毫秒,在预期目录上大约 30 毫秒(使用 time [command]
测量)。
$ 查找 ./ -name "*.sqlite" -ls
在这里你可以使用这个命令来获取你的文件的信息。使用过滤器来获取时间戳
I just thought of an even better solution than , 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.
cksum
calculates the checksum of the timestamps.
- ????
- 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). 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.
您可以记住上次更改的文件并使用
查找更新的文件,而不是记住上次更改的时间戳
find . -type f -newer "$lastfilethatchanged"
但是,如果同一个文件再次更改,这将不起作用。因此,您可能需要先用 touch
创建一个临时文件:
touch --date="$previoustimestamp" "$tempfile"
find . -type f -newer "$tempfile"
其中 "$tempfile"
可能位于 /dev/shm/
的内存中。
我正在处理一个需要在对指定目录中的任何文件进行任何更改时递归地重新启动的进程。
我想避免使用任何重物,例如 inotify
。我不需要知道哪些文件已更新,而只需要知道文件是否已更新。此外,我不需要在每次更改时都收到通知,而只是知道是否在特定时间间隔内发生了任何更改,由流程动态确定。
必须有一种方法可以使用相当简单的 bash 命令来完成此操作。我不介意必须多次执行命令;性能不是我对这个用例的主要关注点。但是,命令越快越好。
我唯一需要的输出是上次更改的时间戳,因此我可以将它与我存储在内存中的时间戳进行比较。
我也乐于接受更好的解决方案。
我实际上从 another closely related question 找到了一个很好的答案。
我只是稍微修改了命令以适应我的需要:
find . -type f -printf '%T@\n' | sort -n | tail -1
%T@
returns修改时间为unix时间戳,这正是我需要的。sort -n
按数字对时间戳进行排序。tail -1
只保留 last/highest 时间戳。
运行速度相当快;在我的整个主目录上大约 400 毫秒,在预期目录上大约 30 毫秒(使用 time [command]
测量)。
$ 查找 ./ -name "*.sqlite" -ls 在这里你可以使用这个命令来获取你的文件的信息。使用过滤器来获取时间戳
I just thought of an even better solution than
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.cksum
calculates the checksum of the timestamps.- ????
- 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). 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.
您可以记住上次更改的文件并使用
查找更新的文件,而不是记住上次更改的时间戳find . -type f -newer "$lastfilethatchanged"
但是,如果同一个文件再次更改,这将不起作用。因此,您可能需要先用 touch
创建一个临时文件:
touch --date="$previoustimestamp" "$tempfile"
find . -type f -newer "$tempfile"
其中 "$tempfile"
可能位于 /dev/shm/
的内存中。