LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   File size using BASH script (http://www.linuxquestions.org/questions/programming-9/file-size-using-bash-script-410766/)

cb951303 02-02-2006 03:17 AM

File size using BASH script
 
How can I get the size of a file using bash scrpt? thanks.

Hko 02-02-2006 03:38 AM

Code:

#!/bin/bash
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."


cb951303 02-02-2006 03:41 AM

thanks very much

bogoda 02-02-2006 03:59 AM

Hi cb.....,

U can use DF command. Please use manual man df ,info df for more.

kshkid 02-02-2006 04:26 AM

dummy one,

Code:

ls -l filename  |awk -F" "'{ print $5 }'

bigearsbilly 02-03-2006 11:52 AM

try:
du file
du -h file
du -k file

Aries97 01-28-2007 11:23 PM

Get File Size In Bash Script Using DU and SED
 
Another option:

Code:

X=~/The_File
size=$(du -b ${X} | sed 's/\([0-9]*\)\(.*\)/\1/')

- Aries

jlinkels 01-29-2007 05:08 AM

cat <filename> | wc -c

jlinkels

tomines 12-13-2007 06:26 PM

another way to skin the cat


ls -l $FILENAME | cut -d " " -f 6

H_TeXMeX_H 12-14-2007 10:34 AM

'ls' is NOT reliable for telling file sizes. Use 'stat' it is most reliable.

tomines 12-17-2007 04:35 PM

Can you please explain why these other methods are not reliable?

gnashley 12-18-2007 03:25 AM

Not all programs determine the file size in the same way. Some methods simply don't give an accurate result.

frenchn00b 08-07-2010 06:49 AM

Quote:

Originally Posted by tomines (Post 2993903)
Can you please explain why these other methods are not reliable?

I confirm , this would be interesting to know ...

konsolebox 08-07-2010 07:45 PM

Quote:

Originally Posted by H_TeXMeX_H (Post 2990638)
'ls' is NOT reliable for telling file sizes. Use 'stat' it is most reliable.

I'll agree with H_TeXMeX_H. Using stat is the best method:
Code:

stat --printf="%s" file.any

Aries97 08-11-2010 10:48 PM

Ooh! Yes, using stat is much better than what I came up with. Thank you!


All times are GMT -5. The time now is 05:59 AM.