You are not logged in.

#1 2020-10-18 13:01:32

860lacov
Member
Registered: 2020-05-02
Posts: 452

[SOLVED] How to set up ls to color output?

I've found thisl:
and:
color output
and:
dircolors

I tried

ls --color=auto

but it didn't work. My movie and document files are grey.


I wanted to try with dircolors
I made .dir_colors file. Inside this file I have output from dircolors -p

Then I putted line:

eval "$(dircolors dircolors -b ~/.dir_colors)"

into my .bashrc
After sourcing it and after pc restart nothing changed.

I'm not sure that should I do to make it work.

Last edited by 860lacov (2020-10-18 20:43:58)

Offline

#2 2020-10-18 13:27:00

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] How to set up ls to color output?

First, your eval command is wrong.  You repeat dircolors twice which is why it fails.  Also - there really is no need to run dircolors every time the shell starts up (though that is what many tutorials advise).  Running a command line "dircolors -b ~/.dir_colors" will simply generate output of how to set LS_COLORS.  You can run the command once in an interactive shell, and just append the output to your shellrc file.

However, note your terminal (emulator) will also need to support color (and be configured for color).  Most of them have at least 16 colors enabled "out of the box" but if fixing the dircolors command doesn't work, you should tell us about your terminal ... or just test whether the folowing produces red text:

tput setaf 1; echo hello world; tput setaf 0

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#3 2020-10-18 13:54:58

860lacov
Member
Registered: 2020-05-02
Posts: 452

Re: [SOLVED] How to set up ls to color output?

I use xfce4-terminal
In my config dircolor occur only once. I've made a mistake here.

When I run

tput setaf 1; echo hello world; tput setaf 0

text is dark red/brown (hardly visible)


I started from begening (removed dircolors command from .bashrc

When I run dircolors -b I get:

LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:';
export LS_COLORS

It seems to me that it should set colors temporarily because LS_COLORS variable is being set and at the end there is export command

But when I ls my Videos directory everything is gray.

So I did:
dircolors -b
I copied LS_COLORS='rs...
I did export LS_COLORS='rs...

And it kind of work. But...
My videos (avi,mkv,...,), images(jpg,png) have the same dirty pinkish color and documents(pdf,txt) are gray and some zst archives are similar to movies and images bud darker.

Offline

#4 2020-10-18 14:03:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] How to set up ls to color output?

So it worked.  It sounds like you just need to configure the color scheme of xcfe4-terminal to your liking.  The "hello world" command I gave outputs red text - but you describe it as brown and hardly visible which means your xfce4-terminal color scheme has a dark brown color set for color 1 (which is generally a shade of red).  It likely has other shades that either do not work well for your monitor or just are not to your liking for the other colors.

Last edited by Trilby (2020-10-18 14:04:14)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#5 2020-10-18 15:43:25

860lacov
Member
Registered: 2020-05-02
Posts: 452

Re: [SOLVED] How to set up ls to color output?

Thank you but I'm not sure if I get it right.

If I don't use LC_COLORS and change xfce terminal colors (color tab in xfce4-terminal settings) nothing is going on.
When I export LC_COLORS then the output of ls is changed but the colors of files don't math color scheme (from settings)

Offline

#6 2020-10-18 15:49:22

seth
Member
Registered: 2012-09-03
Posts: 50,009

Re: [SOLVED] How to set up ls to color output?

What does this look like?

print '\e[1;35m color of mp4 \e[0m'

Offline

#7 2020-10-18 15:53:20

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] How to set up ls to color output?

860lacov wrote:

If I don't use LC_COLORS and change xfce terminal colors (color tab in xfce4-terminal settings) nothing is going on.

Correct, except it's LS_COLORS.  the 'ls' program uses the environment variable LS_COLORS to determine which color codes to emit (if and only if a --color or related flag is passed to ls).  In other words 'ls' doesn't actually present any colors at all, it just sends instructions to the terminal to show colors.  The ls program sends numerical codes representing the colors.  These codes range from 0-255, though most often people would use 0-15 for most of their LS_COLORS settings.  The xcfe terminal determines what colors these numbers actually represent.  In fact, the terminal can associate any color at all with a given number, but there are de-facto standards for the first 16 colors that 1 = (some shade of) red, 2 = green, 3 = yellow, etc.  But you can configure your terminal to change the exact shades of these colors used.

