Tutorial: Shortcut to List all Unique Commands from your Recent Terminal History

The L5 by default is set to remember the last 1000 commands you’ve issued, so it can be tedious to run the history command and then look for that obscure and lengthy command you ran weeks ago, although the search box in the terminal can find it if you remember a word it contains.

I made a little utility to export the recent terminal history to a text file and list only the unique commands alphabetically, which whittles it down for easier reviewing. It consists of a short bash script, a desktop application to execute it, and an icon to launch it from the app screen.

1 The script (e.g. commands.sh):

#!/bin/bash

# List unique recent terminal commands, remove column 1 (item number) of history file.

HISTFILE=~/.bash_history
set -o history
history | nawk '{$1="";$0=substr($0,2)}1' | sort -u > history_unique.txt
nano history_unique.txt

2 The desktop application, which should be saved to .local/share/applications as BashHistory.desktop (or similar):

[Desktop Entry]
Name=BashHistory
Exec=gnome-terminal -x bash "commands.sh"
StartupNotify=true
Terminal=false
Type=Application
Categories=Utilities;
Icon=/home/purism/.local/share/icons/penguin_comp.png
Name[en_US]=BashHistory.desktop

3 The icon for the app screen should be saved to local/share/icons; I saved an image from a free clip-art website.

Top row, second from left:


Launching:

Text file is created here:

Mine contains about 250 unique commands out of the 1000 logged commands.

The launcher also opens the list of unique commands in the terminal for easy scrolling, copy, and paste.

See related thread.

6 Likes

Note that the bash history component itself has limited functionality for searching e.g. !s, and !? in various forms.

If you are just looking for lengthy commands then e.g.
history | grep -E '.{110,}'
and that could obviously be used to look for commands containing some string (or pattern).

The main use I see for your script is when you can’t remember much about the command but you feel that you will recognise it when you see it. :wink:

1 Like

I’m a big fan of ctrl+r for searching my history

5 Likes

And that’s usually the case with me. :rofl:

2 Likes