48

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 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?

  • 4
    Late but for anyone searching: for GNU ls and grep 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 FreeBSD ls -G or 'CLICOLOR' is 'disabled if the output is not directed to a terminal unless ... CLICOLOR_FORCE ...'. – dave_thompson_085 Jul 19 '16 at 3:13
67

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.

  • 3
    You could also use command ls, of course, but this is a little faster. – iconoclast Jan 1 '14 at 19:45
  • Neat trick! Gotta update the scripts to have leading ``. – Viet Apr 27 '17 at 10:15
  • 1
    I loved the \ls – Alex Angelico Apr 12 at 13:59
25

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.)

  • 1
    This 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). – iconoclast Jan 1 '14 at 16:42
3

on many derivatives 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

2
alias ls=ls

or

unalias ls

This disables permanently the colorings.

  • 2
    Of 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. – iconoclast Aug 5 '15 at 22:50
  • That's why i said "permanently" :-) – Slyx Aug 7 '15 at 0:23
2

In many *nix 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 
1

use the command

unalias ls

this will force it to use standard ls during the session

1

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.

  • Or unalias ls ll l in you .bashrc if you don't have permissions – avsej Jan 4 '18 at 16:42
0

To Turn Off the color: unalias ls

To Turn On the color: alias ls='ls --color=auto'

To Temporarily disable the color: \ls -ltr

0

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 of ls --color=always | od -a with that of ls --color=never | od -a with this setting of LS_COLORS. – Kusalananda Mar 19 at 20:02
  • The real trick is that ls | od -a doesn't emit the escapes, and that's what I want. The ls 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. – brian d foy Mar 19 at 21:24

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.