LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 01-19-2006, 12:27 PM   #1
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Rep: Reputation: 30
Bash script that returns the size of a file


how can I make a script that echoes the size of a file in human-readable format?
the file is $1 (the first parameter)

Thank you!
 
Old 01-19-2006, 12:34 PM   #2
haertig
Senior Member
 
Registered: Nov 2004
Distribution: Debian, Ubuntu, LinuxMint, Slackware, SysrescueCD
Posts: 1,979

Rep: Reputation: 290Reputation: 290Reputation: 290
You want either the ls or wc command.

"wc filename" will tell you the number of lines, words, and characters. If you only want one of these attributes you can use "wc -l", "wc -w", or "wc -c".

"ls -l filename" will tell you the number of characters, along with other stuff.
 
Old 01-19-2006, 12:46 PM   #3
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Original Poster
Rep: Reputation: 30
yes wc -b filename returns me the size in bytes and the name of the file.
but for a dir? how can i get the size (include all its subfolders) ?
 
Old 01-19-2006, 12:50 PM   #4
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
Use 'du' if you want to know the size of a directory.

-twantrd
 
Old 01-19-2006, 12:54 PM   #5
XaViaR
Member
 
Registered: Dec 2004
Distribution: RHEL, CentOS, SuSE
Posts: 170

Rep: Reputation: 30
use the following command.

du -sh
 
Old 01-19-2006, 02:10 PM   #6
trickykid
Guru
 
Registered: Jan 2001
Posts: 24,133

Rep: Reputation: 197Reputation: 197
Quote:
Originally Posted by XaViaR
use the following command.

du -sh
du -sh <directory>

No script needed, it's already a built in command.. and if you just wanted to make it so there's no options to be passed, create an alias for 'du -sh' so it would be:

$ aliasname <directory>
 
Old 01-19-2006, 03:24 PM   #7
spank
Member
 
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278

Original Poster
Rep: Reputation: 30
solved that! thank you!

how can i print the first parameter using a variable. fo example i have x=1 and i want to print $1, x=2 -> $2 etc!
 
  


Reply


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete files with 0k file size in a directory justgiver Linux - Newbie 4 01-28-2008 04:56 AM
File Size Shell Script BlackLabel Programming 7 11-27-2007 07:48 PM
newbie: how to see the size of file under bash? sirpelidor Linux - Newbie 3 05-19-2004 07:23 PM
Help with file size script jpc82 Programming 10 04-01-2004 04:55 AM
Need script to check file size. _TK_ Linux - General 1 08-06-2001 08:30 AM


All times are GMT -5. The time now is 10:36 AM.

Main Menu
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