Unix LS Command With Colors… Without the —color Option

| Posted by watashii | Filed under Programming, Unix

lscolor03

Having command line text in colors makes things easier to read.  For example the ls command for listing directories and files.  In some Unix environments, the ls command may not support the –color option to let us do that (as it  requires GNU ls).  So how can we workaround this to add color to ls?

Here are my steps to make it happen. Note i’m no Unix guru, and I didn’t have time to perfect it.. but it gets the job done!

1) Basic test. Echo some random color text.  I had to specify the full path of echo to make it work. The value 01;34 is the color code for light blue.

/bin/echo "\033[0m\033[01;34mthis is blue text\033[0m"

lscolor01

2) Here are some other color codes.

Black       0;30     Dark Gray     1;30
Blue        0;34     Light Blue    1;34
Green       0;32     Light Green   1;32
Cyan        0;36     Light Cyan    1;36
Red         0;31     Light Red     1;31
Purple      0;35     Light Purple  1;35
Brown       0;33     Yellow        1;33
Light Gray  0;37     White         1;37

3) Here are the ls commands I would like to color… with a bit of help from Google somewhere.

ls -ld .*/             # List hidden directories
ls -ld */              # List visible directories
ls -lAd .* |grep '^-'  # List hidden files
ls -l |grep '^-'       # List visible files

4) Combining 1 and 3, gives me the following commands:

/bin/echo "\033[0m\033[01;34m`ls -ld .*/`\033[0m"
/bin/echo "\033[0m\033[01;32m`ls -ld */`\033[0m"
/bin/echo "\033[0m\033[00;31m`ls -lAd .* |grep '^-'`\033[0m"
/bin/echo "\033[0m\033[01;33m`ls -l |grep '^-'`\033[0m"

5) Mash it together, assign it to an alias, and thats it! One single action does the job!

alias ll='/bin/echo "\033[0m\033[01;34m`ls -ld .*/`\033[0m" ; /bin/echo "\033[0m\033[01;32m`ls -ld */`\033[0m" ; /bin/echo "\033[0m\033[00;31m`ls -lAd .* |grep '^-'`\033[0m" ; /bin/echo "\033[0m\033[01;33m`ls -l |grep '^-'`\033[0m"'

lscolor02

Share:

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Print
  • email

Related Posts:

  1. Unix ps command – Long Process Listing
  2. UNIX file size listing – du command
  3. Unix Shell – Check If Directory Is Empty
  4. Unix Split, Join & Validate Files
  5. Unix – Creating Cron Jobs with Crontab

Tags: , ,

Leave a Reply