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. ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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 |
|
|||
|
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 |
|
|||
|
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. |
|
|||
|
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. |
![]() |
| Thread Tools | |
| Display Modes | |
|
|