Prevent duplicate commands in Terminal history, and clear history

If you use Terminal a lot, you've noticed that each command you type is saved, and you can use the up and down arrow keys to 'scroll' through the commands.

The problem, though, is if you use the same command more than once, it gets entered into the list more than once. Here's how to prevent that:

sudoedit ~/.bash_profile
Enter the line: export HISTCONTROL=ignoredups
Save the file and exit.

If you want to clear the history for the current session: history -c

If you want to clear the history for all past sessions: history -cw


One problem with history -cw is that your commands are all gone, so you have to remember them.

Start Nautilus, navigate to Home... you'll see a file named .bash_history.

Now create a new file, rename it .good_history, edit it in Text Editor, and put in the list of commands you want.

Now, when you clear your history, you'd issue:
history -c && cd ~ && sudo cp .good_history .bash_history && exit

That wipes the session Terminal history, changes the directory back to the default, copies your .good_history file over to .bash_history, then exits Terminal.

You might want to include that in your .good_history file. If you put it as the first entry, it'll be the last command you scroll to. So all you have to do when you're done using Terminal is hold the Up key until it scrolls to that entry, hit Enter, and you're done.

If you run that command each time before you close Terminal, all the commands you want to use are pre-populated in Terminal's history, without all the commands you messed up.


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.

systemd-analyze critical-chain	# Boot chain analysis
sudo systemd-analyze blame	# Boot startup time analysis
sudo apt list --installed	# Installed packages
sudo arcstat	# ARC cache stats
sudo zpool iostat -vl 10	# Drive read/write stats
sudo swapon --show	# Show swap file status
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.