LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-25-2010, 01:30 PM   #1
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Rep: Reputation: 0
BASH. How check file size ?


Hi All,

Can someoen help me with a script that will check if my log file is a certain size ? EX: I want to limit the size of my rsync log to say 5MB, if that's true I would move it and create a new one.

Thank you in advance.
 
Old 03-25-2010, 01:47 PM   #2
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
du -m <filename> | awk '{ print $1 }' -
Returns the file size in megabytes on stdout.
 
Old 03-25-2010, 01:50 PM   #3
djsmiley2k
Member
 
Registered: Feb 2005
Location: Coventry, UK
Distribution: Home: Gentoo x86/amd64, Debian ppc. Work: Ubuntu, SuSe, CentOS
Posts: 343
Blog Entries: 1

Rep: Reputation: 72
log rotate can be made to do this for you, along with compressing and archiving old logs - I couldn't find the "offical" site as I got mine from gentoo but - http://www.linuxconfig.org/logrotate explains it.

Or are you doing this to learn? If so then I'm afraid all I know is
Code:
du /path/to.file
will give you the size of the file (Normally in Kb I think but it can be changed, for example `du -h` gives you a nice human readable size).


(This is my first helpful post, Hope its ok!).
 
Old 03-26-2010, 04:56 AM   #4
PMP
Member
 
Registered: Apr 2009
Location: ~
Distribution: RHEL, Fedora
Posts: 381

Rep: Reputation: 58
stat <filename>

look for "man stat" for more options
 
Old 03-26-2010, 07:22 AM   #5
rbalaa
Member
 
Registered: Mar 2010
Posts: 61

Original Poster
Rep: Reputation: 0
Thanks. I used stat -c %s "filename" , it worked perfectly.

FILESIZE=$(stat -c %s "filename")
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Basic bash script question re: file size or # of lines in a file the_fornicator Programming 6 09-03-2009 09:41 AM
shell script - how to check file size noir911 Programming 3 01-24-2009 09:17 AM
How to write crontab to check file size and all file in a directory? modpriest Linux - Newbie 1 02-14-2008 06:48 AM
perl- check size to see if file is complete michael.barnes Programming 2 02-13-2008 06:20 PM
Need script to check file size. _TK_ Linux - General 1 08-06-2001 08:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:45 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration