Prevent duplicate commands in Terminal history, and clear history

I just figured out a fantastic little trick... in the procedure above, I wrote about creating a list of commands in .good_history, with the very first command being history -c && cd ~ && sudo cp .good_history .bash_history && exit, which copies .good_history to .bash_history, so the next time you open Terminal, all your most-used commands are already populated in the history, and all the commands you messed up are removed so you don't have to scroll through them the next time you open Terminal.

It soon becomes second-nature to scroll up until that first command is reached then hitting 'Enter' in order to close Terminal, rather than typing 'exit' or clicking the 'x' on the window to close Terminal.

But when your list of commands gets long, it get difficult to remember what each command does... if only there were some way of including a description along with the command, eh?

Well, there is.

history -cw && cd ~ && sudo cp .good_history .bash_history && exit					# Reset Terminal commands
journalctl -b											# View Boot Logs
sudo top												# View Processes
sudo df -l												# Show file system stats
echo "System Entropy"; cat /proc/sys/kernel/random/entropy_avail					# View System Entropy
sudo systemctl list-unit-files									# List All Services
sudo systemctl list-units --type=service --all							# List All Services
sudo systemctl --type=service									# List All Services
sudo service --status-all										# List All Services
sudo systemd-analyze critical-chain									# Boot Chain Analysis
sudo systemd-analyze blame										# Boot Startup Time Analysis
read -p "Command? " in;sudo dpkg -S */$in$*                                                          # Show which package a command belongs to
read -p "Package? " in;sudo dpkg -L $in | xargs which						# Show which commands belong to a package
compgen -c | sort | uniq										# Show all available commands
sudo apt list --installed										# Installed Packages
sudo dpkg-query -l											# Installed Packages (verbose)
sudo swapon --show											# Show Swap File Status
sudo arcstat												# ARC Cache Stats
sudo zpool iostat -vl 10										# Drive Read/Write Stats
currentdate=$(date +%Y-%m-%d_%H%M); echo Saving new snapshot...; sudo zsysctl save $currentdate -s	# Save ZFS Snapshot
sudo zfsflush.sh -s 1 -p bpool; sudo zfsflush.sh -s 1 -p rpool					# Clear All ZFS Snapshots
sudo zfs list -t snapshot										# List ZFS Snapshots
sudo zpool status											# Show zpool Status

In .good_history, just put your comment after the command, with a # (tab, then pound sign).

If you want to get fancy with it, you could vary the number of tabs so all the descriptions align at the same column in Terminal as you scroll through the list of commands, as I've done.

"But what if the list of commands I've entered is really, really long... so pressing the up key to get to that first command in the .good_history list to exit takes too long? I'd rather just type 'exit'!", some may ask.

Ok:
gedit /home/$USER/.bashrc

Add this at the end of the file:

# Trap the 'exit' command. We can't do anything about mousing out of the window, though.
trap 'echo "closing" && history -cw && cd ~ && sudo cp .good_history .bash_history && sleep 3' EXIT
1 Like