Operating System - HP-UX
1833883 Members
1772 Online
110063 Solutions
New Discussion

Re: Numeric versions of file permissions?

 
SOLVED
Go to solution
Greg White
Frequent Advisor

Numeric versions of file permissions?

Hi everyone,

Is there an easy way to get the numeric versions of file permissions rather than the rwxrwxrwx versions. I've done a man of ls but I can't find anything.

TIA, Greg
I know how to do it in pascal.
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Numeric versions of file permissions?

Hi Greg:

I've answered this before but it's only about a minute's worth of Perl; viz

#!/usr/bin/perl -w

use strict;

my $i = 0;
while ($i <= $#ARGV)
{
my ($dev,$ino,$mode) = stat($#ARGV[$i]);
printf("%s\t%03o\n",$#ARGV[$i],$mode & 0777);
++$i;
}

That should do it. Use it as fmode.pl file1 ...

Regards, Clay
If it ain't broke, I can fix that.
S.K. Chan
Honored Contributor

Re: Numeric versions of file permissions?

Maybe this is not quite what you what but take a look at 'getaccess' command
# man getaccess

Example:
filename = abc
permission = -rw-r--r--
owner:group = root:bin

# getaccess -n -u skchan abc
4 abc
==> the -n option gives the numerical representation of the permission with reference to user "skchan" in the above example.

Greg White
Frequent Advisor

Re: Numeric versions of file permissions?

The Perl script works great! Is it difficult to add the setuid and setgid bits?

Thanks, Greg
I know how to do it in pascal.
A. Clay Stephenson
Acclaimed Contributor

Re: Numeric versions of file permissions?

Hi again Greg:

No, it's quite trivial. We simply expand the printf by 1 octal digit and mask off 1 less nibble.

#!/usr/bin/perl -w

use strict;

my $i = 0;
while ($i <= $#ARGV)
{
my ($dev,$ino,$mode) = stat($#ARGV[$i]);
printf("%s\t%04o\n",$#ARGV[$i],$mode & 07777);
++$i;
}

P.S. This also reveals the 'sticky' bit. The 1xxx part.

Regards and learn Perl, Clay

If it ain't broke, I can fix that.