LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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



Reply
 
Search this Thread
Old 02-02-2006, 04:17 AM   #1
cb951303
Member
 
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Rep: Reputation: 30
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.
Old 02-02-2006, 04:38 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
Code:
#!/bin/bash
FILENAME=/home/heiko/dummy/packages.txt
FILESIZE=$(stat -c%s "$FILENAME")
echo "Size of $FILENAME = $FILESIZE bytes."
 
3 members found this post helpful.
Old 02-02-2006, 04:41 AM   #3
cb951303
Member
 
Registered: Jan 2004
Distribution: Slackware 11 + Dropline Gnome 2.16
Posts: 194

Original Poster
Rep: Reputation: 30
thanks very much
 
Old 02-02-2006, 04:59 AM   #4
bogoda
Member
 
Registered: Jan 2006
Posts: 32
Blog Entries: 2

Rep: Reputation: 15
Hi cb.....,

U can use DF command. Please use manual man df ,info df for more.
 
0 members found this post helpful.
Old 02-02-2006, 05:26 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
dummy one,

Code:
ls -l filename  |awk -F" "'{ print $5 }'
 
Old 02-03-2006, 12:52 PM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,314

Rep: Reputation: 175Reputation: 175
try:
du file
du -h file
du -k file
 
Old 01-29-2007, 12:23 AM   #7
Aries97
LQ Newbie
 
Registered: Jan 2007
Posts: 2

Rep: Reputation: 0
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
 
Old 01-29-2007, 06:08 AM   #8
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,236

Rep: Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545Reputation: 545
cat <filename> | wc -c

jlinkels
 
Old 12-13-2007, 07:26 PM   #9
tomines
LQ Newbie
 
Registered: Oct 2007
Posts: 2

Rep: Reputation: 1
another way to skin the cat


ls -l $FILENAME | cut -d " " -f 6
 
Old 12-14-2007, 11:34 AM   #10
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
'ls' is NOT reliable for telling file sizes. Use 'stat' it is most reliable.
 
1 members found this post helpful.
Old 12-17-2007, 05:35 PM   #11
tomines
LQ Newbie
 
Registered: Oct 2007
Posts: 2

Rep: Reputation: 1
Can you please explain why these other methods are not reliable?
 
Old 12-18-2007, 04:25 AM   #12
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,775

Rep: Reputation: 481Reputation: 481Reputation: 481Reputation: 481Reputation: 481
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.
Old 08-07-2010, 07:49 AM   #13
frenchn00b
Senior Member
 
Registered: Jun 2007
Location: E.U., Mountains :-)
Distribution: Debian, Etch, the greatest
Posts: 2,546

Rep: Reputation: 51
Quote:
Originally Posted by tomines View Post
Can you please explain why these other methods are not reliable?
I confirm , this would be interesting to know ...
 
Old 08-07-2010, 08:45 PM   #14
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 16

Rep: Reputation: 233Reputation: 233Reputation: 233
Quote:
Originally Posted by H_TeXMeX_H View Post
'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
 
Old 08-11-2010, 11:48 PM   #15
Aries97
LQ Newbie
 
Registered: Jan 2007
Posts: 2

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


Reply

Tags
awk, bash, filesize, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File Size Shell Script BlackLabel Programming 7 11-27-2007 08:48 PM
Bash script that returns the size of a file spank Linux - General 6 01-19-2006 04:24 PM
newbie: how to see the size of file under bash? sirpelidor Linux - Newbie 3 05-19-2004 08:23 PM
Help with file size script jpc82 Programming 10 04-01-2004 05:55 AM
Need script to check file size. _TK_ Linux - General 1 08-06-2001 09:30 AM


All times are GMT -5. The time now is 11:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration