File Size In A Bash Script.

This is a discussion on File Size In A Bash Script. within the Linux Administration forums, part of the Linux Forums category; I would like to run a small script from Cron, to monitor certain file size and then take an action. ...


Go Back   Usenet Forums > Linux Forums > Linux Administration

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 07-18-2004
Adam
 
Posts: n/a
Default File Size In A Bash Script.

I would like to run a small script from Cron, to monitor certain file
size and then take an action.

Something like

$Filename=
$Filepath=

$size=

If $size= {certain size}

Action here.

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

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

Thank you.

Reply With Quote
  #2 (permalink)  
Old 07-18-2004
Bit Twister
 
Posts: n/a
Default Re: File Size In A Bash Script.

On Sun, 18 Jul 2004 15:00:10 -0700, Adam wrote:
> I would like to run a small script from Cron, to monitor certain file
> size and then take an action.
>
> Something like
>
> $Filename=
> $Filepath=
>
> $size=
>
> If $size= {certain size}
>
> Action here.
>
> I have no idea how to capture a file size in bash, any suggestions?


stat -c %s /etc/profile

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


All sorts of ways, for instance

set $(ls -s /etc/profile)
echo size is $1 name is $2

You do need to take time to play around here
http://www.tldp.org/LDP/abs/html/index.html
Reply With Quote
  #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
  #4 (permalink)  
Old 07-20-2004
Adam
 
Posts: n/a
Default Re: File Size In A Bash Script.

In article <slrncfokl7.qdn.ibuprofin@atlantis.phx.az.us>
ibuprofin@painkiller.example.com (Moe Trin) wrote:

>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,


It did, more than I have ever expected.

Thank you very much.

Reply With Quote
  #5 (permalink)  
Old 07-20-2004
Adam
 
Posts: n/a
Default Re: File Size In A Bash Script.

In article <slrncflt97.ff3.BitTwister@wb.home.invalid>
Bit Twister <BitTwister@localhost.localdomain> wrote:

>On Sun, 18 Jul 2004 15:00:10 -0700, Adam wrote:
>> I would like to run a small script from Cron, to monitor certain file
>> size and then take an action.
>>
>> Something like
>>
>> $Filename=
>> $Filepath=
>>
>> $size=
>>
>> If $size= {certain size}
>>
>> Action here.
>>
>> I have no idea how to capture a file size in bash, any suggestions?

>
>stat -c %s /etc/profile
>
>> I read somewhere that we can use ls -s, but how to capture that in the
>> script I have no idea.

>
>All sorts of ways, for instance
>
>set $(ls -s /etc/profile)
>echo size is $1 name is $2
>
>You do need to take time to play around here
>http://www.tldp.org/LDP/abs/html/index.html


Thank you for the pointers.


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 01:48 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0