How to List Linux File Permissions in Octal Notation

When looking at a list of filesystem objects, I have trouble visually parsing rwxr-xr-x or similar permissions. It’s probably something with my eyes, but I’d much prefer to see 755. More than just a visual preference, somehow I just “get” it faster than seeing letters and dashes.┬áSurely there must be some simple switch in ls that will do this, right?

Wrong.

However, a quick-n-dirty way of doing this is with the “stat” command using the -c switch. Stat itself will show you file or filesystem status information. The -c switch allows you to customise the output. To see file permissions in octal use the “%a” format sequence. I toss in a few other format sequences for my tastes:

stat -c "%n %a %G %g" IMG_0346.MOV
IMG_0346.MOV 664 wesley 500

The file’s name is shown as a result of %n, %a shows octal permissions, %G shows the owner’s group name and %g shows the owner’s group ID.

To see the octal permissions of the contents of an entire directory (in this case my Downloads directory) simply use a star thusly:

stat -c "%a %n" Downloads/*
 
664 Downloads/localhost.sql
664 Downloads/premium-pixels-fancy-pants-blog-magazine-theme.zip
755 Downloads/premium-pixels-package
644 Downloads/readme.html
664 Downloads/RobDuck1.JPG
664 Downloads/RobDuck2.JPG
664 Downloads/socialite-modern-wordpress-theme.zip

This isn’t my ideal, however. I’d really like ls to have the option. Perhaps there’s some bastardized and recompiled ls out there. Have you ever wanted to see octal permissions on your filesystem lists? How did you go about achieving that goal?

6 Comments

  1. Philip

    February 16, 2012 at 7:39 am

    Well, you could use an alias – something like…

    alias ‘ls’=”stat -c “%F(%a) %h %U %G %s %y %n” ”

    The only problem there is you would have to always remember to explicitly type, ‘ls *’ – rather than just ‘ls’.

    I suppose you could get round that with a function declaration like:

    function ls() {
    if [[ $# -ne 0 ]];then
    stat -c “%F(%a) %h %U %G %s %y %n” $*
    else
    stat -c “%F(%a) %h %U %G %s %y %n” *
    fi
    }

    Okay, so it’s not a flag for ls, but it’s close, right? :)

    Reply

    • Wesley David

      February 16, 2012 at 9:41 am

      I’ve been resolved that, in order to get ls to do anything near what I want, it’s going to be painful. Very painful.

      Thanks for the function! =)

      Reply

  2. Zoredache

    February 16, 2012 at 5:13 pm

    Have you looked at using find?

    find . -printf ‘%m %pn’

    Reply

    • Wesley David

      February 16, 2012 at 6:42 pm

      Oohhhh, no I hadn’t known about that. I just tested it out and it seems like an intriguing possibility. Thanks!

      Reply

  3. Garrett Nievin

    February 17, 2012 at 6:52 am

    I’ve had to do a lot of this in the past on some Dynix/ptx, Tru64, and other Unix systems, without a GNU or modern “find” command or “stat”. I wrote programs to do it. It seems like it should be easy to replicate the file metadata (ownerships, modes) from one directory to another similar directory should be simply accomplished with a few commands, but I could not find an elegant way. Modern fileutils may offer a way for which I’ve not yet looked.

    A persistent minor irritation of Unixes is what I think of as unbalanced operations, and this is one of them. It’s my contention that the input to a command, like “chmod”, should be able to be gotten as the output counterpart, like “lsmod”; similar with “chown/lsown”, “chgrp/lsgrp”, etc. I think that the “route” command, which can print out the entire routing table, should be able to similarly input an entire routing table from a file. Particularly considering the Unix philosophy of taking small tools and connecting their inputs/outputs to build more complex and task-focused tools, this has always seemed a glaring omission to me.

    Reply

    • Wesley David

      February 17, 2012 at 10:45 am

      Wow, fascinating timing because I was just about to learn how I could replicate permissions from one directory tree to another. I am now officially scared about what might be discovered. Hopefully, yes, some modern utilities will have made it something less than excruciating.

      As I’ve been learning about the *nix way, I’ve liked the philosophy of “small, connecting tools” and have agreed with the criticisms of those who think many GNU tools have been victims of feature creep. However, it does seem like the ideal hasn’t been lived out consistently.

      Reply

Leave a Reply

Follow TheNubbyAdmin!

follow us in feedly

Raw RSS Feed:

%d bloggers like this: