1

I created an alias to display permissions in octet format:

alias lso="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
}'

I also set added this code to display ls in a colored format:

export CLICOLOR=1
export LSCOLORS=GxFxCxDxBxegedabagaced

What I am wondering, is how to make my alias display in a colored format as well? Also, if anyone could explain the syntax of the alias (after the pipe)? I copied the text and am trying to learn to be able to synthesize commands like it.

4

The default ls provided with OSX comes from BSD and won't allow you to do what you want.

Using CoreUtils ls (installed with macport it's available with the gls command)

You'll get your colors using --color=always:

ls -l --color=always | 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
}'

About the awk part :

  • ls outputs file permissions as 9 characters. That's why there's a loop.
  • the substr returns at each step one of the char (incremented index, 1 char-length substring)
  • ~/[rwx] converts the char to 1 if it's a r or a w or a x
  • the operation behind just does the operation the get the decimal result
  • print("%0o ", k) just prints the octal result of the decimal value calculated before

You must log in to answer this question.

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