UNIX – List Files With Permissions In CHMOD Numbers

| | | | | | | |

If you’re on UNIX you know ls will list files in a directory but typically you’ll get something like

$ ls
dns  index.html  info.php
$ ls -la
total 20
drwxr-xr-x. 3 webadmin www-data 4096 Jan 24 11:41 .
drwxr-xr-x. 4 webadmin www-data 4096 Jan 24 08:13 ..
drw-r--r--. 2 webadmin www-data 4096 Jan 24 09:31 dns
-rw-r--r--. 1 webadmin www-data 2199 Jan 24 08:14 index.html
-rw-r--r--. 1 webadmin www-data   19 Jan 24 10:25 info.php

You can simily stat the file(s) with the below

# On Mac
stat -f "%OLp" <file>

# On Ubuntu (or most Linux/Unix servers)
stat --format '%a' <file>

# On Busybox
stat -c '%a' <file>

What if we work with stat and make it easier?

# UNIX
sudo cp /etc/bashrc /etc/bashrc.original
echo "alias lsc='stat -c \"%a %A \$s %U:%G %y %n\" *'" | sudo tee -a /etc/bashrc
# MAC OS X
#alias lsc='stat -f "%A %t %Su:%Sg %t %Sm %t %N" *'
lsc
$ lsc
644 drw-r--r--  webadmin:www-data 2017-01-24 09:31:15.077301144 -0500 dns
644 -rw-r--r--  webadmin:www-data 2017-01-24 08:14:03.161307507 -0500 index.html
644 -rw-r--r--  webadmin:www-data 2017-01-24 10:25:42.844728348 -0500 info.php
Originally Posted on January 24, 2017
Last Updated on April 13, 2020
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.