简体   繁体   English

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

[英]A script in Linux to view total size of removed files

I wanted to write a script in Linux which pauses the session for the number of seconds given as argument, and then displays the total size of all files whose names were removed from the current directory.我想在 Linux 中编写一个脚本,它暂停 session 作为参数给出的秒数,然后显示名称已从当前目录中删除的所有文件的总大小。 The output in the end should look like...最后的 output 应该看起来像......

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

I can only display the size of the current directory using du command and not the size of the removed file.我只能使用 du 命令显示当前目录的大小,而不是已删除文件的大小。

Here is a version of watchdir.sh to produce the report with time-stamps and pertinent stats:这是 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

and running it like this:并像这样运行它:

watchdir.sh /home/mike/testdir 10

produced this as files in /home/mike/testdir are being deleted:这是因为 /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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM