#!/usr/bin/perl use strict; use warnings; =pod =head1 DESCRIPTION Removes duplicate lines from bash history file. The contents are first saved to ~/.bash_history_old. Needed to keep file size trimmed, to prevent deletion of contents. Deletion occurs when a line count limit is reached (a bash config item I think, and mine is currently set at 5000 lines). =cut # Read history file. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - my $history_file = $ENV{HOME} . '/.bash_history'; # my $history_file = '/tmp/.bash_history'; my @line; open FILE, '<' . $history_file or die "$history_file: $!"; { @line = ; close FILE; } # Save contents. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - my $history_file_old = $history_file . '_old'; umask 0077; # fully private, no permissions to group or others open FILE, '>' . $history_file_old or die "$history_file_old: $!"; { for( @line ) { print FILE; } close FILE; } # Map lines and remove duplicates. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - my @line_collapsed; my %line; for( ;; ) { my $line = pop @line; $line or last; $line{$line} and next; unshift @line_collapsed, $line; $line{$line} = 1; } # Save collapsed history. # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - open FILE, '>' . $history_file or die "$history_file: $!"; { for( @line_collapsed ) { print FILE; } close FILE; }