jkarthik3 has asked for the wisdom of the Perl Monks concerning the following question:

hi, is there an easy way to list (ls) the file names with differnt colors in accordance with their extension. For example all *.txt files in one color, all *.pl files in a differnt color etc???

Replies are listed 'Best First'.
Re: ls with colored output
by tachyon (Chancellor) on Sep 12, 2001 at 06:53 UTC

    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

      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

Re: ls with colored output
by Zaxo (Archbishop) on Sep 12, 2001 at 04:23 UTC

    One tip. If you alias this, use the --color=auto option. That way scripts will get monochrome listings and color will only appear in a tty.

    After Compline,
    Zaxo

Re: ls with colored output
by broquaint (Abbot) on Sep 12, 2001 at 14:03 UTC
    AFAICT ls uses the environment variable $LS_COLORS to define what colours are associated with which extensions. So if you re-declare that in your shell rc file (~/.(?:ba|t?c|z|k|pdk)shrc) you can pick and choose the colors of your choice e.g
    export LS_COLORS=*.txt:01;32:*.pl:01;35:
    ls should now list *.txt files in green and *.pl in a purpley colour. The numbers and punctuation are in a standard escape code format, and generally go - \d\d(?:;\d\d;)?(?:\d\d)?. I'm not sure how portable this all is but I get the feeling that it should be applicable on most Linux systems.
    HTH

    broquaint

Re: ls with colored output
by cajun (Chaplain) on Sep 12, 2001 at 02:42 UTC
    Try 'man ls'.