If you don't use LS_COLORS, it doesn't matter how your terminal is configured to map color instructions to actual colors becuase nothing is sending it color instructions.  But once you use LS_COLORS, you'll want to configure your terminal so that those color instructions generate colors you prefer.

860lacov wrote:

When I export LC_COLORS then the output of ls is changed but the colors of files don't math color scheme (from settings)

Can you be more specific?  How is the output changed exactly?  Can you give an example of a file (or file type) that has a color that doesn't match the color scheme?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#8 2020-10-18 17:25:54

860lacov
Member
Registered: 2020-05-02
Posts: 452

Re: [SOLVED] How to set up ls to color output?

Currently, when I do printenv there is no LS_COLORS variable
For my xfce terminal I chose color palette dark pastels (settings -> colors -> profiles)

when I do ls my txt, jpg, avi, pdf files are gray

when I do:

LS_COLORS='rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=00:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.zst=01;31:*.tzst=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.wim=01;31:*.swm=01;31:*.dwm=01;31:*.esd=01;31:*.jpg=01;35:*.jpeg=01;35:*.mjpg=01;35:*.mjpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.webp=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.m4a=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.oga=00;36:*.opus=00;36:*.spx=00;36:*.xspf=00;36:';

And then ls
My files (txt, jpg, mkv, avi, pdf) have pin/magenta color
I can see than in line LS=... avi and jpg are having the same values so probably it's normal. Don't look good but it's normal.

So to sum up. Is this the correct (and maybe default) way to set ls colors?:

generate colors for LS_COLORS by running dircolors -b
put the output of dircolors to bashrc in format:
export LS_COLORS=...[here is dircolors -b output

I think that it is not pretty but is it the way I should do this?

And if I want to change for example mkv highlighting I need to change value mkv=01;35 to something else?

Offline

#9 2020-10-18 18:44:02

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] How to set up ls to color output?

If you don't like the color codes specified in LS_COLORS, change them.  That's the point.  In your LS_COLORS, both *.avi and *.jpg are listed with the same color codes, so that will produce the same color from ls, yes.  But you can change one or both of them - they are both set to use the fifth color which is generally a shade of cyan.

860laconv wrote:

And if I want to change for example mkv highlighting I need to change value mkv=01;35 to something else?

Yup.  You can also add other file extensions as desired.

860lacov wrote:

So to sum up. Is this the correct (and maybe default) way to set ls colors?:

generate colors for LS_COLORS by running dircolors -b
put the output of dircolors to bashrc in format:
export LS_COLORS=...
And if I want to change for example mkv highlighting I need to change value mkv=01;35 to something else?

That has been my recommendation.  You'll find many tutorials that will suggest combining those two steps in bashrc like below:

incorrect code block removed - see posts below.

The only advantage of this is the file ~/.dircolors is a bit easier to edit than the LS_COLORS variable.  So if you are in the process of doing a lot of tweaking and adjusting to ~/.dircolors, this is probably better.  But once you've configured the colors, there is really no need to have a subshell reread and reformat that file every time the shell starts up (which is what the dircolors command does).

Last edited by Trilby (2020-10-18 21:50:29)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#10 2020-10-18 19:43:28

860lacov
Member
Registered: 2020-05-02
Posts: 452

Re: [SOLVED] How to set up ls to color output?

Thank you very much.
I have one more problem

with:
In my $HOME I made a .dircolors file with command dircolors -b > .dircolors
In my .bashrc I have:

export LS_COLORS=$(dircolors ~/.dircolors)

But when I source rc file or I get an error:

dircolors: /home/jm/.dircolors:1: invalid line;  missing second token

Offline

#11 2020-10-18 19:45:32

seth
Member
Registered: 2012-09-03
Posts: 50,009

Re: [SOLVED] How to set up ls to color output?

command dircolors -b > .dircolors

Try "dircolors -p" …

Offline

#12 2020-10-18 20:01:26

860lacov
Member
Registered: 2020-05-02
Posts: 452

Re: [SOLVED] How to set up ls to color output?

Didn't help but when I do [in xfce terminal]:

eval $(dircolors ~/.dircolors)

Then coloring is working. I can put this command into bashrc but I don't understand why export isn't working

Offline

#13 2020-10-18 20:07:00

seth
Member
Registered: 2012-09-03
Posts: 50,009

Re: [SOLVED] How to set up ls to color output?

Yes it did help, because "dircolors -b" will generate an invalid dircolors file (hence the error that is now gone)

Post your entire ~/.bashrc and ~/.dircolors (in code tags, https://bbs.archlinux.org/help.php#bbcode )

Offline

#14 2020-10-18 20:11:32

860lacov
Member
Registered: 2020-05-02
Posts: 452

Re: [SOLVED] How to set up ls to color output?

.bashrc

#
# ~/.bashrc
#

# If not running interactively, don't do anything
[[ $- != *i* ]] && return

export LS_COLORS=$(dircolors ~/.dircolors)

alias ls='ls --color=auto'
alias ll='ls -alh --color=auto'
alias grep='grep --color=auto'

PS1='[\u@\h \W]\$ '

# Use bash-completion, if available
[[ $PS1 && -f /usr/share/bash-completion/bash_completion ]] && \. /usr/share/bash-completion/bash_completion

# History
HISTSIZE=1000
HISTFILESIZE=1000
HISTCONTROL=erasedups

.dircolors

# Configuration file for dircolors, a utility to help you set the
# LS_COLORS environment variable used by GNU ls with the --color option.
# Copyright (C) 1996-2020 Free Software Foundation, Inc.
# Copying and distribution of this file, with or without modification,
# are permitted provided the copyright notice and this notice are preserved.
# The keywords COLOR, OPTIONS, and EIGHTBIT (honored by the
# slackware version of dircolors) are recognized but ignored.
# Below are TERM entries, which can be a glob patterns, to match
# against the TERM environment variable to determine if it is colorizable.
TERM Eterm
TERM ansi
TERM *color*
TERM con[0-9]*x[0-9]*
TERM cons25
TERM console
TERM cygwin
TERM dtterm
TERM gnome
TERM hurd
TERM jfbterm
TERM konsole
TERM kterm
TERM linux
TERM linux-c
TERM mlterm
TERM putty
TERM rxvt*
TERM screen*
TERM st
TERM terminator
TERM tmux*
TERM vt100
TERM xterm*
# Below are the color init strings for the basic file types.
# One can use codes for 256 or more colors supported by modern terminals.
# The default color codes use the capabilities of an 8 color terminal
# with some additional attributes as per the following codes:
# Attribute codes:
# 00=none 01=bold 04=underscore 05=blink 07=reverse 08=concealed
# Text color codes:
# 30=black 31=red 32=green 33=yellow 34=blue 35=magenta 36=cyan 37=white
# Background color codes:
# 40=black 41=red 42=green 43=yellow 44=blue 45=magenta 46=cyan 47=white
#NORMAL 00 # no color code at all
#FILE 00 # regular file: use no color at all
RESET 0 # reset to "normal" color
DIR 01;34 # directory
LINK 01;36 # symbolic link. (If you set this to 'target' instead of a
 # numerical value, the color is as for the file pointed to.)
MULTIHARDLINK 00 # regular file with more than one link
FIFO 40;33 # pipe
SOCK 01;35 # socket
DOOR 01;35 # door
BLK 40;33;01 # block device driver
CHR 40;33;01 # character device driver
ORPHAN 40;31;01 # symlink to nonexistent file, or non-stat'able file ...
MISSING 00 # ... and the files they point to
SETUID 37;41 # file that is setuid (u+s)
SETGID 30;43 # file that is setgid (g+s)
CAPABILITY 30;41 # file with capability
STICKY_OTHER_WRITABLE 30;42 # dir that is sticky and other-writable (+t,o+w)
OTHER_WRITABLE 34;42 # dir that is other-writable (o+w) and not sticky
STICKY 37;44 # dir with the sticky bit set (+t) and not other-writable
# This is for files with execute permission:
EXEC 01;32
# List any file extensions like '.gz' or '.tar' that you would like ls
# to colorize below. Put the extension, a space, and the color init string.
# (and any comments you want to add after a '#')
# If you use DOS-style suffixes, you may want to uncomment the following:
#.cmd 01;32 # executables (bright green)
#.exe 01;32
#.com 01;32
#.btm 01;32
#.bat 01;32
# Or if you want to colorize scripts even if they do not have the
# executable bit actually set.
#.sh 01;32
#.csh 01;32
 # archives or compressed (bright red)
.tar 01;31
.tgz 01;31
.arc 01;31
.arj 01;31
.taz 01;31
.lha 01;31
.lz4 01;31
.lzh 01;31
.lzma 01;31
.tlz 01;31
.txz 01;31
.tzo 01;31
.t7z 01;31
.zip 01;31
.z 01;31
.dz 01;31
.gz 01;31
.lrz 01;31
.lz 01;31
.lzo 01;31
.xz 01;31
.zst 01;31
.tzst 01;31
.bz2 01;31
.bz 01;31
.tbz 01;31
.tbz2 01;31
.tz 01;31
.deb 01;31
.rpm 01;31
.jar 01;31
.war 01;31
.ear 01;31
.sar 01;31
.rar 01;31
.alz 01;31
.ace 01;31
.zoo 01;31
.cpio 01;31
.7z 01;31
.rz 01;31
.cab 01;31
.wim 01;31
.swm 01;31
.dwm 01;31
.esd 01;31
# image formats
.jpg 01;35
.jpeg 01;35
.mjpg 01;35
.mjpeg 01;35
.gif 01;35
.bmp 01;35
.pbm 01;35
.pgm 01;35
.ppm 01;35
.tga 01;35
.xbm 01;35
.xpm 01;35
.tif 01;35
.tiff 01;35
.png 01;35
.svg 01;35
.svgz 01;35
.mng 01;35
.pcx 01;35
.mov 01;35
.mpg 01;35
.mpeg 01;35
.m2v 01;35
.mkv 01;35
.webm 01;35
.webp 01;35
.ogm 01;35
.mp4 01;35
.m4v 01;35
.mp4v 01;35
.vob 01;35
.qt 01;35
.nuv 01;35
.wmv 01;35
.asf 01;35
.rm 01;35
.rmvb 01;35
.flc 01;35
.avi 01;35
.fli 01;35
.flv 01;35
.gl 01;35
.dl 01;35
.xcf 01;35
.xwd 01;35
.yuv 01;35
.cgm 01;35
.emf 01;35
# https://wiki.xiph.org/MIME_Types_and_File_Extensions
.ogv 01;35
.ogx 01;35
# audio formats
.aac 00;36
.au 00;36
.flac 00;36
.m4a 00;36
.mid 00;36
.midi 00;36
.mka 00;36
.mp3 00;36
.mpc 00;36
.ogg 00;36
.ra 00;36
.wav 00;36
# https://wiki.xiph.org/MIME_Types_and_File_Extensions
.oga 00;36
.opus 00;36
.spx 00;36
.xspf 00;36

Offline

#15 2020-10-18 20:17:45

seth
Member
Registered: 2012-09-03
Posts: 50,009

Re: [SOLVED] How to set up ls to color output?

export LS_COLORS=$(dircolors ~/.dircolors)

Won't work. The output of "dircolors" constitutes an assignment and an export itself.

Do eg.

dircolors ~/.dircolors | source /dev/stdin

Offline

#16 2020-10-18 20:28:32

860lacov
Member
Registered: 2020-05-02
Posts: 452

Re: [SOLVED] How to set up ls to color output?

Isn't

eval $(dircolors ~/.dircolors)

the same in result as

dircolors ~/.dircolors | source /dev/stdin

?
I checked what eval is. I checked in my bashrc and It works but I don't know if it can have any undesirable effects.

Offline

#17 2020-10-18 20:41:19

seth
Member
Registered: 2012-09-03
Posts: 50,009

Re: [SOLVED] How to set up ls to color output?

Yup.
You can shopt away the subshell ("lastpipe"), but they end up doing the same.

Offline

#18 2020-10-18 21:03:42

860lacov
Member
Registered: 2020-05-02
Posts: 452

Re: [SOLVED] How to set up ls to color output?

I read quite a lot during research about dircolorsI but probably I somehow missed it.
For example:
ogm 01;35

Where can I find the list of colors/codes I can use?

Offline

#19 2020-10-18 21:10:48

seth
Member
Registered: 2012-09-03
Posts: 50,009

Re: [SOLVED] How to set up ls to color output?

    for x in 0 1 4 5 7 8; do
        for i in {30..37}; do
            for a in {40..47}; do
                printf "\e[$x;$i;$a""m\\\e[$x;$i;$a""m\e[0;37;40m "
            done
            echo
        done
    done
    echo

Offline

#20 2020-10-18 21:53:11

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,449
Website

Re: [SOLVED] How to set up ls to color output?

Sorry about the wrong code above - I've not used dircolors in ages.

The code above will output all those combinations - there are actually quite a few more colors that can be used to.  They are described here:
https://en.wikipedia.org/wiki/ANSI_escape_code#Colors


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB