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
ls
is being invoked with--color
? Try suspending the pipeline (^Z
) or usingps
from another terminal. What output doeswhich ls
produce? – zackse Feb 18 '15 at 17:04less -r
so it is not an issue withless
but withls
– terdon Feb 18 '15 at 18:31ls
result depends on stdout). The one I remember is http://unix.stackexchange.com/questions/157285/why-does-ls-wc-l-show-the-correct-number-of-files-in-current-directory which itself is marked as duplicate of http://unix.stackexchange.com/questions/10421/output-from-ls-has-newlines-but-displays-on-a-single-line-why – jimmij Feb 18 '15 at 22:28ls --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:34which ls
outputsls: aliased to ls -G
– nunos Feb 20 '15 at 19:00CLICOLOR_FORCE
as explained in Gilles' answer. – terdon Feb 20 '15 at 19:15