Thursday 27 November 2008

Octal permissions from a shell

I recently had the chance to resurrect a script from my HP-UX days which listed not only the usual human-readable drwx type permissions, but the octal permissions as well. Very useful when you're trying to teach someone about UNIX file permissions.

Here's a simple one liner that could be set as an alias or incorporated in a script. Personally I have mine created as a script called lsj, as shown below:

ls -l | awk '{k=0;for(i=0;i<=8;i++)k+=((substr($1,i+2,1)~/[rwx]/)*2^(8-i));if(k)printf("%0o ",k);print}'

An absolute gem to be honest and very useful. Works fine on Ubuntu (Intrepid Ibex - korn shell).

Looking to get it working so it will display all 4 octal values i.e. SETUID and SETGID, plus the sticky bit. More later.

No comments: