check files in a directory with bash script

Sun microsystems Sun fire v20z amd opter...
February 9, 2011 at 12:39:40
Specs: Linux
I would like to be able to check certain files in one directory if they are emtpy or older than 2 days. if they are, I would like to send myself an email. The directory has bunch of files, I am interested in certain ones that I know the names.

can somebody help?


See More: check files in a directory with bash script

Report •

#1
February 9, 2011 at 15:36:05

find files in the current director that are zero length:

find . -type f -size 0 -print

find files in the current directory that are older than 2 days:

find . -type f -mtime 2 -print

find has a -name option that let's you look for files. If your spec was more definite, I would comment further.


Report •

#2
February 10, 2011 at 07:25:07
there is a directory called /var/infiles, in this directory, there are bunch of files like today.csv, assets.csv, feed.txt and many others. I would like to be able to check only these files if they are 0 bytes or older than 2 days. I started to script this but I am getting bunch of syntax error. This is what i did so far:

#!/bin/bash

DIR=/var/infiles

for file in $DIR; do
if [ [ $file=today.csv ] || [ $file = assets.csv ] || [ $file = feed.txt ] ]; then
if [ ! -s $file ]; then
/bin/mail -s "Re: problem" admin@yahoo.com
fi
fi
done

I need to do the same for if the file older than 2 days below. Above if staments are not working, giving me errors like too many arguments


Report •

#3
February 10, 2011 at 08:25:20
You are using the [ incorrectly:

#!/bin/bash

# except for the syntax check, this is untested
DIR="/var/infiles"

for file in $DIR; do
if [[ ($file="today.csv") || ("$file" = "assets.csv") || ("$file" = "feed.txt") ]];
then
   if [ ! -s $file ]; then
      echo "email string"
   fi
fi
done


Report •

Related Solutions

#4
February 10, 2011 at 08:43:10
This is a way of doing it with the find command:

#!/bin/bash

DIR=/var/infiles

count=0
find $DIR -type f \( -name "today.csv" -o -name "feed.txt" -o -name "assets.csv" \)
-size 0 -print|while read file
do
   count=1
   break
done
if [[ count > 0 ]]
then
   echo "mail string"
fi

# end script

If you want to implement a 2-day check, you'll probably have to use find's -mtime option.


Report •

#5
February 10, 2011 at 08:47:44
this does not work either

Report •

#6
February 10, 2011 at 10:06:49
I had to do some cleanup but It works. thanks for the help.

Report •

Ask Question