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 |
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
01-19-2006, 01:27 PM
|
#1
|
Member
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278
Rep:
|
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!
|
|
|
01-19-2006, 01:34 PM
|
#2
|
Senior Member
Registered: Nov 2004
Distribution: Mint, MX, antiX, SystemRescue
Posts: 2,337
|
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.
|
|
|
01-19-2006, 01:46 PM
|
#3
|
Member
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278
Original Poster
Rep:
|
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) ?
|
|
|
01-19-2006, 01:50 PM
|
#4
|
Senior Member
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,440
Rep:
|
Use 'du' if you want to know the size of a directory.
-twantrd
|
|
|
01-19-2006, 01:54 PM
|
#5
|
Member
Registered: Dec 2004
Distribution: RHEL, CentOS, SuSE
Posts: 170
Rep:
|
use the following command.
du -sh
|
|
|
01-19-2006, 03:10 PM
|
#6
|
LQ Guru
Registered: Jan 2001
Posts: 24,149
|
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>
|
|
|
01-19-2006, 04:24 PM
|
#7
|
Member
Registered: Aug 2003
Location: Romania
Distribution: Ubuntu 6.06
Posts: 278
Original Poster
Rep:
|
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!
|
|
|
All times are GMT -5. The time now is 11:55 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
|
|