LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Bash script that returns the size of a file (https://www.linuxquestions.org/questions/linux-general-1/bash-script-that-returns-the-size-of-a-file-405436/)

spank 01-19-2006 12:27 PM

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!

haertig 01-19-2006 12:34 PM

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.

spank 01-19-2006 12:46 PM

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) ?

twantrd 01-19-2006 12:50 PM

Use 'du' if you want to know the size of a directory.

-twantrd

XaViaR 01-19-2006 12:54 PM

use the following command.

du -sh

trickykid 01-19-2006 02:10 PM

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>

spank 01-19-2006 03:24 PM

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 05:43 PM.