Find the answer to your Linux question:
Results 1 to 5 of 5
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Dec 2004
    Posts
    4

    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
    Location
    Michigan
    Posts
    1
    A long time ago I was searching for the same thing, I found a script that does it:
    Code:
    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
    Just Joined!
    Join Date
    Sep 2006
    Posts
    1

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

    Cheers,
    Mr. Milk

  4. #4
    Just Joined!
    Join Date
    Aug 2011
    Posts
    1
    Hi,

    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.

    Thanks.

  5. #5
    oz
    oz is offline
    forum.guy
    Join Date
    May 2004
    Location
    arch linux
    Posts
    18,733
    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.

    Thanks.
    oz

Posting Permissions

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