1761329 Members
2977 Online
108901 Solutions
New Discussion юеВ

Numeric file modes

 
SOLVED
Go to solution
Kris Spander
Advisor

Numeric file modes

Hello,

A friend suggested that I ask you guys. Is there an easy way to get a file's permission in numeric form rather than rw-rw-rw-?
I am trying to use a case inside a script and the symbolic forms are such a pain. Anybody know an easy method?

Thanks, Kris
Dilbert is my hero.
8 REPLIES 8
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Numeric file modes

Hi Kris:

Can you say 'Perl'?

Try this 60-second quickie:

#!/usr/bin/perl -w

use strict;

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

That should do it. We are taking a slice (the 3rd element) from the stat function.

For each argument on the command line, you get a filename and the octal permissions.

If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Numeric file modes

Using perl-

perl -e '$file= ; chomp $file; @x=stat $file ; printf "%4o\n",$x[2]'

Run this and enter in the filename and it will display in octal the permissions.

I wrote this so it could be used as a filter program.

-- Rod Hills
There be dragons...
harry d brown jr
Honored Contributor

Re: Numeric file modes

#!/usr/bin/perl
use File::Find;
{
$filename = $ARGV[0];
finddepth(\&wanted,"$filename");
}
sub wanted {
my ($dev,$ino,$mode,$nlink,$uid,$gid);
if ((($f_dev,$f_ino,$f_mode,$f_nlink,$f_uid,$f_gid,$f_rdev,$f_size,$f_at
ime,$f_mtime,$f_ctime,$f_blksize,$f_blocks) = lstat($_))) {
$f_filetype = substr(sprintf("%6.6o",$f_mode),0,2);
$f_filemode = substr(sprintf("%6.6o",$f_mode),2,4);
printf("%d:%s:%s:%d:%d:%d:%d:%d:%d:%d:%d:%d:%s\n",$f_ino,$f_filetype,
$f_filemode,$f_uid,$f_gid,$f_rdev,$f_size,$f_atime,$f_mtime,$f_ctime,$f_blksize,
$f_blocks,$File::Find::name);
}
}


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

+mode +wanted


live free or die
harry
Live Free or Die
Kris Spander
Advisor

Re: Numeric file modes

That was quick. I got 3 answers in about 10 minutes!!

I used Clay's answer because it displayed just what I needed. Rodney's solution displays 6 digits and I only wanted the last 3 digits. Harry's was more than I needed.

Thanks very much, Kris
Dilbert is my hero.
Martin Johnson
Honored Contributor

Re: Numeric file modes

Show how grateful you are by assigning points!


:-)
Marty
Kris Spander
Advisor

Re: Numeric file modes

Hello again,

I need help with a small change. I would really like this to print separate digits for "owner","group", and "other". If I do this command "perms user1.dat", the script displays "user1.dat 664". I would like it to display "user1.dat 6 6 4". I know I could do this with cut but I would rather do this directly in the perms perl program because I need to process thousands of files. This is probably easy for a perl programmer but I can't seem to get it to work. Please help.

Thanks, Kris
Dilbert is my hero.
A. Clay Stephenson
Acclaimed Contributor

Re: Numeric file modes

Hi Kris:

I assume that you are probably doing something like this in the shell in some sort of loop:

echo "${FNAME}" | perms.pl read FILENAME OWNER GROUP OTHER

You would like for me to make life easier for you. Well, it is simple:

Change:
printf("%s\t%03o\n",$ARGV[$i],$mode & 0777);
To:
printf("%s\t%o\t%o\t%o\n",
$ARGV[$i],($mode & 0700) >> 6,
($mode & 070) >> 3,($mode & 07));

The >> 6 bit-shifts the operand 6 bits (2 octal digits) to the right. I could have divided by 0100 but the bit-shift is more elegant, don't you think?

If I haven't made a typo, that should fix you. I left as separators but you could replace them with spaces if you like.

P.S. O'reilly has some excellent Perl books. Look for the ones with camels on the cover.

Regards, Clay


If it ain't broke, I can fix that.
Rodney Hills
Honored Contributor

Re: Numeric file modes

change Clay's

printf("%s\t%03o\n",$ARGV[$i],$mode & 0777);

to

@x=split(//,sprintf("%03",$mode & 0777));
printf "%s\t%s\n",$ARGV[$i],join(" ",@x);

-- Rod Hills
There be dragons...