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 gkittelson 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 4

Status
Not open for further replies.

sbrews

Technical User
Jun 11, 2003
413
US
Ever wish you had a color ls on AIX - like the one available in linux?

You cant or are not allowed to install 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 = "" ]]
then
#   echo "\n\n  Usage: lsc -la | pg"
#   echo "  Note: use \033[32mpg\033[1;33m instead of \033[31mmore\033[1;33m \c"
#   echo "so that the colors are properly displayed."
   ls
   exit 0
fi

#***************************************************************************
#* Issue ls with parameters, send to a tmp file                            *
#***************************************************************************
ls $1 $2 |grep -v $$.tmp >> ${HOME}/$$.tmp

#***************************************************************************
#* Process the file.                                                       *
#* If dir: blue, link: light blue, exe: green, no match: just display      *
#* Other colors may be added as needed/the mood strikes.                   *
#***************************************************************************
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
 
Nice script sbrews!

I will try it tomorrow when i get to work!

Thanks

Regards,
Khalid
 
Thanks Scott, have a star for bringing a little colour into our lives!

I want to be good, is that not enough?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top