Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Color ls script

Useful Commands-aix

Color ls script

by  sbrews  Posted    (Edited  )
Ever wish you had a color ls on AIX - like the one available in linux?

You cant (dont have root privs [neutral] ) or are not allowed to install (OMG! its open source, everybody panic! [bugeyed] ) the GNU ls? You might like this script.

This is something I threw together to give me colors.
Once the script is saved, just set up an alias:

alias lsc=/path/to/script

Then to see your files in color: lsc -la
For longer (more than one screen) listings, pipe to pg - as more messes up the escape color codes.

One more thing... lsc with no parameters will just list your files the same as a plain old ls would.

use/modify to suit your needs

Code:
#!/bin/ksh
#***************************************************************************
#* Created by: Scott Brewster                                              *
#*       Date: 16-Apr-2007                                                 *
#*   Location: TBD                                                         *
#*                                                                         *
#* Description                                                             *
#* ----------------------------------------------------------------------- *
#* Half an effort to create a color 'ls' script for AIX WITHOUT having to  *
#* install the GNU ls (which does support color).  Lots of room for        *
#* improvement.                                                            *
#*                                                                         *
#*     Date       Modified By     Reason for modification                  *
#* -----------  ----------------  ---------------------------------------- *
#* 16-Apr-2007  Scott Brewster    Created                                  *
#*                                                                         *
#***************************************************************************
#***************************************************************************
#* Check to see if any parameters were passed.  If not, just list files.   *
#***************************************************************************
if [[ $1 = ""  || $(echo $1|cut -b1) = "/" ]]
then
   ls $1
   exit 0
fi

#***************************************************************************
#* Issue ls with parameters, send to a tmp file                            *
#* $1 is any ls parameters - ie -la                                        *
#* $2 may be a directory other than CWD - ie /etc                          *
#* the grep -v is in case this is issued in home dir - we dont want to see *
#* our tmp file since it will be deleted when this script finishes.        *
#***************************************************************************
ls $1 $2 |grep -v $$.tmp >> ${HOME}/$$.tmp

#***************************************************************************
#* Process the file.                                                       *
#* If dir: blue, link: light blue, char/block device: inverse blue, white  *
#* socket: red, everything else: no color                                  *
#***************************************************************************
while read LINE
do
  TYPE=$(echo $LINE|cut -b1)
  case $TYPE in
           d) echo "\033[1;34m$LINE\033[0m" ;;
           l) echo "\033[1;36m$LINE\033[0m" ;;
        [cb]) echo "\033[44;37m$LINE\033[0m" ;;
           s) echo "\033[0;31m$LINE\033[0m" ;;
           -) if [[ $(echo $LINE|cut -b4) = "x" || \
                    $(echo $LINE|cut -b7) = "x" || \
                    $(echo $LINE|cut -b10) = "x" ]]
              then
                echo "\033[32m$LINE\033[0m"
              else
                echo "\033[0m$LINE"
              fi ;;
           *) echo "\033[0m$LINE" ;;
  esac
done < ${HOME}/$$.tmp

#***************************************************************************
#* Clean up the tmp file                                                   *
#***************************************************************************
rm ${HOME}/$$.tmp
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top