As a developer spending much of my day in the terminal, having an optimized and readable LS output is crucial for productivity. The default LS colors work reasonably well, but leave room for improvement. In this comprehensive guide, I‘ll share how to customize LS colors to suit your personal workflow using the LS_COLORS variable.

How the Linux LS Command Handles Colors

The colors and formatting used in the LS output are governed by the DIRCOLORS environmental variable and dircolors command.

DIRCOLORS contains color codes mapping file attributes to colors and terminal display properties. For example, the code for setting directories to blue is di=34.

The dircolors command is used to generate the LS_COLORS environmental variable based on the DIRCOLORS config file. So DIRCOLORS is the source, while LS_COLORS applies those colors to actual LS executions.

Here‘s a simplified overview:

DIRCOLORS config
     |
     | parsed by dircolors
     v
LS_COLORS env var
     |
     | retrieved by ls 
     v
Colored ls output

LS_COLORS uses numeric color codes as well as attribute codes like 01 for bold text. Here is a reference:

Text Attributes:

  • 00 – default
  • 01 – bold
  • 04 – underlined
  • 05 – flashing

Colors:

  • 30 – black
  • 31 – red
  • 32 – green
  • 33 – yellow
  • 34 – blue
  • 35 – magenta

The format for a DIRCOLORS/LS_COLORS code is:

file-attribute=text-attributes;foreground;background

For example:

di=00;34;40 sets directories to blue text on black background
*.txt=00;32;40 sets .txt files to green text on black background

This allows precisely defining colors based on file type, extension, permissions, and other attributes.

Viewing and Editing the Active LS_COLORS Settings

To view your current LS_COLORS configuration, echo the variable in your shell:

echo $LS_COLORS

To make persistent changes, you‘ll need to edit your shell profile (eg. ~/.bashrc):

  1. Add this line:
eval `dircolors -b >> ~/.bashrc` 
  1. Edit ~/.bashrc with vim or another editor

  2. At the bottom you will now see the full LS_COLORS config encoded into key-value pairs

  3. Make changes as desired to color codes

  4. Save changes and reload with:

source ~/.bashrc

Now ls will use the updated colors.

Grouping Colors by File Extension

One useful technique is assigning colors based on file extensions. This allows quickly glimpsing different file types in a directory.

For example to make all .html files magenta:

LS_COLORS="*.html=00;35..." 

Here are some suggested groupings:

Images: Yellow

  • *.jpg=00;33
  • *.png=00;33

Documents: Blue

  • *.docx=00;34
  • *.pdf=00;34

Code: Green

  • *.js=00;32
  • *.py=00;32
  • *.java=00;32

Compressed: Red

  • *.zip=00;31
  • *.tar.gz=00;31

This color coding reflects common workflows. Code files stand out for developers, images for designers, docs for writers, archives for sysadmins.

Color Coding by File Permissions and Ownership

We can also color code files by their system permissions and ownership attributes.

For example, to set user writable files to red:

u+w=00;31

Some ideas:

Root owned: Purple

  • u+root=00;35

Group writable: Green

  • g+w=00;32

Other executable: Magenta

  • o+x=00;35

With such coding applied, at a glance you can review a directory‘s permissions security. Note odd files writable by groups, wide open other executable programs, etc.

This technique takes more advanced Bash skills but can enhance analysis.

Distinguishing Symbolic Links

By default symbolic links display in a different color, commonly cyan. This is controlled by the ln code:

ln=00;36 

However sometimes you may wish to color the actual file pointed to by the symlink differently.

To do so, we can leverage an ls feature to print symlinks with a trailing @. Then color based on that:

LS_COLORS="*.@=00;31..." ln -F

Now the target files will be red while the symlink itself remains cyan. This provides more nuance.

Optimized Color Schemes for Common Setups

With a deep understanding of available color coding, we can define optimized schemes tailored for specific users.

Here are some starter suggestions:

Web Developer Profile

Emphasizes source code with docs and images as secondary:

# Code 
*.html=00;32
*.js=00;32
*.css=00;32

# Docs
*.txt=00;34
*.md=00;34  

# Images    
*.jpg=00;33
*.png=00;33

Graphics Designer Profile

Prioritizes images, then docs, code, and file types relevant to creative workflows:

# Images
*.jpg=00;32   
*.png=00;32
*.psd=00;32

# Docs
*.txt=00;35
*.md=00;35

# Code  
*.html=00;31
*.css=00;31

# Archives
*.zip=00;34
*.7z=00;34

Sysadmin Profile

Focuses on permissions, compressed archives, logs, and executable binaries:

# Compressed
*.zip=00;31  
*.gz=00;31
*.bz2=00;31

# Permissions
ugo+rwx=00;35

# Logs   
*.log=00;34 

# Executables
*.exe=00;32

You can extend these ideas tailored to other common terminal users like data scientists, developers, multi-media editors and more. The concepts remain building intuitive, specialized color coding.

Accessibility Considerations

When choosing custom color palettes, consider accessibility factors as well. Those with color blindness or other vision deficiencies may struggle to discern certain color combinations.

Some tips:

  • Ensure sufficient contrast between foreground and background colors
  • Avoid red/green colorblindness combinations
  • Allow a "high contrast mode" fallback option
  • Permit users to customize colors as needed for accessibility

With such options in place, your color scheme can remain inclusive for all types of users.

Integrating Color Scripts into Bash Tools

Once you have configured an optimal LS_COLORS palette, consider integrating it into your shell scripts and tools.

For example, a backup script may list the latest modified files. Color coding those entries makes scanning easier:

#!/bin/bash

eval `dircolors -b >> ~/.bashrc` # Export colors

latest_files=$(ls -lt | head -5)

echo $latest_files # Colored output

For any script outputting file listings, adding LS_COLORS improves visualization.

Conclusion

With the powerful color configuration options available, a customized LS output can greatly boost productivity. Tailor intuitive colors for your workflows to highlight important files, permissions issues, symlinks and more. Use cases are endless – get creative with ASCII art or prose formatting! With this advanced guide, you now have the expertise to optimize LS colors for terminal readability.

Let me know if you have any other cool tricks for pimping your Bash prompt!

Similar Posts