Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

How do I get all the permission flags on a file?

by arturo (Vicar)
on Sep 25, 2000 at 23:04 UTC ( #33944=categorized question: print w/replies, xml ) Need Help??
Contributed by arturo on Sep 25, 2000 at 23:04 UTC
Q&A  > files


I want to take $file1 and give it the same permissions as $file2, which may be owned by another user. Perl's stat() function returns the mode, but it's not obvious how to get from the 5-digit decimal number returned to octal values (executing oct($mode) on it doesn't give a sensible value). So, how do you do the conversion?

Answer: How do I get all the permission flags on a file?
contributed by merlyn

You don't need octal, ever. You need a number. That number from stat can be handed over directly. For safety, you may want to bit-and it with 07777, but I don't think there'll be any nonsense anyway.

Answer: How do I get all the permission flags on a file?
contributed by BastardOperator

If merlyn's answer confuses you at all, try this:

sub return_mode { my $mode = shift; my @modelist = (($mode & 7000)>>9, ($mode & 0700)>>6, ($mode & 007 +0)>>3, ($mode &0007)); return join('', @modelist); }
Also, I don't know if you would be concerned with ACL's at all, I'm not sure how you would handle those if you were, just wanted to give you a heads up depending on how detailed you're being.

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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?

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

    How do I use this? | Other CB clients
    Other Users?
    Others musing on the Monastery: (4)
    As of 2020-10-20 20:27 GMT
    Find Nodes?
      Voting Booth?
      My favourite web site is:

      Results (210 votes). Check out past polls.