Find the answer to your Linux question:
Results 1 to 5 of 5
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    A command that shows me the permissions of a file in octal ?

    Hi all,

    Does anyone know a shell command to show me the permissions of a file in octal form ?
    'ls -l' will not do, as it returns a string in the format -rwxr-xr-x. I need the octal form for that, which is 755.

    Actually, I am writing a Java program to monitor some files in the hard drive, and I intend to have it call this command to know the permissions of the file, and I need it in octal form. If anyone knows any way to get the number from inside Java code instead of making a call to the system, it would be welcome as well...

  2. #2
    Just Joined!
    Join Date
    Jan 2005
    A long time ago I was searching for the same thing, I found a script that does it:
    ls|while read i; do echo -ne "$i\t"; stat "$i"|sed  -n -e 4p|cut -f2 -d" "|cut -b 2-5;done|awk '{print $2,$1}'

  3. #3

    Full ls with octal+symbolic permissions

    For those who want to have "0755" style permissions with ls I made this script:

    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}'

    Just type the line above to get the full ls listing with octal + the drwxr-xr-x style.

    If you want to make it permanent, just paste this line into ~/.bashrc file (and use "l" insted of ls):

    alias l="ls -la --color | 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}'"

    Mr. Milk

  4. $spacer_open
  5. #4

    I know this is very old topic. But the solution given above failed when there is no permission given to a file. i.e no read, write, execute permissions. Its showing ---------- instead of 000.


  6. #5
    Join Date
    May 2004
    arch linux
    Hello and welcome!

    This thread is going on 7 years old, so I'm going to lock it down, but feel free to start a fresh thread of your own if you are having any issues with Linux.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts