I'm writing a script to audit updates to files in a set of production unix directories (until we can implement a proper change control process).

I understand that mode from stat() contains file type and permission. As per the user manual, I extract the permissions using %o and a mask (see example below).

Question #1:
How can I extract the file type from mode ?
The mode (5 digit decimal) converts to a 6 digit octal.
Using the mask, returns the permissions (4 digits octal) (see example below).

Question #2:
Is there an easier way to audit changes to files ?
Are there existing functions or scripts to do this ?
(i.e. am I reinventing the wheel ?)

My approach is as follows:
I'll run a nightly cron job which will identify file adds and removes by comparision to previous nights results. I'll identify modifies and changes via mtime and ctime. I'll store all add/remove/update/changes in an audit file which will contain all the info in stat().

Thanks in advance for any help.

Joe

Example: ======= bshell6.0 > cat test.pl #!/opt/perl64/bin/perl my $file = $ARGV[0]; my $mode = (stat($file))[2]; print "File: $file\n"; print "mode = $mode\n"; printf "mode (octal) = %o\n", $mode; printf "Permissions = %04o\n", $mode & 07777; print "=============================\n"; bshell6.0 > ls -ldn testfile testdir drwxr-x--- 2 643 125 96 May 26 14:24 testdir -rwxr-x--- 1 643 125 0 May 26 14:24 testfile bshell6.0 > test.pl testfile File: testfile mode = 33256 mode (octal) = 100750 Permissions = 0750 ============================= bshell6.0 > test.pl testdir File: testdir mode = 16872 mode (octal) = 40750 Permissions = 0750 =============================

In reply to Extract file type from stat() mode by joe_s

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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, details, 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, summary, 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.