It is normally nice to have color output from ls
, grep
, etc. But when you don't want it (such as in a script where you're piping the results to another command) is there a switch that can turn it off? ls -G
turns it on (with some BSD-derived versions of ls
) if it's not the default, but ls +G
does not turn it off. Is there anything else that will?
10 Answers
Color output for ls
is typically enabled through an alias in most distros nowadays.
$ alias ls
alias ls='ls --color=auto'
You can always disable an alias temporarily by prefixing it with a backslash.
$ \ls
Doing the above will short circuit the alias just for this one invocation. You can use it any time you want to disable any alias.
-
6You could also use
command ls
, of course, but this is a little faster. Jan 1, 2014 at 19:45 -
-
3
With GNU ls
, you can specify ls --color=never
to explicitly disable color output. (Even if you have an alias ls='ls --color=auto'
, when you run ls --color=never
, it will expand to ls --color=auto --color=never
, and the later option takes precedence.)
-
1This is really the answer I was looking for, but I'll keep slm's answer as the chosen one since it handles cases of non-GNU ls, like BSD-derived versions (OS X, Solaris, and of course *BSD). Jan 1, 2014 at 16:42
alias ls=ls
or
unalias ls
This disables permanently the colorings.
-
3Of course this is a bit like killing a fly with a shotgun: you lose all other options you'd added to
ls
aliases, and you lose them for the whole session, instead of just a single command. Aug 5, 2015 at 22:50 -
You can also simply use (as on DOS):
dir
It will show the results without color, you can add the arguments same to ls
, like -l
.
In many Linux distributions this is turned on by default in the users .bashrc
.
Edit ~/.bashrc
and remove the line that looks like:
alias ls='ls --color=auto'
If you wish to disable this feature for all new accounts generated on this machine in the future, remove the same line from:
/etc/skel/.bashrc
To Turn Off the color: unalias ls
To Turn On the color: alias ls='ls --color=auto'
To Temporarily disable the color: \ls -ltr
On Centos7 this is being set in /etc/profile.d/colorls.sh
... last 3 lines of this script are:
alias ll='ls -l --color=auto' 2>/dev/null
alias l.='ls -d .* --color=auto' 2>/dev/null
alias ls='ls --color=auto' 2>/dev/null
If you have the desire and the permissions ... commenting out the undesired line should do the trick.
There might be something else going on. ls
shouldn't use colors unless it thinks it's connected to something interactive. It shouldn't colorize things when you pipe to another program.
An alias is generally a bad idea because you can never get rid of that option and you become accustomed to personal settings that you might not be able to carry with you to some other machine you have to work on. If you need to turn it off to pipe it to something else, use the switch in that pipe. But, colors shouldn't be there in a pipe.
For ls
, see if yours supports the LS_COLORS
environment variable. The trapd00r/LS_COLORS shows you how tht works. dircolors lets you adjust the colors minutely and per file type. If you don't set LS_COLORS
but have coloring turned on, ls
will make a bunch of system calls to figure out the file types and attributes so it can figure out what to color things. The people behind Sherlock found that setting LS_COLORS
can avoid all that and give a 40x improvement in speed. Read their gory details to see what they found.
I make all the file types use the default colors:
$ export LS_COLORS='bd=0:ca=0:cd=0:di=0:do=0:ex=0:pi=0:fi=0:ln=0:mh=0:no=0:or=0:ow=0:sg=0:su=0:so=0:st=0:tw=0:'
Here's the LS_COLORS file I fed to dircolors:
BLK 0
CAPABILITY 0
CHR 0
DIR 0
DOOR 0
EXEC 0
FIFO 0
FILE 0
LINK 0
MULTIHARDLINK 0
NORMAL 0
ORPHAN 0
OTHER_WRITABLE 0
SETGID 0
SETUID 0
SOCK 0
STICKY 0
STICKY_OTHER_WRITABLE 0
For grep
, see if yours supports GREP_COLORS
or GREP_OPTIONS
.
-
Note that this still makes GNU
ls
output escape sequences for the chosen colours. Compare the output ofls --color=always | od -a
with that ofls --color=never | od -a
with this setting ofLS_COLORS
.– Kusalananda ♦Mar 19, 2019 at 20:02 -
The real trick is that
ls | od -a
doesn't emit the escapes, and that's what I want. Thels
should only emit those sequences when it's sending the output to a terminal. If you tell it to, though, sure it's going to do that. So, don't do that. Mar 19, 2019 at 21:24
adding on @brian-d-foy 's answer, most of the time there is a simple annoying color your like to change of instance the invisible blue of the directory or the "OTHER_WRITABLE"'s flashy green, so let's use our 24-bit display :)
export LS_COLORS='di=04;01;38;5;33:ow=04;01;38;5;33;48;2;60;10;0'
to redefine only directory color and its other-writable color
04: underline;
01: bold/brighter;
38;5;33: redifined-blue;
48;2;60;10;0: dark red background
read more about de ANSI code
-
1The question at hand is about removing all colour from the output of GNU
ls
, not about how to change individual colours.– Kusalananda ♦Aug 15, 2019 at 4:56 -
ok, how about "turning off one particular color with 'ls' with : export LS_COLORS='ow=00' to turn off only the OTHER_WRITABLE. Aug 15, 2019 at 5:45
# chmod -x /path/to/dircolors
e.g.
# chmod -x /usr/bin/dircolors
seems to be an effective way to disable color ls on some systems.
-
1Making binaries non executable is a very effective way to have a lot of weird error messages in logs. Yes, it will probably disable the colors but it's a very bad idea to do it this way. Nov 21, 2020 at 20:37
ls
andgrep
when piping you don't need to do anything because--color=auto
already turns off coloring when its output is a pipe, or in fact anything but a tty; that's what 'auto' means. Similarly FreeBSDls -G
or 'CLICOLOR' is 'disabled if the output is not directed to a terminal unless ... CLICOLOR_FORCE ...'.