检查目录树中文件时间戳的快速方法
Fast way to check timestamp of files in directory tree
我需要每秒检查目录树中的文件结构,但我的脚本花费的时间太长,因为我怀疑在大型目录树中收集时间戳并根据其名称计算 base64 需要一些时间。
如何收集树结构,然后将它们存储在数组中,并循环遍历每个文件,在数组中收集它们的时间戳,其中数组键是来自文件路径的密码?
稍后我将数组与旧版本进行比较,这增加了额外的浪费时间,所以我想提高效率。
shopt -s globstar dotglob
files=("$base"/**)
new() {
keys=("${!files_new[@]}")
for i in "${keys[@]::2}"
do
unset "files_new[$i]"
done
for file in "${files[@]}"
do
stamp=$(stat -c "%Y" "$file")
hash=$(echo "$file" | base64)
files_new[$hash]=$stamp
done
}
我按照 Peter Cordes 的建议,通过使用 Find
命令和 -printf
输出实现了更快的探测。循环从 3 秒变为大约 300 毫秒。
loop() {
while read string
do
stamp=${string%|*}
file=${string#*|}
files_new[$file]=$stamp
done < <(find "${base}" -name "*" -printf "%TD%TT|%p\n")
}
我需要每秒检查目录树中的文件结构,但我的脚本花费的时间太长,因为我怀疑在大型目录树中收集时间戳并根据其名称计算 base64 需要一些时间。
如何收集树结构,然后将它们存储在数组中,并循环遍历每个文件,在数组中收集它们的时间戳,其中数组键是来自文件路径的密码?
稍后我将数组与旧版本进行比较,这增加了额外的浪费时间,所以我想提高效率。
shopt -s globstar dotglob
files=("$base"/**)
new() {
keys=("${!files_new[@]}")
for i in "${keys[@]::2}"
do
unset "files_new[$i]"
done
for file in "${files[@]}"
do
stamp=$(stat -c "%Y" "$file")
hash=$(echo "$file" | base64)
files_new[$hash]=$stamp
done
}
我按照 Peter Cordes 的建议,通过使用 Find
命令和 -printf
输出实现了更快的探测。循环从 3 秒变为大约 300 毫秒。
loop() {
while read string
do
stamp=${string%|*}
file=${string#*|}
files_new[$file]=$stamp
done < <(find "${base}" -name "*" -printf "%TD%TT|%p\n")
}