in reply to ls with colored output

Being a perl forum and all here is a short ls type script that you can configure to your hearts content. You can get all sorts of info out of the file test operators if you want to generate a custom listing like from ls -Cal, see perlman:perlop Script takes one argument [dir to list] or defaults to the cwd. We use ANSI escapes to colour the text. The Term::ANSIColor module exports some constants so we just say what color we want in the print.

#!/usr/bin/perl -w use strict; use Term::ANSIColor qw(:constants); $Term::ANSIColor::AUTORESET = 1; my $dir = shift @ARGV || '.'; opendir DIR, $dir or die "Can't open dir $!\n"; while (my $file = readdir DIR) { if ( -d "$dir/$file" ) { # directory print BLUE "$file\n"; } elsif ( -l "$dir/$file" ) { # symbolic link print CYAN "$file\n"; } elsif ( $file =~ m/\.(?:pl|pm|cgi)$/ ) { # looks Perly print GREEN "$file\n"; } elsif ( -x "$dir/$file" ) { # is executable print YELLOW "$file\n"; } elsif (-f "$dir/$file" and -T "$dir/$file" ) { # is a Text file print RED "$file\n"; } else { # none of the abo +ve! print "$file\n"; } } # Colors available: # BLACK WHITE RED GREEN YELLOW BLUE MAGENTA CYAN

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Replies are listed 'Best First'.
Re: Re: ls with colored output
by jkarthik3 (Initiate) on Sep 13, 2001 at 05:30 UTC
    how about for a unix system (solaris). does the same script works?

      It works under Linux which is unix. I have tested it. Term::ANSIColor is said to work under Solaris, it is unix, so I expect so. Have you considered actually testing it? I often find this an effective approach to see if something will work ;-)

      tachyon

      s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print