shell脚本来算没有进入的目录中的文件在10秒 [英] shell script to count the no of incoming files in a directory in 10sec

查看:161
本文介绍了shell脚本来算没有进入的目录中的文件在10秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个shell脚本,将计算这些目录中的文件来(比如分钟)。

I have written a SHELL SCRIPT which will count the number of these files coming in the directory (say in minutes).

#!/bin/bash
LOCATION="D:/Dir1/*"
FILECOUNT=0
while true
do
  for item in $LOCATION
  do
    if [ -f $item ]
    then
       FILECOUNT=$[$FILECOUNT+1]
    fi
  done
  echo "No of files are $FILECOUNT"
  FILECOUNT=0
  sleep 30s
done

的要求是这些文件被在几秒钟内自动删除。因此,我们必须只计算文件的不同没有。同时,我们也不能确定这些文件有多少秒留在目录中。

The requirement is these files gets automatically deleted in few seconds. So we have to count only the distinct no of files. Also, we are not sure for how many sec these files remain in the directory.

请提供什么样的变化,我应该在上面code做的。

please provide what changes should I do in the above code.

推荐答案

如果你正在使用bash,无论如何,使用其内置的工具。

If you are using Bash anyway, use its built-in facilities.

#!/bin/bash

files=( D:/Dir1/* )
echo Count: "${#files[@]}"

请参阅例如阵列上的ASG章中的Bash

如果你想看到它被添加或两次迭代之间删除的文件,使用两个阵列和的比较它们

If you want to see which files were added or deleted between two iterations, use two arrays, and compare them.

同样,如果要排除的目录,你可以做一个阵列路口 D:/方向1 /*/.

Similarly, if you want to exclude directories, you can do an array intersection with D:/Dir1/*/.

最后,一个简单的差异之间的两个临时文件可能会接近你实际需要。例如参见监测目录的更改

Ultimately, a simple diff between two temp files might be closer to what you actually need. See e.g. Monitor Directory for Changes

这篇关于shell脚本来算没有进入的目录中的文件在10秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