change-directory-stats 837 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #!/usr/bin/env perl
  2. use strict;
  3. use warnings;
  4. our %cds;
  5. open(HISTORY, "$ENV{HOME}/.histfile") || die "Cannot open history file: $!\n";
  6. sub hashValueDescendingNum {
  7. $cds{$b} <=> $cds{$a};
  8. }
  9. while (<HISTORY>) {
  10. my $command = $_;
  11. if ($command =~ m/^$/) {
  12. next;
  13. }
  14. my @comm = split(' ', $command);
  15. if ($comm[0] eq "sudo") {
  16. shift @comm;
  17. }
  18. if (not $comm[0] eq "cd") {
  19. next;
  20. }
  21. if (exists $comm[1]) {
  22. if (not exists($cds{$comm[1]})) {
  23. $cds{$comm[1]} = 0;
  24. }
  25. $cds{$comm[1]}++;
  26. } else {
  27. if (not exists($cds{"HOME"})) {
  28. $cds{"HOME"} = 0
  29. }
  30. $cds{"HOME"}++;
  31. }
  32. }
  33. foreach my $key (sort hashValueDescendingNum (keys(%cds))) {
  34. my $value = sprintf "%8s", $cds{$key};
  35. print "$value\t$key\n";
  36. }