Dashboard > Public Content > ... > LinuxShell > FindFile
  Public Content Log In   View a printable version of the current page.  
  FindFile
Added by James Richardson, last edited by James Richardson on Jul 09, 2006
Labels: 
(None)

use File::Find;

$TERM_HOME="\x1b[H";
$TERM_CLR_EOL="\x1b[K";
$TERM_CLR_SCREEN="\x1b[J";
$TERM_RED_COLOR="\x1b[31m";
$TERM_GREEN_COLOR="\x1b[32m";
$TERM_NORMAL_COLOR="\x1b[m";            # no attributes;

sub wanted {

    local ($file_path) = $_;

    return if ( $file_path =~ /\.s*[ao]$/);
    return if ( $file_path =~ /~$/ );
    return if ( $file_path =~ /^\.\#.*\d+\.\d+$/ );
    return if ( $file_path =~ /^\#.*\#$/ );
    return if ( ! -f $file_path );
    return if ( ! -f $file_path );
    return if ( -B $file_path );

    if (-f $file_path) {
        if (!open (FILE, $file_path)) {
            print "Can't open: $File::Find::name: $!\n";
        }
        else {
            while (<FILE>) {
                if (/$findit/) {
                    print "$File::Find::name line $.\n" unless $f{$File::Find::name}++ && $all;
                    chomp;
                    s/^\s*/\t/;
                    print "Line $.:" if $all;
                    print "$TERM_GREEN_COLOR $_ $TERM_NORMAL_COLOR\n" if $verbose;
                    last unless $all;
                }
            }
            close (FILE);
        }
    }
}


($dir, $findit, $verbose, $all) = @ARGV;

find (\&wanted, $dir);
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.1 Build:#806 May 06, 2007) - Bug/feature request - Contact Administrators