Operating System - HP-UX
1752270 Members
4910 Online
108786 Solutions
New Discussion юеВ

Converting Permissions into Octal

 
Michael Steele_2
Honored Contributor

Converting Permissions into Octal

-rw-r--r-- 644
-rw-rw-rw- 666
-rw------- 600, etc.

anyone have a command or script to make this conversion for a bunch of files?
Support Fatherhood - Stop Family Law
4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: Converting Permissions into Octal

Hi:

You could this Perl script to list octal permissions and filenames:

# cat ./showperms
#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
my @dir = @ARGV ? @ARGV : ('.');
find(
sub {
if ( -f $_ ) {
my $mode = ( stat(_) )[2];
printf "%04o %-s\n", $mode & 0777, $File::Find::name;
}
},
@dir
);
1;

...to do things like:

# ./showperms /etc /sbin /usr/local/bin

If you don't pass any argument, the current working directory is assumed.

If you simply wish to obtain the octal permissions for a list of files you could use:

# perl -le 'printf "%04o %s\n",((stat($_))[2])&0777,$_ for @ARGV'

...Pass one or more directory and/or filenames as arguments, as for example:

# perl -le 'printf "%04o %s\n",((stat($_))[2])&0777,$_ for @ARGV' dir1/f1 dir2/f2
0770 dir1/f1
0644 dir2/f2

Regards!

...JRF...
Tim Nelson
Honored Contributor

Re: Converting Permissions into Octal

What is the end purpose ?

just curious...
Dennis Handly
Acclaimed Contributor

Re: Converting Permissions into Octal

I have a script that will convert -rw-r--r-- to the symbolic form: u=rw,g=r,o=r

http://h30499.www3.hp.com/t5/General/sed-command-giving-an-error/m-p/4165891#M134085

It shouldn't be that hard to convert to the obsolescent octal form.

Pete Randall
Outstanding Contributor

Re: Converting Permissions into Octal

Years ago I found a script that would do this. The script seems to be authored by Jack C. Mahaffey, so I can't claim any credit for it other than finding it and passing it along. The script is attached.


Pete

Pete