Empty Directory Check

Hi All,
I have a requirement to check all the files in a directory and mail non empty files
Files are in csv format , i need to skip header while checking
pls help

Thanks

check

for file in dir/*
do
if [ -s $file ] ; then
  echo "Non empty file"
else
  echo "File has size"
fi
done

cheers,
Devaraj Takhellambam

The -s will work for only normal file. It is not suitable for csv files

You can use the following script to do your requirement.

for file in `ls`
do
if [ "$(grep -c "." $file)" -ge 1 ];
then
echo "Non-Empty File"
echo "File Name:$file" | mail -s "Non-Empty File" <EMail-ID>
else
echo "Empty File"
fi
done

@thillai_selven
The "test -s " statement will work for any file (as distinct from directories, pipes etc.).
Core unix shell does not take into account MSDOS-style filenames and there is no file extension field in a unix inode.

thanks for the response
but again this will send separate mail for each of the non empty files
i need to send one consolidated mail with non empty files as attachment

To avoid separate mails for each file ,just store the non empty file's name in an array when you found the non empty file. After the loop execution manipulate the array elements and send as a mail.

Oops. vivekrag has read the requirement. In this problem a file counts as empty if it only contains a header line.
Mail is machine-specific. Please state what Operating System you have and which mail client you use.

Yeah gwrm,its simple as Karthik told,you can store it in an array or file whatever you want.Here I did some changes in my script to fulfill your requirement.

echo "Non-Empty Files" > Non-Empty
echo "---------------"
for file in `ls`
do
if [ "$(grep -c "." $file)" -ge 1 ];
then
echo "Non-Empty File"
echo "$file" >> Non-Empty
else
echo "Empty File"
fi
done
mail -s "Non-Empty Files" nvivek < Non-Empty

I assume your CSV file will have 1 line if it has only headers. My below code will check number of lines in a file & keeps track in log.txt file. This log.txt file is emailed at last. Hope this helps.

#!/bin/ksh

if test -f log.txt
then
rm log.txt
fi

for file in test/*
do
no_of_lines=`wc -l $file | awk '{print $1}'`
if [ $no_of_lines -lt 1 ]
then
echo "$file file is empty (or) having only headers" >> log.txt
else
echo "CSV files is correct" >> log.txt
fi
done

uuencode log.txt log.txt | mailx -s "CSV files update" <email id>

bsnithin thanks for the code
i need to send the content of the non empty file as attachment

Welcome !! This should also fetch contents of empty CSV files(files with header only)

#!/bin/ksh

if test -f log.txt
then
rm log.txt
fi

for file in test/*
do
no_of_lines=`wc -l $file | awk '{print $1}'`
if [ $no_of_lines -lt 1 ]
then
echo "$file file is empty (or) having only headers" >> log.txt
cat $file >> log.txt
else
echo "CSV files is correct" >> log.txt
fi
done

uuencode log.txt log.txt | mailx -s "CSV files update" nithin.sukumar@ge.com

You can tell if a file is empty using the -s file operator (-s means "exists and has nonzero size"). For example:

if [! -s $file ]

If you want to do something to files that exist and are empty you can combine this with the -e ("exists") operator, like this:

if [! -s $file && -e $file ]

I'm not entirely sure if that's the answer you were looking for, but that's my two cents worth.

The file operators are:
-a $file : file exists
-d $file : file is a directory
-e $file : file exists
-f $file : file is a regular file (not a directory or symbolic link)
-G $file : you are in the group that owns file
-h $file : file is a symbolic link
-N $file : file has been modified since it was last read
-O $file : you own file
-r $file : you have read permission on file
-s $file : file exists and is not empty
-w $file : you have write permission on file
-x $file : you have execute permission on file
$file1 -nt $file2 : file1 is newer than file2
$file1 -ot $file2 : file1 is older than file2

As an aside, I think Perl has a wider range of file operators than bash, so that may be something to explore.