Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We have various of machines here with all sorts of hardware and Operating Systems, most of them do regular tasks with bash that an agent executes. However it came to my attention that some Solaris machines we own do not have stat util, and adding it would be a problem that might take time (ironic)

Meanwhile I was trying to imitate one sisyphean task which stat did in a magnificent way: return the file permissions in Octal

I found some old example which prints the regular permissions in Octal - rwx, but not for the "special" permissions - sticky group, sticky user, etc

My basic method was this : first calculate the special bits Octal value and then add the regular 'rwx', but it doesn't seem to work well

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

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

can anyone hint me what would be a good solution ?

share|improve this question
Could install GNU coreutils on your Solaris systems for a quick fix. – Kyle Smith Jan 29 '12 at 15:37
Does it have to be awk? Perl has a stat() function which does exactly what you want. – James O'Gorman Jan 29 '12 at 15:37
Unfortunately for now yes, I do know about the perl (perl -e '(stat("file"))[2]' ) solution but I have to fiddle with the most basic utilities for now, which are bash , awk, grep, adding other utilities will take time (permissions, biocracy, configs, deploy, etc) . – dotdot Jan 29 '12 at 16:01
up vote 1 down vote accepted

This should handle any set of permissions including sticky, setuid and setgid ("s" stands for "set" rather than "sticky" which is "t"). Leading zeros are printed.

ls -lahd "$file" | awk '{k = 0; for (g=2; g>=0; g--) for (p=2; p>=0; p--) {c = substr($1, 10 - (g * 3 + p), 1); if (c ~ /[sS]/) k += g * 02000; else if (c ~ /[tT]/) k += 01000; if (c ~ /[rwxts]/) k += 8^g * 2^p} if (k) printf("%05o ", k)}'

Here it is on multiple lines for readability:

awk '{k = 0; 
      for (g=2; g>=0; g--) 
          for (p=2; p>=0; p--) {
              c = substr($1, 10 - (g * 3 + p), 1); 
              if (c ~ /[sS]/) 
                  k += g * 02000; 
                  if (c ~ /[tT]/) 
                      k += 01000; 
              if (c ~ /[rwxts]/) 
                  k += 8^g * 2^p
      if (k) {
          printf("%05o ", k); 


$ touch foo
$ chmod 7654 foo
$ ls -l foo
-rwSr-sr-T 1 user user 0 2012-01-29 13:21 foo
$ ls -l foo | awk '...'
share|improve this answer
this is exactly what I was looking for. Thanks ! – dotdot Jan 30 '12 at 13:05

You can the following command to get the octal permissions of a file/folder:

$ find /tmp/ -printf "%m,%p\n"

This will print the list of files/folders under /tmp preceded with the permissions in octal. This also prints the special permissions like s. This is just an example. You can customize it according to your needs.

share|improve this answer
It seems like our find utility doesn't have that sort of option in Sun :/ – dotdot Jan 29 '12 at 16:09
Yeah, GNU find has all the cool options... – Kyle Smith Jan 29 '12 at 16:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.