7

Is there a way to control the color output of ls to highlight specific permissions on files? (e.g. write permissions).

enter image description here

Any options in LS_COLORS that would do this? (the image above is from Dired+ on Emacs 24.1, not from the output of ls)

5 Answers 5

6

You can always put ls on stereoids to achieve the coloring effect of file permissions.

1
3

I've just tried it, and this is actually possible with a little Ruby program called acoc (Arbitrary Command Output Colourer). It can be used to intercept output from any terminal program (colourised output in top and gcc is particularly neat) and colourise them according to a file of user-specified regular expression rules. It is a bit of work to set up, and you'll have to define your rules, but this is about as flexible as it gets. I was able to successfully add a rule to colourise world-writable files.

A couple of caveats to be aware of if you go down this route:

  1. This project doesn't seem to be in any distribution - you'll have to install manually from the homepage.
  2. You need to patch the code on the project homepage to work with modern versions of Ruby. You can find the patch here (I've contacted the author to let him know of the patch, perhaps he'll update his version).
  3. In the case of ls, intercepting with acoc throws away the existing LS_COLORS, so you'll have to provide all the rules you need as regular expressions, including things that used to be highlighted with LS_COLORS.
1

If dircolors --print-database lists all possible options, there's no way to highlight permissions in long output.

You can still use grep:

ls -l | grep --color -P "(?<=[r-])w"

It will highlight all ws in the permissions part; it will also highlight ws following rs and -s in the file name, alas.

1
  • Looks great. I wonder if there is a way to update this so that it only matches the first hit on a white-space splitting of each line. Wouldn't that do the trick? Sep 21, 2012 at 2:11
1

Don't know if this is what you are looking for. Some time ago I wrote a script that works as "ls" but shows some nice colors for permissions in the output. You can read about it here, download it here. I keep it in /usr/local/bin. If you're a debian|ubuntu based distro user, replace #!/bin/sh to #!/bin/bash in the script;) Ask, if you have questions.

0

https://altbox.dev/ also mentions lsd and exa among ls++ (see user13742's answer) as a replacement for ls to accomplish this task. Both are written in Rust and offer pre-compiled binaries. I have not tried them myself yet, I'm just researching the tool chain of a course instructor.

You must log in to answer this question.

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