Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

check file size script

Status
Not open for further replies.

deefeketje

Programmer
Nov 10, 2005
5
BE
Hi

I'm new in AIX, so i really don't know much about it.

But i'm trying to write a script that does check a particular file for the size of it.
and when it has reached a size, the script should rename it to another file nam.

Can anyone tell me how to begin with this?

Thank you
 
Check out find with the -s option:

Something like:

find /[dir to search] -name [yourfile] -s +[no of 512 byte blocks required]

Then include a routine to rename the file if the result of the above is success.
 
I would follow Ken's advice but I would use 'greater than' rather than compare to an exact size.

If you file grows quickly, then it may exceed the size you specify in your script, without actually being that size at the time your script runs.

So, when file greater than <size>
do
rename file
done

Alex
 
Alex. Fair point, but I was under the imprssion that the flag -s +[value] did that in any case?
 
Can you give an example for:
find /[dir to search] -name [yourfile] -s +[no of 512 byte blocks required]

I'm trying different things but it not works.

like:
find /u2/imps/db -name [file.jn*] -s +[20488]

and it returns:
find: 0652-017 -s is not a valid option.


anyone?
 
Ken

Can't see that in the man page, shouldn't it be 'size'

deefeketje, try using size instead of 's'

Alex
 
Well fiddilly-dee! Of course it should be -size. Silly me [blush] !

Oh, and leave out the square brackets, they're just there to indicate where your actual file name and specified size should go.
 
now I get an error:
fond: 0652-009: there is a missing conjonction

I entered this:
find /u2/imps/db -name file.jn* -size +20488c

(i saw in the man that the C is for the actual bytes and not for blocks)
 
ok tnx guys!

just another question.

I've found the file with that "find"-command.

Now how can I rename it?

like alexhu said?

something like this?
if find dirpath -name 'filename' -size 99999c
do
something
done
 
One approach (and there will probably be other more elegant solutions) would be to create a flag file from the find:

First remove the previous flagfile

rm flagfile

Then

find dirpath -name 'filename' -size 99999c > flagfile

then check if the flagfile is empty:

test -s flagfile

Apply an if/then to check the result of the test:

if [ $? -eq 0 ] (0 being success, ie the file exists and isn't empty.)
then
mv filename anotherfilename
else
echo "No file found"
fi

Hope this helps.
 
find -size" is not really necessary, imho ls -l will do:

Code:
ORIGFILE=/your/dir/and/file/name
NEWFILE=${ORIGFILE}.new
MAXSIZE=99999

SIZE=$(ls -l ${FILEPATH}|awk '{print $5}')

if [ ${SIZE} -gt ${MAXSIZE} ]
then
 mv ${ORIGFILE} ${NEWFILE}
fi

HTH,

p5wizard
 
p5wizard thanks!!

but it still gives 1 error:
script[5]: 0403-057 Syntax error at line 8 : 'fi' is not expected.

Code:
1 ORIGFILE=/u2/imps/db/file.jn.200504041703
2 NEWFILE=${ORIGFILE}.new
3 MAXSIZE=30000
4 SIZE=$(ls -l ${FILEPATH}|awk '{print $5}')
5 if [ ${SIZE} -gt ${MAXSIZE} ]
6 then
7 mv ${ORIGFILE} ${NEWFILE}
8 fi
 
Hi,

Your variable ${FILEPATH} is not assigned in line 4.
I think it will be replaced by ORIGFILE
 
You're absolutely right [blush] ...

That's what happens when I answer a post while "freetyping code" (and without verifying my code at a shell prompt.)



HTH,

p5wizard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top