Linux :: Working with Bash history

One of the best ways to increase your productivity is to learn to use the Bash history more effectively. With that in mind, perhaps one of the most important tweaks you can make in a multi-user environment is to enable the histappend option to your shell. To do that, simply run the following command:

shopt -s histappend

This allows multiple terminal sessions to write to the history at the same time. In most environments this option is not enabled. That means that histories are often lost if you have more than a single Bash session open (either locally or over SSH).

Another common task is to repeat the last command with sudo. For example, suppose you want to create a directory mkdir /etc/ansible/facts.d. Unless you are root, this command will fail. From what I have observed, most users hit the up arrow, scroll to the beginning of the line, and add the sudo command. There is an easier way. Simply run the command like this:

sudo !!

Bash will run sudo and then the entirety of the previous command. Here is exactly what it looks like when run in sequence:

[user@centos ~]mkdir -p /etc/ansible/facts.d
mkdir: cannot create directory ‘/etc/ansible’: Permission denied

[user@centos ~]sudo !!
sudo mkdir -p /etc/ansible/facts.d

When the !! is run, the full command is echoed out to the terminal so you know what was just executed.

Similar but used much less frequently is the !* shortcut. This tells Bash that you want all of the *arguments* from the previous command to be repeated in the current command. This could be useful for a command that has a lot of arguments you want to reuse. A simple example is creating a bunch of files and then changing the permissions on them:

[user@centos tmp]touch file1 file2 file3 file4
[user@centos tmp]chmod 777 !*
chmod 777 file1 file2 file3 file4

It is handy only in a specific set of circumstances, but it may save you some keystrokes.

Speaking of saving keystrokes, let’s talk about finding commands in your history. Most users will do something like this:

history |grep <some command>

However, there is an easier way to search your history. If you press

ctrl + r

Bash will do a reverse search of your history. As you start typing, results will begin to appear. For example:

(reverse-i-search)`hist': shopt -s histappend

In the above example, I typed hist and it matched the shopt command we covered earlier. If you continue pressing ctrl + r, Bash will continue to search backward through all of the other matches.

Our last trick isn’t a trick as much as a helpful command you can use to count and display the most-used commands in your history.

[user@centos tmp]history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr | head
81 ssh
50 sudo
46 ls
45 ping
39 cd
29 nvidia-xrun
20 nmap
19 export

In this example, you can see that ssh is by far the most-used command in my history at the moment.

Leave a Reply

Your email address will not be published. Required fields are marked *

*