I often find myself trying to find the chmod number of a file or of a bunch of files.

But this is one of the command I cannot get myself to remember and after trying a few things, I always end up googling the answer and reading the exact same one each time.

Note that actually the stat -f "%OLp" <file> doesn't work on macOS.

The good answer both for Ubuntu and macOS is :

stat --format '%a' <file>

This time I decided to create and alias for that. If you want to do the same, simply add the following to your .bashrc or .zshrc

# alias the command
alias getchmod="stat --format '%a'"

# print ls with chmod number
function lls {
  LLS_PATH=$1
  \ls -lAFh $LLS_PATH | 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}"
}

The second alias is actually a function made to display the chmod number while calling ls -lAFh. It looks like this:

Output of

You can see that ls is preceded with a backslash \ like \ls. This is to temporarily un-alias the ls command which is aliased with --color=always somewhere else in my .zshrc.

Hope this helps! :)