views:

420

answers:

9

On Linux, I use stat --format="%s" FILE, but Solaris I have access to doesn't have stat command. What should I use then?

I'm writing Bash scripts, and can't really install any new software on the system.

I've considered already using:

perl -e '@x=stat(shift);print $x[7]' FILE

or even:

ls -nl FILE | awk '{print $5}'

But neither of these looks sensible - running Perl just to get file size? Or running 2 commands to do the same?

+1  A: 

You first Perl example doesn't look unreasonable to me.

It's for reasons like this that I migrated from writing shell scripts (in bash/sh etc.) to writing all but the most trivial scripts in Perl. I found that I was having to launch Perl for particular requirements, and as I did that more and more, I realised that writing the scripts in Perl was probably a more powerful (in terms of the language and the wide array of libraries available via CPAN) and more efficient way to achieve what I wanted.

Note that other shell-scripting languages (e.g. python/ruby) will no doubt have similar facilities, and you may want to evaluate these for your purposes. I only discuss Perl since that's the language I use and am familiar with.

Brian Agnew
Well, I do a lot of Perl writing myself, but sometimes the tool is chosen for me, not by me :)
depesz
+1  A: 

What about du -s <file> ?

Bertrand Marron
It shows size in blocks. Which is not what I need. And Solaris du doesn't have -b option.
depesz
A: 

on linux you can use du -h $FILE, does that work on solaris too?

knittl
It doesn't show size in bytes.
depesz
A: 

If your find has the necessary capabilities:

size=$( find . -maxdepth 1 -type f -name filename -printf '%s' )
Dennis Williamson
Not really portable.
TheBonsai
solaris find doesn't have maxdepth :(
depesz
...nor a builtin printf and its directives, unfortunately.
Dennis Williamson
+8  A: 

wc -c

Will tell you the number of bytes in a file - hopefully in Solaris too.

I was worried it wouldn't work for binary files, but on my Linux box it worked OK on a .ZIP file.

Carl Smotricz
it gives corrent number of bytes, but still needs postprocessing to remove filename. I think I'll stick with `ls -l | awk` approach. But wc -c is really cool, and I totally forgot about it.
depesz
Hopefully you have `cut` at least - then you could do: `wc -l Quiz.zip | cut -d' ' -f1` .
Carl Smotricz
IMO, awk is a bit heavy for such a simple task. :)
Carl Smotricz
Or just `wc -c < file` if you don't want the filename appearing.
caf
That looks like a winner!
Carl Smotricz
+1  A: 

if you have Perl on your Solaris, then use it. Otherwise, ls with awk is your next best bet, since you don't have stat or your find is not GNU find.

A: 

Did you try du -ks | awk '{print $1*1024}'. That might just work.

Aditya
no, it doesn't. for file of 60 bytes it reports 4096.
depesz
A: 

Finally I decided to use ls, and bash array expansion:

TEMP=( $( ls -ln FILE ) )
SIZE=${TEMP[4]}

it's not really nice, but at least it does only 1 fork+execve, and it doesn't rely on secondary programming language (perl/ruby/python/whatever)

depesz
A: 

There is a trick in Solaris I have used, if you ask for the size of more than one file it returns just the total size with no names - so include an empty file like /dev/null as the second file:

eg command fileyouwant /dev/null

I can't rememebr which size command this works for ls/wc/etc - unfortunately I don't have a solaris box to test it.

Martin Beckett