9

consider the output of this ls -l command:

$ ls -l /usr/bin | tail
-rwxr-xr-x 1 root root     105696 Oct 25  2014 zenity
-rwxr-xr-x 1 root root     188296 Oct 21  2013 zip
-rwxr-xr-x 1 root root      86096 Oct 21  2013 zipcloak
-rwxr-xr-x 1 root root      48459 Mar  3  2015 zipdetails
-rwxr-xr-x 1 root root       2953 Oct 29 10:45 zipgrep
-rwxr-xr-x 2 root root     166584 Oct 29 10:45 zipinfo

the permissions are listed in the nice, human readable characters. Is there a way to get ls to output the numerical equivalent to those permissions?

j0h
  • 14,825

5 Answers5

11

ls does not have such flag, but stat command allows showing octal permissions. Consider sample output for a test file,

testdir:$ stat TESTER
  File: ‘TESTER’
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 801h/2049d  Inode: 1197        Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/ xieerqi)   Gid: ( 1000/ xieerqi)
Access: 2015-11-05 17:42:01.914917433 -0700
Modify: 2015-11-05 17:42:01.914917433 -0700
Change: 2015-11-05 18:41:04.463776180 -0700
 Birth: -

stat also has --format flag , which allows you to "simulate" ls -l :

testdir:$ stat --format="%a %h %U %G %s %y %n" * | head -n 3                   
664 1 xieerqi xieerqi 0 2015-11-05 17:42:01.918917452 -0700 Aaaaaaa.bbb - 0000003 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42:01.922917471 -0700 Aaaaaaa.bbb - 0000004 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42:01.930917509 -0700 Aaaaaaa.bbb - 0000005 tag tag_tag 9tag

The only limitation here is the time format cannot be altered and color output cannot be added .

You could always alias that command in .bashrc to some shorter command, e.g. alias lsl2='stat --format="%a %h %U %G %s %y %n" *'

Alternatively, I've put together a small function that uses find and awk for nicer formatting

 function lsl2
   {
     find . -maxdepth 1 | sort |  xargs -I{} stat --format="%a %h %U %G %s %y %n" {}  | awk '{$7=substr($7,1,5);$8    =" ";print}'
  }

Sample output

testdir:$ lsl2 | head -n 7
775 3 xieerqi xieerqi 4096 2015-11-05 22:20   .
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000003 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000004 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000005 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000006 tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000006 tag tag_tag 9tag
664 1 xieerqi xieerqi 0 2015-11-05 17:42   ./Aaaaaaa.bbb - 0000007 tag 9tag

testdir:$ type lsl2
lsl2 is a function
Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • 2
    Uh.. find can print the permissions, and most of these values (if not all): find . -printf "%m %n %u %g %s %t %f\n" | column -t – muru Nov 10 '15 at 15:45
  • 1
    @muru well I didn't know that . . . .-_- find has way too many options . . . Write an answer ! You're gonna have my upvote – Sergiy Kolodyazhnyy Nov 10 '15 at 15:47
4

According to https://stackoverflow.com/questions/1795976/can-the-unix-list-command-ls-output-numerical-chmod-permissions it is not possible.

An answer to that question, however proposes a way to 'almost' achieve that:

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

You could make that an alias for easy use - but ls alone can not do this.

Tobias
  • 1,608
2

Here's a fun pipeline

paste <(printf "%04d\n" $(stat -c '%a' *)) <(ls -l | sed 1d) | 
  sed -r 's/([[:digit:]]+)\t(.)........./\2\1/' 
  • printf "%04d\n" $(stat -c '%a' *) -- prints the zero-padded octal access rights for each file
  • ls -l | sed 1d -- the long listing, minus the first line "total 12345"
  • paste <(...) <(...) -- takes one line from each process substitution, and joins them with a tab
  • sed -r 's/([[:digit:]]+)\t(.)........./\2\1/' -- replaces the rwxrwxrwx human readable permissions with the octal value.
glenn jackman
  • 17,900
1

Take a look at GNU find:

find -printf "%y %m %n %u %g %Ac %f\n" -maxdepth 1

See: man find

Cyrus
  • 5,554
0

Hmm... found this is in searching for something totally else,

One more to the collection:

ls -lh \
  | sed -re 's/rwx/7/g' \
         -e 's/rw-/6/g' \
         -e 's/r-x/5/g' \
         -e 's/r--/4/g' \
         -e 's/-wx/3/g' \
         -e 's/-w-/2/g' \
         -e 's/--x/1/g' \
         -e 's/---/0/g'
Hannu
  • 5,374
  • 1
  • 23
  • 40