Operating System - HP-UX
1835673 Members
2476 Online
110082 Solutions
New Discussion

Re: Numeric file permissions?

 
SOLVED
Go to solution
Kris Spander
Advisor

Numeric file permissions?

Hello,

Is there an easy way to display the permissions of a file in numeric format like "775"?

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

Re: Numeric file permissions?

Hi Kris:

Probably about the easiest method is to use Perl's stat() function.

Try this:

fmode.pl myfile1 myfile2
to output
myfile1 775
myfile2 600

I already had this one written.

Regards, Clay
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Numeric file permissions?

Hi Kris,

I tried to write a script in shell just for sake of it and ofcourse for my fun. I made it bit eloborate so that I wouldn't get confused myself while writing it. This lists the permissions in four digit format. For ex., 4555 or 0555 so that you would know if the file has suid/sgid/sticky bits.

Call it get_perms.ksh and run it with

---start---

#!/usr/bin/ksh
if [ $# -ne 1 ]
then
echo "Usage: $0 file"
exit
fi

PERM=$(ll $1|sed 's/-/0/g'| awk '{print $1}')

A=0

B=$(echo $PERM|cut -c 4)

if [[ "$B" = "s" || "$B" = "S" ]]
then
(( A = $A + 4 ))
fi

C=$(echo $PERM|cut -c 7)

if [[ "$C" = "s" || "$C" = "S" ]]
then
(( A = $A + 2 ))
fi
D=$(echo $PERM|cut -c 10)

if [[ "$C" = "t" || "$C" = "T" ]]
then
(( A = $A + 1 ))
fi

PERM=$(echo $PERM|sed -e 's/r/4/g' -e 's/w/2/g' -e 's/x/1/g' -e 's/s/1/g' -e 's/
t/1/g' -e 's/S/0/g' -e 's/T/0/g')



printf "$A"

printit()
{
c=0
K=$1
for i in $K
do
VAL=$(echo $PERM|cut -c $i)
((c = $c + $VAL ))
done
printf "$c"
}

printit "2 3 4"
printit "5 6 7"
printit "8 9 10 "

printf " $1\n"

---End--

-Sri
PS: Becareful while cutting pasting the sed statement. It should be in one line.
You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor

Re: Numeric file permissions?

Hi Kris:

No argument that perl is shorter/faster here, but here's another shell solution I offered sometime ago when this question was asked:

The script returns the octal numeric mode of the file passed to it. The script handles the setuid, setgid and sticky bits in addition to 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

Regards!

...JRF...
Kris Spander
Advisor

Re: Numeric file permissions?

Hello,

I decided to use A. Clay's method. It was was much easier.

Thanks to everybody,
Kris
Dilbert is my hero.