Get file size in c shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get file size in c shell script?
# 1  
Old 09-24-2005
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  
Old 09-25-2005
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  
Old 09-25-2005
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  
Old 09-26-2005
MySQL

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  
Old 09-26-2005
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  
Old 09-26-2005
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  
Old 09-26-2005
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

By pass a process in a Shell Script on file size

I wish to by pass a process if the file is over a certain size? not sure this makes sense current bit of the script below #if we are bypAssing the OCR if ; then echo Bypassing HOTFOLDER OCR HOT_FOLDER_DIR=$BATCH_POST_OCR_DIR; potential change below? would this work would I need... (1 Reply)
Discussion started by: worky
1 Replies

2. Shell Programming and Scripting

Shell script to report file size, pid and also kill the process

Hi All, Looking for a quick LINUX shell script which can continuously monitors the flle size, report the process which is creating a file greater than certain limit and also kill that process. Can someone please help me on this? (4 Replies)
Discussion started by: vasavimacherla
4 Replies

3. Shell Programming and Scripting

shell script for getting the file size

Hi can some one please help me how i can get the output i require: My text file "sample.txt" contains the text like below Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_fedora-lv_root 15G 2.6G 12G 19% /hari Filesystem Size ... (3 Replies)
Discussion started by: harimhkr
3 Replies

4. Shell Programming and Scripting

How to remove a file in shell script if its size exceeds limit?

How can i remove a file using shell script when its size exceeds 10MB. Given that file is located in different location to the shell script where it is running? (4 Replies)
Discussion started by: vel4ever
4 Replies

5. Shell Programming and Scripting

Script to read file size and send email only if size > 0.

Hi Experts, I have a script like $ORACLE_HOME/bin/sqlplus username/password # << ENDSQL set pagesize 0 trim on feedback off verify off echo off newp none timing off set serveroutput on set heading off spool Schemaerrtmp.txt select ' TIMESTAMP COMPUTER NAME ... (5 Replies)
Discussion started by: welldone
5 Replies

6. Shell Programming and Scripting

alias to a shell script - tablespace size

I have developed the following shell script. The idea was to create an alias to execute it and return the tablespaces and their sizes from an oracle database. When I execute is via the alias, I have issues. So, here is the script first: > cat ts_size.sh #!/bin/ksh xwhere=" " xwild=" "... (8 Replies)
Discussion started by: desibabu
8 Replies

7. Shell Programming and Scripting

shell script to check file size greater than 50M

Hi All, OS:AIX 64 bits using korn shell. Requirement: shell script to check file size greater than 50M and send mail alert. Thanks for your time! Regards, (3 Replies)
Discussion started by: a1_win
3 Replies

8. Shell Programming and Scripting

Shell script to FTP if file size is 0KB

Hi all, I was a UNIX shell script that will check for the file size of each file and it should be re directed to a text file say filesize.txt And if there are more than 10 0KB files then FTP should not happend and whenever there are less than 10 0KB files it should FTP the files. Kindly help me... (1 Reply)
Discussion started by: Codesearcher
1 Replies

9. Shell Programming and Scripting

Shell script to Find file size

Hi, I am writing a script which takes the input file name and concat as a new file by appending a "1" to the file name. However i am not able to get the size of this new file. I am not sure where i am going wrong. Please check the script and help me get this working. #!/bin/sh ... (1 Reply)
Discussion started by: ragsnovel
1 Replies

10. Shell Programming and Scripting

Size of an array in sh shell script

Is there a way to find out the size of an array in sh shell script? Thanks. (1 Reply)
Discussion started by: trivektor
1 Replies
Login or Register to Ask a Question