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

5 Likes

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.

3 Likes

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

An updated .good_history list:

history -c && cd ~ && sudo cp .good_history .bash_history && exit					# Reset Terminal commands
tput rev;read -p "Action? " in;tput sgr0;apropos $in							# List of commands for action taken
tput rev;read -p "Command? " in;tput sgr0;whatis $in                                                 # Action of a command
tput rev;read -p "Command? " in;tput sgr0;sudo dpkg -S */$in$*					# Show which package a command belongs to
tput rev;read -p "Package? " in;tput sgr0;sudo dpkg -L $in | xargs which				# Show which commands belong to a package
tput rev;read -p "File or Directory? " in;tput sgr0;whereis $in					# Show location of file or directory
compgen -c | sort | uniq										# Show all available commands
journalctl -b											# View Boot Logs
sudo top												# View Processes
sudo df -l												# Show file system stats
sudo cat /proc/meminfo										# View memory 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 xprop												# Click to get window properties.
sudo systemd-analyze critical-chain									# Boot Chain Analysis
sudo systemd-analyze blame										# Boot Startup Time Analysis
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
sudo blkid												# Drives UUID / Partitions PARTUUID
sudo zsysctl save $(date +%Y-%m-%d_%H%M) -s								# Save ZFS Snapshot
sudo zfsflush.sh -s 1 -p bpool;sudo zfsflush.sh -s 1 -p rpool;sudo update-grub			# Clear All ZFS Snapshots
sudo zfs list -t snapshot										# List ZFS Snapshots
sudo zpool status											# Show zpool Status

Specifically, this was added:
tput rev;read -p "Action? " in;tput sgr0; apropos $in # List of commands for action taken

If you're not sure which command to use for a given action, this lets you figure it out... for instance, say you wanted to print... you'd run the command above, it would prompt "Action?", you'd type in "print", and it would give you the list of commands related to that action.

And this:
sudo xprop # Click to get window properties.

That's for tweaking the system look and can also be used for troubleshooting.

And this:
sudo cat /proc/meminfo # View memory stats

And this:
tput rev;read -p "Command? " in;tput sgr0;whatis $in # Action of a command

And this:
tput rev;read -p "File or Directory? " in;tput sgr0;whereis $in # Show location of file or directory

Two new changes:
gedit /home/$USER/.bashrc

I changed from this:

# 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

... to this:

# Trap the 'exit' command and mousing out of the terminal window.
trap 'history -cw && cd ~ && sudo cp .good_history .bash_history && sleep 3' SIGHUP EXIT

The SIGHUP part is supposed to trap when you click the button to exit the terminal.


I added the following commands to /home/$USER/.good_history:

uname -a												# Show kernel info
sudo netstat -natpve											# Show network connections

The netstat command is part of net-tools:
tput rev;read -p "Command? " in;tput sgr0;sudo dpkg -S */$in$* # Show which package a command belongs to
Command? netstat
net-tools: /bin/netstat


tput rev;read -p "Command? " in;tput sgr0;whatis $in # Action of a command
Command? netstat
netstat (8) - Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships


sudo apt search net-tools

net-tools/focal,now 1.60+git20180626.aebd88e-1ubuntu1 amd64 [installed]
  NET-3 networking toolkit

So now my /home/$USER/.good_history file is:

history -c && cd ~ && sudo cp .good_history .bash_history && exit					# Reset Terminal commands
uname -a												# Show kernel info
sudo netstat -natpve											# Show network connections
tput rev;read -p "Action? " in;tput sgr0;apropos $in							# List of commands for action taken
tput rev;read -p "Command? " in;tput sgr0;whatis $in                                                 # Action of a command
tput rev;read -p "Command? " in;tput sgr0;sudo dpkg -S */$in$*					# Show which package a command belongs to
tput rev;read -p "Package? " in;tput sgr0;sudo dpkg -L $in | xargs which				# Show which commands belong to a package
tput rev;read -p "File or Directory? " in;tput sgr0;whereis $in					# Show location of file or directory
compgen -c | sort | uniq										# Show all available commands
journalctl -b											# View Boot Logs
sudo top												# View Processes
sudo df -l												# Show file system stats
sudo cat /proc/meminfo										# View memory 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 xprop												# Click to get window properties.
sudo systemd-analyze critical-chain									# Boot Chain Analysis
sudo systemd-analyze blame										# Boot Startup Time Analysis
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
sudo blkid												# Drives UUID / Partitions PARTUUID
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;sudo update-grub			# Clear All ZFS Snapshots
sudo zfs list -t snapshot										# List ZFS Snapshots
sudo zpool status											# Show zpool Status
1 Like

An updated /home/$USER/.good_history file:

history -c && cd ~ && sudo cp .good_history .bash_history && exit					# Reset Terminal commands
uname -a												# Show kernel info
sudo netstat -natpve											# Show network connections
tput rev;read -p "Action? " in;tput sgr0;apropos $in							# List of commands for action taken
tput rev;read -p "Command? " in;tput sgr0;whatis $in                                                 # Action of a command
tput rev;read -p "Command? " in;tput sgr0;sudo dpkg -S */$in$*					# Show which package a command belongs to
tput rev;read -p "Package? " in;tput sgr0;sudo dpkg -L $in | xargs which				# Show which commands belong to a package
tput rev;read -p "File or Directory? " in;tput sgr0;whereis $in					# Show location of file or directory
compgen -c | sort | uniq										# Show all available commands
journalctl -b											# View Boot Logs
sudo top												# View Processes
sudo df -l												# Show file system stats
sudo cat /proc/meminfo										# View memory 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 xprop												# Click to get window properties.
sudo lsmod												# Show all loaded modules.
sudo systemd-analyze critical-chain									# Boot Chain Analysis
sudo systemd-analyze blame										# Boot Startup Time Analysis
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
sudo atop -d												# Drive Read/Write Stats
sudo blkid												# Drives UUID / Partitions PARTUUID
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;sudo update-grub			# Clear All ZFS Snapshots
sudo zfs list -t snapshot										# List ZFS Snapshots
sudo zpool status											# Show zpool Status