48

Is there a way to get files/directories permissions in number format "0777" instead of "-rw--r--r" format?

0

3 Answers 3

60

You can display the octal permissions for a file using the stat command:

stat -c %a [filename]

Or by using find:

find [path] -printf '%m %p\n'

Note that find is recursive, and will print all files in all subdirectories as well. You can use options like maxdepth or prune to stop it from recursing.

0
16

I know this is an old post but I found it while looking for a solution to this, and expanded upon it:

stat -c '%a - %n'

That will show the file permissions and the file name. This allows you to see the permissions of every file in a folder with:

stat -c '%a - %n' *

I also took this a step further and made an alias:

alias perms="stat -c '%a - %n'"

So typing perms * will give me the permissions of every file or perms file.php will give me the permissions of just that one file.

For users who find this while looking for a solution for OSX:

Versions of OSX after 10.10 no longer have a version of stat that accepts the -c parameter. If you get an error about "illegal option -- c" then you should be able to use this stat command:

stat -f "%A -  %N" *

This can also be aliased like the previous command I shared:

alias perms="stat -f '%A -  %N'"
3
  • This is great. Any way to make it default to showing all w/out having to add an asterisk? Sort of a ternary or something, where doing perms is the same as perms * unless user entered something like perms <file or folder>? Mar 16, 2023 at 20:58
  • @J.ScottElblein you might have to make a bash script for that.
    – rmmoul
    Mar 17, 2023 at 0:03
  • 1
    Ended up trying to create a ternary myself; got it 99% working, then ended up asking the linux gurus (in case ya wanna use it, too): stackoverflow.com/q/75772498/553663 Mar 18, 2023 at 3:48
8

You can also use this workaround:

find FILENAME/DIRECROY -printf "%m:%f\n"

Example check my Videos directory:

find Videos -printf "%m:%f\n"

755:Videos

Another Method:

Used to list all directory files with their permissions

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

Not the answer you're looking for? Browse other questions tagged .