Linux 中的脚本,用于查看已删除文件的总大小



我想在Linux中编写一个脚本,该脚本将会话暂停为参数的秒数,然后显示名称已从当前目录中删除的所有文件的总大小。最后的输出应该看起来像...

$ watchdir 60
# sleeping for a while ...
removed files total size: (size of removed files) eg.2345

我只能使用 du 命令显示当前目录的大小,而不能显示已删除文件的大小。

以下是生成带有时间戳和相关统计信息的报告的 watchdir.sh 版本:

#!/bin/bash
[ $# -ne 2 ] && echo "Dir and Sleep_seconds?" && exit
DIR=$1
SLEEPTIME=$2
# Note: The assumption is that DIR size is always decreasing as files are being deleted
PREV_SIZE=`du -s $DIR | awk '{print $1}'`
while true
do
CURR_SIZE=`du -s $DIR | awk '{print $1}'`
DIFF_SIZE=$(( $PREV_SIZE - $CURR_SIZE ))
echo "`date '+%Y-%m-%d %T'` CURR_SIZE=$CURR_SIZE REMOVED=$DIFF_SIZE"
PREV_SIZE=$CURR_SIZE
sleep $SLEEPTIME
done

并像这样运行它:

watchdir.sh /home/mike/testdir 10

产生这个,因为/home/mike/testdir 中的文件被删除:

2020-07-04 00:29:43 CURR_SIZE=736 REMOVED=0
2020-07-04 00:29:53 CURR_SIZE=696 REMOVED=40
2020-07-04 00:30:03 CURR_SIZE=668 REMOVED=28
2020-07-04 00:30:13 CURR_SIZE=652 REMOVED=16
2020-07-04 00:30:23 CURR_SIZE=640 REMOVED=12
2020-07-04 00:30:33 CURR_SIZE=640 REMOVED=0

最新更新