Operating System - HP-UX
1752576 Members
4271 Online
108788 Solutions
New Discussion юеВ

Getting file permissions in 'NNN' mode

 
Miguel Cuesta
Advisor

Getting file permissions in 'NNN' mode

Hi!

I want to obtain a file's permission in 'NNN' mode, not
in '-rwxrwxrwx' mode.

I mean, I would like a script to get DIRECTLY '666' from
a file, not '-rw-rw-rw'.

Any help?
16 REPLIES 16
linuxfan
Honored Contributor

Re: Getting file permissions in 'NNN' mode

Hi Miquel,


I am not aware of any way to directly obtain the permissions in "octal format"

One of the ways you can do is put in a case statement in your script and convert the permissions to octal format.

Eg:
perm=`ls -al file | awk '{print $1}'

case $perm in
-rwxrwxrwx)
PERM=777 ;;
-rw-rw-rw-)
PERM=666 ;;
esac

Include all the permissions in the case statement. I am sure there is an easier way, just can't think of one right now.

-Ramesh
They think they know but don't. At least I know I don't know - Socrates
Santosh Nair_1
Honored Contributor

Re: Getting file permissions in 'NNN' mode

Miguel,

I assume you're trying to get these permissions so that you can set permissions on some other file via a script? If that's the case, you should be able to use the octal values directly, i.e. you should be able to do chmod u=rw,g=rw,o=rw file.
This could be done using cut and sed, i.e something like:

PERM="-rw-rw-rw-"
UPERM=$(echo $PERM|cut -c2-4|sed s/\-//g|sed s/s/xs/g)
GPERM=$(echo $PERM|cut -c5-7|sed s/\-//g|sed s/s/xs/g)
OPERM=$(echo $PERM|cut -c8-10|sed s/\-//g|sed s/s/xs/g)

chmod u=$UPERM,g=$GPERM,o=$OPERM file.

Hope this helps.

-Santosh
Life is what's happening while you're busy making other plans
A. Clay Stephenson
Acclaimed Contributor

Re: Getting file permissions in 'NNN' mode

Hi Miguel:

Here is a 30 second perl script to do what you want:

#!/usr/bin/perl

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

After making it executable, run it like this:
omode.pl myfile1 myfile2 myfile3
to produce this output:
myfile1 750
myfile2 400
myfile3 664

That should do it, Clay
If it ain't broke, I can fix that.
Darrell Allen
Honored Contributor

Re: Getting file permissions in 'NNN' mode

Nice script Clay. Can you show the mod for files with sticky or suid bits?

Darrell
"What, Me Worry?" - Alfred E. Neuman (Mad Magazine)
A. Clay Stephenson
Acclaimed Contributor

Re: Getting file permissions in 'NNN' mode

Hi Darrell:

Easy as pie; we just mask off 3 less bits and expand the output field to 4 octal digits:

#!/usr/bin/perl

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

That should do it with the setuid, setgid, & sticky bits displayed in the leftmost digits.

Another variation on the theme is to not mask anything and expand the octal field to reveal file type (regular, directory, fifo, ...) as well.

#!/usr/bin/perl

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

Regards, Clay
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: Getting file permissions in 'NNN' mode

Hi:

Here's a shell script to return the octal numeric mode of the file passed to it. The script handles the setuid, setgid and sticky bits along with the standard permissions:

#!/usr/bin/sh
#
typeset -i PERMFCT=0 #...permission mode factor
typeset -i MISCFCT=0 #...miscellaneous mode factor
typeset -i BITSNBR=2 #...bit number (1-relative)
typeset -i BITSVAL=0 #...simple bit value
typeset -i OCTMODE=0 #...final, octal numeric mode
#
[ "$#" != 1 ] && { echo "Usage: $0 file"; exit 1 ; }

SMODE=`ls -l $1|awk '{print $1}`
#
while (( BITSNBR <= 10 ))
do
case `expr \( $BITSNBR - 2 \) / 3` in #...establish weighted values...
0 ) PERMFCT=100
MISCFCT=4000
;;
1 ) PERMFCT=10
MISCFCT=2000
;;
2 ) PERMFCT=1
;;
esac
#
case `expr substr $SMODE $BITSNBR 1` in
r ) BITSVAL=4
MISCFCT=0
;;
w ) BITSVAL=2
MISCFCT=0
;;
x ) BITSVAL=1
MISCFCT=0
;;
s|t )
BITSVAL=1
;;
S|T )
BITSVAL=0
;;
* ) BITSVAL=0
MISCFCT=0
;;
esac
#
OCTMODE=`expr $PERMFCT \* $BITSVAL + $MISCFCT + $OCTMODE`
#
let BITSNBR=$BITSNBR+1
done
#
echo "mode = $OCTMODE"
exit 0
#
#.jrf.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: Getting file permissions in 'NNN' mode

Hi:

>>> Corrected Copy <<<
----------------------

Here's a shell script to return the octal numeric mode of the file passed to it. The script handles the setuid, setgid and sticky bits along with the standard permissions:

#!/usr/bin/sh
#
typeset -i PERMFCT=0 #...permission mode factor
typeset -i MISCFCT=0 #...miscellaneous mode factor
typeset -i BITSNBR=2 #...bit number (1-relative)
typeset -i BITSVAL=0 #...simple bit value
typeset -i OCTMODE=0 #...final, octal numeric mode
#
[ "$#" != 1 ] && { echo "Usage: $0 file"; exit 1 ; }
#
SMODE=`ls -l $1|awk '{print $1}`
#
while (( BITSNBR <= 10 ))
do
case `expr \( $BITSNBR - 2 \) / 3` in #...establish weighted values...
0 ) PERMFCT=100
MISCFCT=4000
;;
1 ) PERMFCT=10
MISCFCT=2000
;;
2 ) PERMFCT=1
MISCFCT=1000
;;
esac
#
case `expr substr $SMODE $BITSNBR 1` in
r ) BITSVAL=4
MISCFCT=0
;;
w ) BITSVAL=2
MISCFCT=0
;;
x ) BITSVAL=1
MISCFCT=0
;;
s|t )
BITSVAL=1
;;
S|T )
BITSVAL=0
;;
* ) BITSVAL=0
MISCFCT=0
;;
esac
#
OCTMODE=`expr $PERMFCT \* $BITSVAL + $MISCFCT + $OCTMODE`
#
let BITSNBR=$BITSNBR+1
done
#
echo "mode = $OCTMODE"
exit 0
#
#.jrf.

Regards!

...JRF...
Frank Slootweg
Honored Contributor

Re: Getting file permissions in 'NNN' mode

Kind of a hack and slow for large files (because the file is read completely):

echo local/bin/procmail | cpio -oB | cpio -iBtv

To get only the last three mode 'digits' (and no "... blocks" messages):

echo file | cpio -oB 2> /dev/null | cpio -iBtv 2>/dev/null | cut -c4-6

Robin Wakefield
Honored Contributor

Re: Getting file permissions in 'NNN' mode

Hi Miquel,

Here's an awk way of doing it for 000-777 modes:

#!/bin/sh

ls -l $* | awk '
{
x=0
for (i=2;i<11;i++){
n=int((10-i)/3)
if (substr($1,i,1) == "r")
x=x+10**n*4
if (substr($1,i,1) == "w")
x=x+10**n*2
if (substr($1,i,1) == "x")
x=x+10**n*1
}
print x,$NF
}'

Rgds, Robin