Last active
August 29, 2015 13:57
-
-
Save adnils/9808161 to your computer and use it in GitHub Desktop.
dynamic tail -F that watches for new files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use Time::HiRes qw (usleep); | |
use IO::Select; | |
my $patterns = (join ' ', @ARGV or "*"); | |
my $sel = IO::Select->new; | |
my (%files, $last); | |
for (my $nop=0;;$nop=1) | |
{ | |
usleep 1e5; | |
chdir $ENV{PWD} or next; | |
for my $fh ($sel->can_read (0)) | |
{ | |
my @lines = <$fh> or next; | |
print "\n\e[33m[", $files{$last = $fh}, "]\e[0m\n" if $last ne $fh; | |
print @lines; | |
} | |
for my $file (`find $patterns -maxdepth 0 -type f 2> /dev/null`) | |
{ | |
chop $file; | |
unless ($file ~~ [values %files]) | |
{ | |
open my $fh, $file or next; | |
$files{$fh} = $file; | |
$nop or seek $fh,0,2; | |
$sel->add ($fh); | |
} | |
} | |
for my $fh (keys %files) | |
{ | |
next if -e $files{$fh}; | |
undef $files{$fh}; | |
$sel->remove ($fh); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment