计算数字随时间的变化率

计算数字随时间的变化率

我有一个处理文件的程序。我可以使用 ls | wc 来查看文件数量下降,如下所示:

ubuntu@h:/home/user/data/013176$ ls -1 | wc
3245666 29210987 246670579
ubuntu@h:/home/user/data/013176$ ls -1 | wc
2768811 24919292 210429599
ubuntu@h:/home/user/data/013176$ ls -1 | wc
2662466 23962187 202347379

我想知道是否有一个工具可以计算文件的消耗率,以便随着时间的推移看到类似的情况:

Current File Count: 2662466 rate = 5.6/s

如果它可以根据当前计数和速率估计完成时间就更好了

答案1

一个非常简单的概念证明就可以满足您的需求。

#!/bin/bash

initial_state=$(ls -1 | wc -l)
sleep 1
current_state=$(ls -1 | wc -l)
rate=$(echo $initial_state - $current_state | bc)
eta=$(echo $current_state / $rate | bc)

echo "Current file count: $current_state rate = $rate /s"
echo "Aprox. time to completion: $eta"

这将读取其运行目录的初始状态,然后在 1 秒(大约)后重新检查,然后为您提供每秒输出的变化率。从那里计算近似值就很简单了。完成时间。

编辑:添加了非常非常简单和粗略的完成时间。

编辑:要获得实时更新,您可以向脚本添加一个简单的 while 循环,但这样做会在终端运行时锁定该终端。

相关内容