Permission bit translator - octal to textual and vice versa

Hi,

Does anyone know of any permission bit "translator"? One that can translate the permission bit from its textual value to its octal value and vice versa. It is alright if it is always just rwx but on a lot of occasions nowadays, getting a lots of s, S, t, etc.

-rwxrwxrwx   1 oracle   dba            0 Mar 11 13:27 x        ==> 777
-rwxr-x---   1 oracle   dba            0 Mar 11 13:27 y            ==> 750
-rw-r-----   1 oracle   dba            0 Mar 11 13:27 z            ==> 640

Sometimes I receive a request to change the octal value of the permission, i.e. do chmod 640, chmod 655 etc., and I want to know how it is supposed to look like after I've done the change so I can verify its ls -l output before and after the change. Am finidng it tedious to remember the text equivalence of the octal values ... is there a trick to that :wall:

What am wanting to do is to be able to read a list of file from a directory and then display their permission bit, in both textual and octal, ls -l will do the trick for the textual, but don't know how to do the octal bit. At the same time, also being able to run the script in such a way that when I give 777 as a parameter, it will display -rwxrwxrwx.

So for example, if the script is called checkperm.ksh, if I run it as checkperm.ksh /usr/test, it will list the files in that directory and display its permission bit, both octal and textual. If I run checkperm.ksh 750, it should display that 750 is -rwxr-x---

I found one in Google that is similar to what I wanted, but it is an online permission bit translator, not a script ... :frowning:

Any suggestion will be much appreciated. Thanks.

Text to octal has already been dicussed in this thread:

ls -l | awk 'BEGIN {
v["r1"]=400; v["w2"]=200; v["x3"]=100; v["s3"]=4100; v["S3"]=4000
v["r4"]=40 ; v["w5"]=20 ; v["x6"]=10 ; v["s6"]=2010; v["S6"]=2000
v["r7"]=4  ; v["w8"]=2  ; v["x9"]=1  ; v["t9"]=1001; v["T9"]=1000}
{val=0
 for (i=1;i<=9;i++) val=val+v[substr($0,i+1,1)i]
 printf "%4d %s\n",val,$NF}'

It's not terribly complicated. If you learn how to convert between the symbolic (textual) and octal forms, you won't need a crutch. I'm not criticizing using a tool to help you get things done, but unix permissions are a fundamental aspect of the system which isn't very difficult to learn. A little time invested in this area will probably pay great dividends. Perhaps this post by Perderabo will be of use to you: Unix File Permissions

The stat(1) command can do that on *BSD systems. I'm almost certain that gnu stat(1) can do it as well. Not sure about other platforms. It's not a standardized utility. If you decide to look into it, look for a format option.

Regards,
Alister

Great post, it can really help me.....

Here is a modified version of the above AWK-based permissions translator which I give my students.

ls -l | awk 'BEGIN {
   v["r1"] = 400; v["w2"] = 200; v["x3"] = 100; v["s3"] = 4100; v["S3"] = 4000;
   v["r4"] = 40 ; v["w5"] = 20 ; v["x6"] = 10 ; v["s6"] = 2010; v["S6"] = 2000;
   v["r7"] = 4  ; v["w8"] = 2  ; v["x9"] = 1  ; v["t9"] = 1001; v["T9"] = 1000;
}
NF <= 2 { print $0 }
NF > 2 { val = 0; xa = " ";
   for (i = 1; i <= 9; i++) val += v[substr($1, i+1, 1)i];
   if (substr($1, 11, 1) == "+") xa = "+";
   printf " %04d%c %s\n", val, xa, $0;
}'

Adds octal bits to left of regular ls -l output