Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

file modes: convert from "text" to octal 1

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL
hello,
my current solution bases on a map file (generated maybe not in "elegant" way)...

Code:
# a=0 b=0 c=0 d=0;until [ $a -eq 8 ];do until [ $b -eq 8 ]; do until [ $c -eq 8 ];do until [ $d -eq 8 ];do touch ${a}${b}${c}${d};chmod ${a}${b}${c}${d} ${a}${b}${c}${d};ls -ld ${a}${b}${c}${d}|awk '{print $1,$NF}';rm ${a}${b}${c}${d};((d+=1));done;d=0;((c+=1));done;c=0;((b+=1));done;b=0;((a+=1));done >mapa.permissionow
# wc mapa.permissionow
    4096    8192   65536 mapa.permissionow
# ls -ld /tmp
drwxrwxrwt   14 bin      bin           20480 Feb 14 16:25 /tmp
# grep ^.rwxrwxrwt mapa.permissionow|cut -d" " -f2
1777
#

and I am looking for something quicker or more natural way to check it in a UNIX directly on file, - a command which can return file modes in octal instead of .rwx..... form

regards
 
a command which can return file modes in octal
You may use the lstat function in perl.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
thx. it was helpful - result:

Code:
pperm () { perl -e '$mode = (stat("$ARGV[0]"))[2]; printf("%04o",$mode & 07777 );' $1; }

I also found another solution/script on a web:

Code:
#!/usr/bin/ksh

ls -ld $1 | sed 's/.\(.........\).*/\1/
h;y/rwsxtST-/IIIIIOOO/;x;s/..\(.\)..\(.\)..\(.\)/|\1\2\3/
y/sStTx-/IIIIOO/;G
s/\n\(.*\)/\1;OOO0OOI1OIO2OII3IOO4IOI5IIO6III7/;:k
s/|\(...\)\(.*;.*\1\(.\)\)/\3|\2/;tk
s/^0*\(..*\)|.*/\1/;q'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top