View Single Post

  #3 (permalink)  
Old 07-20-2004
Moe Trin
 
Posts: n/a
Default Re: File Size In A Bash Script.

In article <sfslf0lqca8smppk8p86bf70f62ebp2t7q@4ax.com>, Adam wrote:
>I would like to run a small script from Cron, to monitor certain file
>size and then take an action.


_Normally_ that would be done with the 'find' command with the '-size'
option.

>I have no idea how to capture a file size in bash, any suggestions?


Go to the Linux Documentation Project, and get two magnificant books
for free. Try http://tldp.org/guides.html and what you are
looking for is

Bash-Beginners-Guide
abs-guide

Another good document to start with is Bash-Prog-Intro-HOWTO although
it's nearly four years old.

>I read somewhere that we can use ls -s, but how to capture that in the
>script I have no idea.


-s gives the size in blocks - you may want to get the actual size in
bytes by using the ls -l command. As to how:

for FILE in * ; do
SIZE=`/bin/ls -l $FILE | /bin/awk '{ print $5 }'`
if [ $SIZE -eq "12345" ] ; then
[Action here]
fi
done

but watch those quotes. The ones around the { print $5 } are the
regular single quotes (next to the Enter key), while the one following
the equal sign (and end of that line) are back-tics (above the Tab key).
They are used when you want to capture the output of a command. Thus,

SIZE=`ls -l /var/dev/foo`

would take the output of the command 'ls -l /var/dev/foo' and stick that
into variable SIZE. To view the _contents of variable SIZE you might to

echo $SIZE

Also, please remember that cron usually has no pre-defined PATH or other
environmental variables. Notice how I used the full pathname to the
common commands 'ls' and 'awk'. Actually, that's a good practice to
get into when writing _any_ scripts.

Hope this helps,

Old guy
Reply With Quote