How can I colour-code files and directories in my shell environment?

14,770

Solution 1

Here is a solution i've found to enable the global terminal colors

Edit your .profile or .bashrc or /etc/profile (depending on availability) or for mountain lion .bash_profile file in your home directory and add following code:

export CLICOLOR=1

export LSCOLORS=GxFxCxDxBxegedabagaced

CLICOLOR=1 simply enables coloring of your terminal.

LSCOLORS=... specifies how to color specific items.

Taken from: https://stackoverflow.com/questions/1550288/mac-os-x-terminal-colors

Solution 2

Probably you are searching the use of the variable LS_COLORS.
In sh, bash and csh shell environments the colours preferences for files, directory, link... are recorded in this variable (if you use ls --color; in many distribution the alias ).

With echo $LS_COLORS you can see your actual setting.
With dircolors you can have the output commands to set the LS_COLORS environment variable.

Note that not all the terminal accept all the specification (it means that you can search an effect that your specific terminal emulation cannot reproduce).

Some example of codes to understand:
di=01;34 means that the directory will be bold and coloured blue (or bright blue).
ln=01;36means that the link will be bold and coloured in cyan (or bright cyan).
*.zip=01;31 means that all the file *.zip will be colored in bright red...

Codes:

  1. no = normal files
  2. di = directory
  3. fi = file
  4. ln = symbolic link
  5. pi = fifo file
  6. so = socket file
  7. bd = block (buffered) special file
  8. cd = character (unbuffered) special file
  9. or = symbolic link pointing to a non-existent file (orphan)
  10. su = file that is setuid (u+s)
  11. sg = file that is setgid (g+s)
  12. tw = dir that is sticky and other-writable (+t,o+w)
  13. ow = dir that is other-writable (o+w) and not sticky
  14. st = dir with the sticky bit set (+t) and not other-writable
  15. mi = non-existent file pointed to by a symbolic link (visible when you type ls -l)
  16. ex = file which is executable (ie. has 'x' set in permissions).
  17. lc rc ec = left , right , end code.

Really nice the script that show with its color each things you have set in your LS_COLORS variable.

As always man dir_colors can give you more informations.

Share:
14,770

Related videos on Youtube

LanceLafontaine
Author by

LanceLafontaine

I'm a driven and hard-working software engineering student at Concordia University with lots of practical experience. When I'm not leveling up my development skills or contributing to open source projects, you can probably find me typing away at a programming competition. If you're looking for a developer with professionalism, passion and expertise, I would love to discuss how my knowledge and skills can benefit your organization.

Updated on September 18, 2022

Comments

  • LanceLafontaine
    LanceLafontaine over 1 year

    I've seen many screencasts online in which the files and directories are colour-coded differently for ease-of-use. I'd like to achieve the same on in my bash terminal, on OS X 10.9 and using iTerm2. I've scoured the internet (including iTerm 2 colour themes) but haven't found exactly what I'm looking for.

    How can I colour-code files and directories differently using iTerm 2 using OS X 10.9?

    • Ben Plont
      Ben Plont over 9 years
      in Linux the file to edit is /etc/DIR_COLORS, not sure about apple.
  • Ben Plont
    Ben Plont over 9 years
    does Apple make use of the standard *nix file: /etc/DIR_COLORS ?
  • krowe
    krowe over 9 years
    According to that page it doesn't seem to. I don't use OSX myself but the question is tagged as such and a LOT of people seem to find it useful.
  • Ben Plont
    Ben Plont over 9 years
    Dang it Apple! Well, if any *nix users come to this question: I recently did this for a couple of Fedora VMs I have deployed. Fedora uses dark blue text on Black background for Directory listings. I just edited the DIR & LINK lines in /etc/DIR_COLORS to read "DIR 01;31;43" and "LINK 01;36", so that I have a contrast that I can read. The attribute codes are listed in the file...
  • Hastur
    Hastur over 9 years
    @BenPlont Not for all *nix ;-) From man dir_colors: "FILES: /etc/DIR_COLORS (Slackware, SuSE and RedHat only; ignored by GNU dircolors(1) and thus Debian...) System-wide configuration file" ... "` ~/.dir_colors` Per-user configuration file." Same distributions.
  • beroe
    beroe over 9 years
    This is nice info, but dircolors (or dir_colors) doesn't seem to exist on OSX?