![](/img/trans.png)
[英]How to list the files and folders in a directory with its total size in Linux?
[英]A script in Linux to view total size of removed files
我想在 Linux 中编写一个脚本,它暂停 session 作为参数给出的秒数,然后显示名称已从当前目录中删除的所有文件的总大小。 最后的 output 应该看起来像......
$ 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.