LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-25-2010, 06:00 PM   #1
buee
Member
 
Registered: May 2009
Posts: 81

Rep: Reputation: 16
Bash script to find newest files and count them


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?
 
Old 02-25-2010, 06:43 PM   #2
whizje
Member
 
Registered: Sep 2008
Location: The Netherlands
Distribution: Slackware64 current
Posts: 594

Rep: Reputation: 141Reputation: 141
Use find with -mmin see man find
with stat you can get the date of file
Code:
bash-3.1# > date.txt
bash-3.1# stat -c %Y date.txt
1267145285
bash-3.1# stat -c %y date.txt
2010-02-26 01:48:05.685865519 +0100

Last edited by whizje; 02-25-2010 at 06:55 PM. Reason: add stat
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
BASH script that removes sub-directories (not just files) but keeps the two newest. kennc Linux - Newbie 2 04-23-2009 01:09 PM
bash script to find files with shebang and then chmod nshewmaker Linux - General 8 11-27-2008 02:38 PM
Using Bash, Find script files in a directory or subdirectories within... ray5_83 Programming 4 10-10-2008 07:42 PM
how to find files using bash script prernasin Linux - Newbie 6 09-26-2007 08:57 AM
A bash script to find duplicate image files fotoguy Programming 7 01-25-2007 06:47 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:03 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration