28

I teach an Intro to UNIX/Linux course at a local college and one of my students ask 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 verses 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?

  • Wrt your question title: such coloring has nothing to do with the ls command itself. – Drew Feb 11 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 Feb 11 at 5:23
  • 3
    @Drew You only know that after investigating the problem. Obviously ls can change the color of its output. – pipe Feb 11 at 10:50
  • @pipe: Nah, I know that from long before there was color. It need not provide color, even if recent versions of ls can provide color. ;-) – Drew Feb 11 at 14:25
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.

  • 1
    Yep, that was it. Last color change in $PS1 set bold+white (1;37) -- Thanks! – Bill R Feb 11 at 19:09
  • 3
    You can mark it accepted, then. – Thomas Dickey Feb 12 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.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.