Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re: Perl one liner and stat function

by ikegami (Patriarch)
on Mar 23, 2009 at 22:59 UTC ( [id://752744]=note: print w/replies, xml ) Need Help??

in reply to Perl one liner and stat function

stat only returns an object when File::stat is used.
perl -MFile::stat -le'$pv=stat(shift); print "$pv->uid,$pv->gid,$pv->m +ode"' /dir/filename

But it would be shorter to use the indexes:

perl -le'$,=","; print((stat shift)[4,5,2])' /dir/filename

It makes more sense to display the permissions in octal:

perl -e'printf "%s,%s,%04o\n", (stat shift)[4,5,2]' /dir/filename

Finally, to process multiple arguments at once:

perl -e'printf "%s:%s,%s,%04o\n", $_, (stat)[4,5,2] for @ARGV' file1 f +ile2

Update: Oops, the first one won't work because the method calls are in a string literal.

Replies are listed 'Best First'.
Re^2: Perl one liner and stat function
by csarid (Sexton) on Mar 24, 2009 at 00:47 UTC
    Thanks for the help ikegami -- for all the variations on the solution. It's amazing how many different ways one can approach this once you have such a great handle on the deep inner workings of the language... not sure how you "PerlMonks" do it.. it's been tough for me.
      By example :) perldoc -f stat
      use File::stat; $sb = stat($filename); printf "File is %s, size is %s, perm %04o, mtime %s\n", $filename, $sb->size, $sb->mode & 07777, scalar localtime $sb->mtime;

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://752744]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-05-30 07:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found