1

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.

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

tripleee
  • 175,061
  • 34
  • 275
  • 318
abc
  • 37
  • 1
  • 10

2 Answers2

3

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

#!/bin/bash

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

See e.g. the ASG chapter on arrays in Bash

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

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

Community
  • 1
  • 1
tripleee
  • 175,061
  • 34
  • 275
  • 318
  • can you please provide me a script for your answer "If you want to see which files were added or deleted between two iterations, use two arrays, and compare them." – abc Sep 19 '13 at 03:34
2

Why can't you just do:

FILECOUNT=`find -type f $LOCATION | wc -l`
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • 2
    This will recurse into subdirectories. You can add `-maxdepth 1` to disable directory recursion, and remove `-type f` if you want to count directories etc, too. – tripleee Sep 16 '13 at 05:22