Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

How do I test the permission of a file for user , group ,and all?

( #159906=categorized question: print w/ replies, xml ) Need Help??
Contributed by Zombie sherifradwan on Apr 17, 2002 at 17:23 UTC
Q&A  > files


Answer: How do I test the permission of a file for user , group ,and all?
contributed by chicks

Here's a script that shows how to use stat to get what you're looking for and an example invocation.

$ cat fileperm #!/usr/bin/perl my $filename = 'fileperm'; my $mode = (stat $filename)[2]; $mode = $mode & 07777; # mask off file type; my $usr = ($mode & 0700) >> 6; my $grp = ($mode & 0070) >> 3; my $oth = $mode & 0007; printf "$filename has permissions %04o (%i,%i,%i)\n", $mode, $usr, $gr +p, $oth; $ ./fileperm fileperm has permissions 0775 (7,7,5) $ ls -l fileperm -rwxrwxr-x 1 chicks chicks 289 Apr 21 11:32 fileperm
Answer: How do I test the permission of a file for user , group ,and all?
contributed by particle

see stat

Answer: How do I test the permission of a file for user , group ,and all?
contributed by ehdonhon

print "Testing File: $test_file\n"; my ( $mode, $uid ) = (stat( $test_file ))[2,4] or die "Stat failed.\n" +; my ( $ownername ) = getpwuid $uid; print "File is owned by '$ownername'\n"; my $ms = 'xwrxwrxwr'; my $modestring; for ( 0..8 ) { if ( $mode & ( 2 ** $_ ) ) { $modestring = substr( $ms, $_ , 1 ) . $modestring; } else { $modestring = '-' . $modestring; } } print "File permissions are $modestring\n";

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (7)
    As of 2015-07-05 20:40 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (68 votes), past polls