Check the file size - help

I want to write a batch job (ksh) with the following requirement

we have file feeds coming to our system from other team,
if the file size is greater than expected then we dont need to process the file for the day
and need to archive the file and send email notification to the manager saying 'file size exceeded max limit.

For this I have a file called file_size.dat which have the information on the location ,expectedsize and batch file associated with it.
for eg.file_size.dat entries

/home/test.dat|1000000|test.bat
/home/test1.dat| 2000000|test1.bat

if the file size is greater than expected then rename the file to test.dat.big
Can any one help me out?

To check the filesize, you could use different commands, of which some are:

ls
stat
...

Maybe write a while/read loop that reads in your file, setting the IFS=| and compare inside that loop the size with the gathered size from one of the commands you have chosen to do that.

To send a mail, use the mail command. There is a man page for it as well as for the other commands on your box or on the web.

If you have a particular question and maybe show the code you have tried so far, do not hesitate to ask here.

1 Like

i am also new to unix and shell. i have done something both the awk things are working. but not getting the way to do both operation into 1. but i think some1 might be able to help u...

ls -l | awk  '{print $1 "     the size of this file is =" $5}'

this particular commands shows all the file with their name and size present in front of them..

ls -l |awk  '{if ($5 > 20 ) print "$1 size is bigger than 20" ; else print "$1 is a small file"}'

this only shows $1 instead of file name and tells whose size is bigger..

ls -l |awk  '{if ($5 > 20 ) {mv $1 $1.dat} }'

this is also not working can anyone please help me with better way., this is my 1st try with awk so not sure if i am doing or bad...

i am not able to do the nested if in awk but i m sure some1 can help u with this.

You did it right in the first command. Also you messed sytax a little:

ls -l |awk  '{if ($5 > 20 ) {print $1" size is bigger than 20"}  else {print $1" is a small file"}}'

As for moving files and other things that need external tools use system() command:

ls -l |awk  '{if ($5 > 20 ) {system("mv "$1 $1".dat")} }'

thanks bartus this is what i was missing.. can u also explain me what is this system is being used for??

ls -l |awk  '{if ($5 > 20 ) {system("mv "$1 $1".dat")} }'

i feel damn happy atleast somethin works now.. earlier it was like something is happening and i have no ideas.


#! /bin/ksh

n=`wc -l < test.dat`
i=1

while [ "$i" -le "$n" ]
do    

# read line by line from the input file   
line=`cat test.dat | head -$i | tail -1`

file=`echo $line | cut -c1-100 | sed -e 's/^ *//g;s/ *$//g'`

size=`echo $line | cut -c101-120 | sed -e 's/^ *//g;s/ *$//g'`

batch_job=`echo $line | cut -c121-160 | sed -e 's/^ *//g;s/ *$//g'`


if [ $(wc -l < `echo $file` ) -gt `echo $size` ] #to notify if the file has more than 100 lines
then
mv `echo $file` `echo $file`.big;
echo "Membership file is large" |mailx -s "Membership file exceeded 200k records" "test@abco.com"

fi


i=`expr $i + 1`
done

This is my trail code