28

I teach an Intro to UNIX/Linux course at a local college and one of my students asked the following question:

Why are some of the files in my directory colored white and others are gray? Are the white ones the ones I created today and the gray are existing files?

As I looked into this I first thought the answer would be in the LS_COLORS variable, but further investigation revealed that the color listings were different when using the -l switch versus the -al switch with the ls command. See the following screen shots:

using ls -l the file named '3' shows as white

using the -al switch the same file shows a gray

Using ls -l the file named '3' shows as white but using the -al switch the same file shows a gray.

Is this a bug in ls or does anyone know why this is happening?

4
  • Wrt your question title: such coloring has nothing to do with the ls command itself.
    – Drew
    CommentedFeb 11, 2019 at 2:32
  • FWIW, the closest you can get to this by messing with LS_COLORS is LS_COLORS='rs=0;1' ls -l, but it looks very different.
    – wjandrea
    CommentedFeb 11, 2019 at 5:23
  • 4
    @Drew You only know that after investigating the problem. Obviously ls can change the color of its output.
    – pipe
    CommentedFeb 11, 2019 at 10:50
  • @pipe: Nah, I know that from long before there was color. It need not provide color, even if recent versions of lscan provide color. ;-)
    – Drew
    CommentedFeb 11, 2019 at 14:25

2 Answers 2

68

It looks as if your prompt-string ($PS1) is setting the bold attribute on characters to make the colors nicer, and not unsetting it. The output from ls doesn't know about this, and does unset bold. So after the first color output of ls, everything looks dimmer.

2
  • 1
    Yep, that was it. Last color change in $PS1 set bold+white (1;37) -- Thanks!
    – Bill R
    CommentedFeb 11, 2019 at 19:09
  • 3
    You can mark it accepted, then.CommentedFeb 12, 2019 at 0:13
0

The whole output of ls will be printed in the last active color. If ls is called without color:

$ printf '\e[0;31m color test\n'; /bin/ls color test filea fileb filec filed filee filef fileg fileh 

will print the list of files in red.

Or, if there is no color change needed for ls, the last color will remain:

$ mkdir t1; cd t1; touch file{a..h} $ printf '\e[0;31m color test\n'; /bin/ls --color -l color test total 0 -rw-r--r-- 1 user user 0 Feb 23 01:16 filea -rw-r--r-- 1 user user 0 Feb 23 01:16 fileb -rw-r--r-- 1 user user 0 Feb 23 01:16 filec -rw-r--r-- 1 user user 0 Feb 23 01:16 filed -rw-r--r-- 1 user user 0 Feb 23 01:16 filee -rw-r--r-- 1 user user 0 Feb 23 01:16 filef -rw-r--r-- 1 user user 0 Feb 23 01:16 fileg -rw-r--r-- 1 user user 0 Feb 23 01:16 fileh 

Still, all in red.

But, as soon as ls needs to set a color (and then reset colors to the default used by the console), the color used from then on will be the console default.

$ printf '\e[0;31m color test\n'; /bin/ls --color -la color test total 8 drwxr-xr-x 2 user user 4096 Feb 23 01:16 . drwxr-x--- 7 user user 4096 Feb 23 01:15 .. -rw-r--r-- 1 user user 0 Feb 23 01:16 filea -rw-r--r-- 1 user user 0 Feb 23 01:16 fileb -rw-r--r-- 1 user user 0 Feb 23 01:16 filec -rw-r--r-- 1 user user 0 Feb 23 01:16 filed -rw-r--r-- 1 user user 0 Feb 23 01:16 filee -rw-r--r-- 1 user user 0 Feb 23 01:16 filef -rw-r--r-- 1 user user 0 Feb 23 01:16 fileg -rw-r--r-- 1 user user 0 Feb 23 01:16 fileh 

With the first three lines (up to the blue dot) printed in red.

    You must log in to answer this question.

    Start asking to get answers

    Find the answer to your question by asking.

    Ask question

    Explore related questions

    See similar questions with these tags.