Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
02-02-2006, 03:17 AM
#1
Member
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194
Rep:
File size using BASH script
How can I get the size of a file using bash scrpt? thanks.
Click here to see the post LQ members have rated as the most helpful post in this thread.
02-02-2006, 03:38 AM
#2
Senior Member
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,524
Rep:
Code:
#!/bin/bash
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."
2 members found this post helpful.
02-02-2006, 03:41 AM
#3
Member
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194
Original Poster
Rep:
thanks very much
02-02-2006, 03:59 AM
#4
Member
Registered: Jan 2006
Posts: 32
Rep:
Hi cb.....,
U can use DF command. Please use manual man df ,info df for more.
0 members found this post helpful.
02-02-2006, 04:26 AM
#5
Member
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383
Rep:
dummy one,
Code:
ls -l filename |awk -F" "'{ print $5 }'
02-03-2006, 11:52 AM
#6
Senior Member
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Puppy
Posts: 3,048
Rep:
try:
du file
du -h file
du -k file
01-28-2007, 11:23 PM
#7
LQ Newbie
Registered: Jan 2007
Posts: 2
Rep:
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
01-29-2007, 05:08 AM
#8
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Etch/Lenny/Squeeze
Posts: 3,485
cat <filename> | wc -c
jlinkels
12-13-2007, 06:26 PM
#9
LQ Newbie
Registered: Oct 2007
Posts: 2
Rep:
another way to skin the cat
ls -l $FILENAME | cut -d " " -f 6
12-14-2007, 10:34 AM
#10
Guru
Registered: Oct 2005
Posts: 11,388
'ls' is NOT reliable for telling file sizes. Use 'stat' it is most reliable.
1 members found this post helpful.
12-17-2007, 04:35 PM
#11
LQ Newbie
Registered: Oct 2007
Posts: 2
Rep:
Can you please explain why these other methods are not reliable?
12-18-2007, 03:25 AM
#12
Amigo developer
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,434
Not all programs determine the file size in the same way. Some methods simply don't give an accurate result.
0 members found this post helpful.
08-07-2010, 06:49 AM
#13
Senior Member
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,545
Rep:
Quote:
Originally Posted by
tomines
Can you please explain why these other methods are not reliable?
I confirm , this would be interesting to know ...
08-07-2010, 07:45 PM
#14
Senior Member
Registered: Oct 2005
Location: Philippines
Distribution: Gentoo, Slackware, LFS
Posts: 1,526
Rep:
Quote:
Originally Posted by
H_TeXMeX_H
'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
08-11-2010, 10:48 PM
#15
LQ Newbie
Registered: Jan 2007
Posts: 2
Rep:
Ooh! Yes, using stat is much better than what I came up with. Thank you!
Thread Tools
Search 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
HTML code is Off
All times are GMT -5. The time now is 04:30 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News