The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can i print the output of the shell script in bigger size mail2sant Shell Programming and Scripting 2 04-14-2008 05:07 AM
how to increase the size of the word in shell script mail2sant Shell Programming and Scripting 3 04-07-2008 12:23 PM
shell script to find files by date and size dadadc UNIX for Dummies Questions & Answers 1 10-20-2007 06:18 AM
Shell script to Find file size ragsnovel Shell Programming and Scripting 1 08-10-2007 11:01 AM
Size of an array in sh shell script trivektor Shell Programming and Scripting 1 09-29-2006 12:01 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-24-2005
Dado Dado is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 2
Get file size in c shell script?

Hi,
I want to use an 'if statement' that will check if a certian file is greater in size than a certain value given by the user, but cannot get it to work. Do you have any ideas how this can be done?
Your help is appreciated!
  #2 (permalink)  
Old 09-25-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by Dado
Hi,
I want to use an 'if statement' that will check if a certian file is greater in size than a certain value given by the user, but cannot get it to work. Do you have any ideas how this can be done?
Your help is appreciated!
Cannot get it to work ? Post the script that you have.
  #3 (permalink)  
Old 09-25-2005
Dado Dado is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 2
Here is what I used

if (size +10c filename)
I used this to search for files larger than 10kb, but it has errors when I run it.
  #4 (permalink)  
Old 09-26-2005
RishiPahuja's Avatar
RishiPahuja RishiPahuja is offline
Registered User
  
 

Join Date: Apr 2005
Location: Bangalore, India
Posts: 203
Thumbs up

how u feel this
Quote:
if (size +10c filename)
is going to work?

To my understanding, you need to get the file size by something like:


Code:
size=`ls -l $filename | awk '{print $5}'`
allowed_size=`expr ($size + 10240)/1024`
if ( $allowed_size > something) then
....

regards,
rishi
  #5 (permalink)  
Old 09-26-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by RishiPahuja
how u feel this

is going to work?

To my understanding, you need to get the file size by something like:


Code:
size=`ls -l $filename | awk '{print $5}'`
allowed_size=`expr ($size + 10240)/1024`
if ( $allowed_size > something) then
....

regards,
rishi
If you have stat, then an improvement for


Code:
size=`ls -l $filename | awk '{print $5}'`


Code:
size=`stat -c %s $filename`

or much better


Code:
size=$(stat -c %s $filename)

vino
  #6 (permalink)  
Old 09-26-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,798
Quote:
Originally Posted by Dado
if (size +10c filename)
I used this to search for files larger than 10kb, but it has errors when I run it.
Read the man pages of find.


Code:
       -size n[bckw]
              File uses n units of space.  The units are  512-byte  blocks  by
              default  or  if b follows n, bytes if c follows n, kilobytes
              if k follows n, or 2-byte words if w follows  n.   The  size
              does  not  count  indirect  blocks,  but it does count blocks in
              sparse files that are not actually allocated.


Code:
find /dir/to/search -size +10k

will give you all files above 10kb.

size will split up the data as follows


Code:
   text    data     bss     dec     hex filename
 341349   10772      16  352137   55f89 file.ext

We can see that 341349+10772+16 does give 352137. But doing an ls -l file.ext gives

Code:
-rwxrwx---    1 xxxxx  xxxxx    505977 Sep 22 22:46 file.ext

which gives a file size of 505977.

Hence size will not give you the actual size of the file.

Look at this article and this man page. It explains what size takes into consideration.

vino

Last edited by vino; 09-26-2005 at 05:45 AM..
  #7 (permalink)  
Old 09-26-2005
vinodyelikar vinodyelikar is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 1
Filesize in if

Quote:
Originally Posted by Dado
Hi,
I want to use an 'if statement' that will check if a certian file is greater in size than a certain value given by the user, but cannot get it to work. Do you have any ideas how this can be done?
Your help is appreciated!
----------------------
Hi,
We can use following command to find out size of file and assign it to variable and then we can use it in if statment for compare

ab=`ls -l $1 | awk '{print $5}'`

Where $1 is file name for which to find out size.

Regards
Vinod
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:35 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0