Browse Source

Run statistics on CD commands

Samuel W. Flint 7 years ago
parent
commit
415b33b7ab
1 changed files with 48 additions and 0 deletions
  1. 48 0
      change-directory-stats

+ 48 - 0
change-directory-stats

@@ -0,0 +1,48 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+our %cds;
+
+open(HISTORY, "$ENV{HOME}/.histfile") || die "Cannot open history file: $!\n";
+
+
+sub hashValueDescendingNum {
+    $cds{$b} <=> $cds{$a};
+}
+
+while (<HISTORY>) {
+    my $command = $_;
+
+    if ($command =~ m/^$/) {
+        next;
+    }
+
+    my @comm = split(' ', $command);
+
+    if ($comm[0] eq "sudo") {
+        shift @comm;
+    }
+
+    if (not $comm[0] eq "cd") {
+        next;
+    }
+
+    if (exists $comm[1]) {
+        if (not exists($cds{$comm[1]})) {
+            $cds{$comm[1]} = 0;
+        }
+        $cds{$comm[1]}++;
+    } else {
+        if (not exists($cds{"HOME"})) {
+            $cds{"HOME"} = 0
+        }
+        $cds{"HOME"}++;
+    }
+}
+
+foreach my $key (sort hashValueDescendingNum (keys(%cds))) {
+    my $value = sprintf "%8s", $cds{$key};
+    print "$value\t$key\n";
+}