Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

File/Dir Attributes

by onegative (Scribe)
on Jun 21, 2005 at 18:40 UTC ( [id://468779]=perlquestion: print w/replies, xml ) Need Help??

onegative has asked for the wisdom of the Perl Monks concerning the following question:

Please forgive my ignorance O' Great Monks, But is there a function or Module that will give me directory attributes such as owner,UID/GID/ect.. without having to issue system commands to extract the information instead of the below code??? Many thanks O' Magnificant Ones
#!/usr/bin/perl -w #pass a directory to script use strict; my $ownergroup=`ls -ld $ARGV[0]\| awk '{print \$3":"\$4}'`; print $ownergroup;

Replies are listed 'Best First'.
Re: File/Dir Attributes
by gellyfish (Monsignor) on Jun 21, 2005 at 18:53 UTC

    Yep, you want to look at the stat builtin function:

    my $filename = $ARGV[0]; + my ( $dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime, $ctime, $blksize, $blocks ) = stat($filename); + print "UID: $uid\nGID: $gid\n";

    /J\

      Very KEWL!!! Thanks a ton, that just made it so much easier to do what I am wanting to do. Thanks again gellyfish, You ROCK!

        I showed all of the values returned from stat for didactic purposes - if you just want the UID and GID you would be better to use an array slice:

        my ($uid,$gid) = (stat($filename))[4,5];

        /J\

      Hi

      Is there anything which equals to Unix 'file' command?

      Thanks,
      ShekarKCB
Re: File/Dir Attributes
by Xaositect (Friar) on Jun 21, 2005 at 18:56 UTC

    The stat() builtin function is what you're looking for. You may also be interested in the -X type functions which test specific bits of the stat info.


    Xaositect - Whitepages.com

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://468779]
Approved by PerlingTheUK
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (2)
As of 2024-04-25 20:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found