best way to get a file's size?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting best way to get a file's size?
# 1  
Old 03-21-2004
best way to get a file's size?

I'm trying to get a file's size.
Specifically, I want to check whether a file is larger than 650MB.
I've tried:

find . -name "filename" -size +681574400 -print

Oddly, that didn't seem to work.
(NB: 1024 bytes x 1024 x 650 = 681574400 bytes = 650 MB)

The problem seems to be the "-size" primary and it's numerical argument "+681574400".

The man pages say:
Quote:
All primaries which take a numeric argument allow the number to be pre-
ceded by a plus sign (``+'') or a minus sign (``-''). A preceding plus
sign means ``more than n'', a preceding minus sign means ``less than n''
and neither means ``exactly n''.
So I don't understand why it doesn't work.
I have since found that the problem can also be solved by getting the file size with ls -l and awk, but I think that's to AWKward (horrible pun Smilie.

Seriously, I've got these questions:

- Why does the above not work and what needs to be done for it to work?

- What do people think is the best and safest way of doing such a file size check in a script?

NB: the shabang is #!/bin/sh -- I could obviously change that to anther shell/language, but I want to remain as compatible as possible.

Many thanks in advance!
# 2  
Old 03-21-2004
............

you could check the filesize inside a script with the "du" command, grep for the value

pressy
# 3  
Old 03-21-2004


try this

find . -name "filename*" -size +650c -exec ll {} \ ; 2> /dev/null

-size +650c (specifies file size in bytes, no space after integer value and the letter 'c')
Quote:
Why does the above not work and what needs to be done for it to work?
The default unit of measure the find command uses is blocks (512 bytes per block). You need to use the "c" parm to allow find to use bytes
Quote:
What do people think is the best and safest way of doing such a file size check in a script?
I could be wrong, but I think the answer is more a matter of preference. It also depends on what you are trying to do. You will probably get many differing answers to this question.
Quote:
NB: the shabang is #!/bin/sh -- I could obviously change that to anther shell/language, but I want to remain as compatible as possible.
Try using the Korn shell (ksh) (if available as it offers many more features than Bourne (sh) )

Last edited by google; 03-21-2004 at 08:56 PM..
# 4  
Old 03-21-2004
Thanks both!

I actually decided to use wc rather than du (or find) to be sure to get the actual size of the file (as opposed to the space that file is taking up on disk).

This is what I currently have and it SEEMS to work:

Quote:
if [ $# == 1 ] && [ -r "$1" ] && [ -f "$1" ] && [ `wc -c "$1" | awk '{print $1}'` -gt 681574400 ]; then
# I should add a check whether the current dir is writable
echo there is only one parameter AND it\'s writable AND it\'s an ordinary file AND it\'s bigger than 650MB
# (...)
fi
I'd still be interested to hear qualified opinions which way might be considered best practice.

Again, MANY thanks! Smilie
# 5  
Old 03-21-2004
Quote:
[ $# == 1 ] && [ -r "$1" ] && [ -f "$1" ] && [ `wc -c "$1" | awk '{print $1}'` -gt 681574400 ]; then
Check the bold portion of the script above. This should probably read [ $# -eq 1 ] And you should break up your logic into smaller more discrete steps! <in my opinion Smilie >
# 6  
Old 03-21-2004
Thanks google!!! Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Ls directory size reporting byte size instead of file count

I have been searching both on Unix.com and Google and have not been able to find the answer to my question. I think it is partly because I can't come up with the right search terms. Recently, my virtual server switched storage devices and I think the problem may be related to that change.... (2 Replies)
Discussion started by: jmgibby
2 Replies

2. Programming

[c] How to calculate size of the file from size of the buffer?

Hi, Can I find size of the file from size of the buffer written? nbECRITS = fwrite(strstr(data->buffer, ";") + 1, sizeof(char), (data->buffsize) - LEN_NOM_FIC, fic_sortie); Thank You :) (1 Reply)
Discussion started by: ezee
1 Replies

3. Shell Programming and Scripting

Optimised way for search & replace a value on one line in a very huge file (File Size is 24 GB).

Hi Experts, I had to edit (a particular value) in header line of a very huge file so for that i wanted to search & replace a particular value on a file which was of 24 GB in Size. I managed to do it but it took long time to complete. Can anyone please tell me how can we do it in a optimised... (7 Replies)
Discussion started by: manishkomar007
7 Replies

4. 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

5. Shell Programming and Scripting

File size and dir size

How to use 'df' only to get the 'Available' space for a specific dir, and then compare with a specific file size using stat -c %s file.txt to see if the file actually can be copied into the dir. Is there any quick way to see if a file can fit into a dir? (4 Replies)
Discussion started by: Emilywu
4 Replies

6. Shell Programming and Scripting

The scripts not able to make the file to size 0, every times it go back to its original size

#!/bin/sh ########################################################################################################## #This script is being used for AOK application for cleaning up the .out files and zip it under logs directory. # IBM # Created #For pdocap201/pdoca202 .out files for AOK #1.... (0 Replies)
Discussion started by: mridul10_crj
0 Replies

7. Solaris

Directory size larger than file system size?

Hi, We currently have an Oracle database running and it is creating lots of processes in the /proc directory that are 1000M in size. The size of the /proc directory is now reading 26T. How can this be if the root file system is only 13GB? I have seen this before we an Oracle temp file... (6 Replies)
Discussion started by: sparcman
6 Replies

8. Shell Programming and Scripting

find with file size and show the size

Hi All... is the below command be modified in sucha way that i can get the file size along with the name and path of the file the below command only gives me the file location which are more than 100000k...but I want the exact size of the file also.. find / -name "*.*" -size +100000k ... (3 Replies)
Discussion started by: rpraharaj84
3 Replies

9. Shell Programming and Scripting

file size comparision local file and remote file

Hi, I have written a script which would FTP a dump file to the FTP server and log the whole activity into a file. to confirm the success of the file copy i grep for "226 file receive OK" and then send out an email saying success. Now i want to make sure the bytes of the local file and... (4 Replies)
Discussion started by: dba.admin2008
4 Replies

10. Solaris

command to find out total size of a specific file size (spread over the server)

hi all, in my server there are some specific application files which are spread through out the server... these are spread in folders..sub-folders..chid folders... please help me, how can i find the total size of these specific files in the server... (3 Replies)
Discussion started by: abhinov
3 Replies
Login or Register to Ask a Question