Operating System - HP-UX
1833788 Members
2173 Online
110063 Solutions
New Discussion

Re: Octal file permissions?

 
SOLVED
Go to solution
Ryan Clerk
Frequent Advisor

Octal file permissions?

Hello Gurus:

Is there an easy way to display a file's permission as separate octal digits? My ideal display would be:

filename1 6 4 0
filename2 7 1 1
for
filename1 rw-r-----
filename2 rwx--x--x

Thanks in advance,
Ryan
9 REPLIES 9
James R. Ferguson
Acclaimed Contributor

Re: Octal file permissions?

Joseph C. Denman
Honored Contributor

Re: Octal file permissions?

Check out this thread. Should do it.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x006c1cc6003bd6118fff0090279cd0f9,00.html

...jcd...
If I had only read the instructions first??
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Octal file permissions?

Hi Ryan:

This should be close:

#!/usr/bin/perl -w

use strict;

my $i = 0;
while ($i <= $#ARGV)
{
my $mode = (stat($ARGV[$i]))[2];
printf("%s\t%o\t%o\t%o\n",$ARGV[$i],
($mode & 0700) >> 6,
($mode & 070) >> 3,
$mode & 07);
++$i;
}

I'm doing this 'on the fly' so there might be a missing ')' but this should be very close.

You might also want to add a test (-e) to make sure the file exists before doing the stat.

If it ain't broke, I can fix that.
Tom Maloy
Respected Contributor

Re: Octal file permissions?

No easy way. You'll have to write some shell/perl scripts.

Tom
Carpe diem!
Joseph C. Denman
Honored Contributor

Re: Octal file permissions?

Here is another.

http://forums.itrc.hp.com/cm/QuestionAnswer/1,,0x3c76107d277ad611abdb0090277a778c,00.html

...jcd...
If I had only read the instructions first??
Ryan Clerk
Frequent Advisor

Re: Octal file permissions?

Thanks very much. It looks like everyone basically posted a link to one of Clay's scripts. Clay, would you mind showing me how to add the check to see if the file exists? I'm not very good at PERL yet but I am trying.

Thanks, Ryan
A. Clay Stephenson
Acclaimed Contributor

Re: Octal file permissions?

Hi Ryan:

Normally I don't spoonfeed you but here you go:


#!/usr/bin/perl -w

use strict;

my $i = 0;
my $cc = 0;
while ($i <= $#ARGV && $cc == 0)
{
if (-e $ARGV[$i])
{
my $mode = (stat($ARGV[$i]))[2];
printf("%s\t%o\t%o\t%o\n",$ARGV[$i],
($mode & 0700) >> 6,
($mode & 070) >> 3,
$mode & 07);
}
else
{
printf STDERR ("File '%s' not found.\n",
$ARGV[$i]);
$cc = 2;
}
++$i;
}
exit($cc);

-----------------------------------

That should do it (if there are no typo's) .

Regards, Clay


If it ain't broke, I can fix that.
Ryan Clerk
Frequent Advisor

Re: Octal file permissions?

Thanks Clay, that was perfect. You make this look so easy.

Ryan
H.Merijn Brand (procura
Honored Contributor

Re: Octal file permissions?

I'm late, I know, but this was just Toooo tempting :)

l1:/stand 113 > perl -le 'print"$_\t@{[split//,sprintf q(%03o),(stat)[2]&511]}"for@ARGV' *
bootconf 6 4 4
build 7 5 5
dlkm 7 5 5
dlkm.vmunix.prev 7 5 5
ioconfig 6 4 4
kernrel 4 4 4
lost+found 7 5 5
rootconf 6 0 0
system 4 4 4
system.d 7 5 5
system.prev 6 4 4
vmunix 7 5 5
vmunix.prev 7 5 5
l1:/stand 114 >


Beat that Clay!
Enjoy, Have FUN! H.Merijn