If I run ls I get colored output which I find pretty handy for quickly getting a glance of the kind of file. When I try to pipe it to less even with the -r and -R flags the coloring always get lost. I am using zsh version 5.0.7. Any ideas? Thanks.

edit: I am on OS X.

  • Can you confirm ls is being invoked with --color? Try suspending the pipeline (^Z) or using ps from another terminal. What output does which ls produce? – zackse Feb 18 '15 at 17:04
  • I cleared this from the close queue since the OP has clearly stated that they're using less -r so it is not an issue with less but with ls – terdon Feb 18 '15 at 18:31
  • I'm surprised that this isn't a duplicate yet, but the closest I could find was Store command output in variable, and that's on Super User. – Scott Feb 18 '15 at 20:24
  • @Scott I'm not sure if there is exact duplicate, but there some questions for which answer is very similar (ls result depends on stdout). The one I remember is unix.stackexchange.com/questions/157285/… which itself is marked as duplicate of unix.stackexchange.com/questions/10421/… – jimmij Feb 18 '15 at 22:28
  • @jimmij But those questions don't mention that ls --color=auto means use color only when writing to a terminal (i.e., when the standard output is a terminal), and not when it's a file or a pipe. – Scott Feb 18 '15 at 22:34

This is by design: programs that produce colored output typically do so only when their output goes to a terminal, not when it's sent to a pipe or to a regular file. The reason is that data sent on a terminal is presumably read by a human, whereas data piped to a program or written to a file is likely to be parsed by some program, so it shouldn't contain extraneous content like color-changing escape sequences.

GNU ls displays colored output on a terminal when you pass the option --color (or --color=auto). To force colored output regardless of the file type of the standard output, pass --color=always or --color=yes (they're synonyms). This convention has been followed by other commands, like GNU grep, FreeBSD grep, git diff, etc.

ls --colors=yes -l | less

With the FreeBSD version of ls (also found on OSX, and available as the colorls port on OpenBSD and NetBSD), pass the option -G to display colors when the output is a terminal. Set the environment CLICOLOR_FORCE to display colors regardless of the output file type.

CLICOLOR_FORCE=1 ls -l | less

The problem most probably is that your ls program has set option --color=auto which basically means that output should be coloured only if it is connected to terminal, otherwise (output connected to a pipe or a file) no colors are emitted.

If you want to have colors is such cases you should set --color option to always, so try

ls --color=always | less -R

If this behaviour is what you expect all the time then just create alias:

alias ls='ls --color=always'
  • In my setup, bash version 4.3, --color is an illegal option.. Grepping for "color" tells me about the "-G" option but doesn't say anything about an auto or always option. Any ideas? – nunos Feb 18 '15 at 21:39
  • 5
    @nunos ls doesn't come from bash, it is standalone program. Run ls --version to check its version. Mine is ls GNU coreutils) 8.21. – jimmij Feb 18 '15 at 22:13
  • @jimiij my ls version doesn't have the --version flag. I am using the default one from OS X, not sure if that helps. – nunos Feb 20 '15 at 18:59
  • @nunos please always mention your operating system. There are huge differences between Linux and non-GNU nixes like OSX. – terdon Feb 20 '15 at 19:13

I can't comment @jimmij 's answer, because I have less than 50 rep, but I would like to explain what worked for me in Bash.

If you run

$ man ls | grep color

you will see all the entries of the man-page for your specific shell environment (in our case bash).

For me the following adjustment did the trick:

$ alias ls='ls -G'
  • 3
    That worked for what? The -G flag has nothing to do with how ls manages its colored output. That just removes the groups from the output of ls -l. It does affect color output in BSD ls (the one used on OSX as well), is that what you mean? In any case, I don't think that will show color when redirected to a file, it will just show color on the command line. – terdon Feb 20 '15 at 19:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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