Skip to content

Instantly share code, notes, and snippets.

@nikolay-n
Created October 5, 2020 18:23
Show Gist options
  • Save nikolay-n/e6d40f1e7a8a26de15e1a7aed12c1d91 to your computer and use it in GitHub Desktop.
Save nikolay-n/e6d40f1e7a8a26de15e1a7aed12c1d91 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
# Files that was born at the near same time
# Usage: perl files_born.pl file_path [5s|m|h|d]
use IPC::Open2;
my $file = shift;
my $interval = shift // "1m";
my $attr = "kMDItemFSCreationDate";
my $c_date = `mdls -raw -name $attr "$file"`;
my $ts = int(`date -j -f'%F %T %z' "$c_date" +%s 2>/dev/null`);
my $map = {("s",1,"m",60,"h",3600,"d",86400)};
$interval =~ /([\d\.]+)([smhd])/ and do {
my $sec_half = int((int($1) * $map->{$2}) / 2);
my @range = map{
chomp;
$t=`date -j -r $_ +"%Y-%m-%dT%H:%M:%S"`;
chomp($t);
"\$time.iso(".$t.")"
}($ts - $sec_half, $ts + $sec_half);
my @files = split("\n", `mdfind '($attr > $range[0]) && ($attr < $range[1])'`);
open2(OUT, IN, "xargs -0 mdls -name $attr") and do{
print IN join("\x00",@files);close(IN);
my $i = 0,
my %dates;
for (map{chomp;(split(/ += +/, (split(/ \+/))[0]))[1]} (<OUT>)){
$dates{$_} = $files[$i++];
}
my $out = "";
my $line = 0;
$i=0;
for(reverse sort keys %dates){
my $f = $dates{$_};
if($f eq $file){
$f = "\e[91m$f\e[0m";
$line = $i;
}
s/(\d+:\d+:\d+)/\e[33m$1\e[0m/;
$out .= "[ \e[90m$_\e[0m ] $f\n";
$i++;
}
open(L,"|-","less","-S", "-R", "+$line", "-j", int(int(`tput lines`)/2))and do {print L $out;close(L);}
};
};
@nikolay-n
Copy link
Author

Shows files created 30 seconds before and after specified file was created
$ perl files_born.pl file_path 1m

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment