I'm working on a bash script that will go through a directory, find the sub-directories that have been created since the last time the script ran, count the results, and output that integer (will most likely be '1' or less per each instance run) to a file.
Give the circumstances, my previous (and very limited) experience with bash is not sufficient for me to pull this off.
The purpose, since it probably has bearing, is that my mail server stores files that it flags as viruses in a folder. It creates a sub-directory for each virus that it quarantines. I want to count those subdirectories and graph them with MRTG. Hence the script. I'm going to post what I've got so far and the purpose of it, because I'm told I have a very odd and efficient way of doing scripting.
Code:
virpath="/opt/zimbra/data/amavisd/tmp/"
hours=$(ls -l $virpath | cut -f8 -d \ | cut -f1 -d ":")
minutes=$(ls -l $virpath | cut -f8 -d \ | cut -f2 -d ":")
day=$(ls -l $virpath | cut -f7 -d \ )
month=$(ls -l $virpath | cut -f6 -d \ )
^^^This bit is to grab just the numbers for hours, minutes, and day of month. Unfortunately ls -l gives me the 3 letter abbreviation for the month. I created a if statement to convert this over to the numbers, which I won't post as it's many lines long.
Code:
currmonth=$(date +"%m")
currhour=$(date +"%k")
currminute=$(date +"%M")
currday=$(date +"%d")
^^^This bit is to grab the current values of time. This was required at the time and I'll explain more a few lines down.
Code:
lastmonth=$(cat /media/.lastmonth.txt)
lastday=$(cat /media/.lasthour.txt)
lastminute=$(cat /media/.lastminute.txt)
lasthour=$(cat /media/.lasthour.txt)
^^^This bit is to grab the previous values for comparison later. I was going to simply echo these out to variables, for example's sake, let's say:
Code:
current=$(echo "$currmonth$currday$currhour$currminute")
last=$(echo "$lastmonth$lastday$lasthour$lastminute")
if [ $last -lt $current ]; then
blah blah blah
But then it dawned on me that it wouldn't work because I would have to not count the directories that have already been counted and count the ones that have not been counted. Given that the purpose of this is to generate a graph about every 5 minutes, using find won't work because, to my knowledge, that will only find things based on whole day values, I need it almost down to the minute. I thought about arrays, but, again, that's way out of my realm of experience.
Does anyone have any suggestions to guide me along my path?