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.
if (size +10c filename)
I used this to search for files larger than 10kb, but it has errors when I run it.
how u feel this
is going to work?
To my understanding, you need to get the file size by something like:
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
size=`ls -l $filename | awk '{print $5}'`
size=`stat -c %s $filename`
or much better
size=$(stat -c %s $filename)
vino
Read the man pages of find.
-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.
find /dir/to/search -size +10k
will give you all files above 10kb.
size will split up the data as follows
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
-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
----------------------
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