Check the file size - help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check the file size - help
# 1  
Old 06-09-2010
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
Code:
/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?

Moderator's Comments:
Mod Comment Use code tags please, ty.

Last edited by zaxxon; 06-10-2010 at 06:11 AM.. Reason: code tags
# 2  
Old 06-10-2010
To check the filesize, you could use different commands, of which some are:
Code:
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.
This User Gave Thanks to zaxxon For This Post:
# 3  
Old 06-10-2010
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...

Code:
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..

Code:
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..

Code:
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.
# 4  
Old 06-10-2010
You did it right in the first command. Also you messed sytax a little:
Code:
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:
Code:
ls -l |awk  '{if ($5 > 20 ) {system("mv "$1 $1".dat")} }'

# 5  
Old 06-10-2010
thanks bartus this is what i was missing.. can u also explain me what is this system is being used for??
Code:
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.
# 6  
Old 06-10-2010
Code:
#! /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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Check file size and mail

Hi, I am trying to write a script which will check if the filesize is grather than 0 KB, compress the file and send to the email list else if the file size is zero KB don't send a mail update the log if then echo "Validate the file" | mailx -s " There are errors : " ${EMAIL_LIST} else... (5 Replies)
Discussion started by: mora
5 Replies

2. Shell Programming and Scripting

File size check

I am trying to check whether two files are empty or not using below if condition but its checking for only one file if ] Again I tried if && ] Need your assistance (2 Replies)
Discussion started by: Aditya_001
2 Replies

3. Shell Programming and Scripting

Check a file size

I'm doing a script thats check if mylogfile.log is bigger then 5000 but i dont know how to write it. thanks in avance. (6 Replies)
Discussion started by: Froob
6 Replies

4. Shell Programming and Scripting

Check the presence of file >size?

Hi, Following script work fine: #!/bin/bash FILE=$1 if ; then echo Yay else echo Boo fi But I would like to add another condition that if FILE... (3 Replies)
Discussion started by: nrjrasaxena
3 Replies

5. UNIX for Dummies Questions & Answers

How to check the buffer size of a file?

I have a c program and I want to know what command to use to display the current buffer size of the file using Terminal in Unix? (0 Replies)
Discussion started by: Izzy123
0 Replies

6. Shell Programming and Scripting

check the file size

if ; then cp /tmp/testfolder/*.* ~/new/logs/ else echo "No files today" exit fi The problem is this doen't work when there is more than 1 file. Please tell me how to take the latest file and check the size of the file in a directory (1 Reply)
Discussion started by: sandy1028
1 Replies

7. Shell Programming and Scripting

Check for file size is zero or not.

I have following script on AIX/KSH if ] ; then echo "filename exists and is > 0 bytes" else echo "filename does not exist or is zero length" fi It is not working. What is wrong here??? (3 Replies)
Discussion started by: Hangman2
3 Replies

8. Shell Programming and Scripting

To check file size

Hi All, I am in small problem.. i have one script which transfers some big files to my ftp usign normal command like put .... my problem is how to check whether my file have been transferred successfully on ftp or not... i know only inside ftp we have option like 'size' command which... (2 Replies)
Discussion started by: Shahul
2 Replies

9. Shell Programming and Scripting

file size check

How can I perform size check of any character file(which switch)? For example: I have to perform certain actions if file size is not zero. How can I do that? Is this syntax fine? if test ! -z $filename then fi (2 Replies)
Discussion started by: malaymaru
2 Replies

10. UNIX for Dummies Questions & Answers

Check file size

I need a unix script that will check the size of multiple files in the same directory or from a text file. (6 Replies)
Discussion started by: alnita
6 Replies
Login or Register to Ask a Question