Skip to main content

Searching your Bash history in Linux

·362 words·2 mins
system-administration tech bash debian grep history linux sysadmin technology
James Pettigrove
Author
James Pettigrove
Cloud Engineer with a focus on Microsoft Azure

Ever spent endless hours, days and nights, deep inside your Bash shell in your Linux distribution of choice and ever wonder “I know I have ran a command to solve this solution but cannot for the life of me remember it”? Using your up/down arrow keys can only get you through so much of your history before it gets pointless, especially if the command was ran a long time ago.

Thankfully in Bash, there is a command for that.

History command outputs the contents of the hidden_.bash_history_ file that is located in each users folder in a numbered form.

The brilliance of this number format is that it provides to us the convenient ability to quickly execute a command from your Bash history using the following convention:

!NumberOfCommandToExecute

So, if we take from the example output above and wanted to execute the cat of my network interfaces file at line 792, I would do the following:

!792

Very handy indeed.

So what if you knew you executed the command you want very recently and didn’t want to list the 800+ entries of your Bash history like mine outputted? Well the history command will display the last X amount of entries simply by typing:

history x

So the last 10 entries would look like:

So overall a pretty powerful command in your Linux tool set.

But none of this solves our original scenario. This is where our friend grep, which we explored in a previous post, comes into play.

Often after a period of time I want to reevaluate what I am getting to startup upon my Raspberry Pi booting. Now, I know that there is a file where I defined this with the term ‘rc’ in the file name but often forget the full name and/or where it is located. Perfect use case for combining with a pipe history and grep.

If we do the following:

history | grep rc

we should be given exactly what I am after. Low and behold:

/etc/rc.local is exactly what I am looking for.

As you can see, using grep to trawl your Bash history is a powerful partnership in a Linux SysAdmin’s belt of magic.

Related

Linux Foundation Certification
·330 words·2 mins
system-administration tech centos certification debian linux opensuse red-hat suse sysadmin technology ubuntu
During my quest to expand my knowledge on Linux thanks to edX course LFS101x Introduction to Linux, I happened upon the announcement from the Linux Foundation (who also run the edX course) that there is now two certification for Linux;
Let's get certified!
·393 words·2 mins
system-administration tech certification cloud containers docker gcp google-cloud k8s kubernetes learning lfcs linux sysadmin technology
It’s been a long time between drinks for certifications that I have achieved personally.
Finding a string across an entire filesystem in Linux
·435 words·3 mins
system-administration tech grep linux raspberry-pi raspbian technology
After coming back from a extra long holiday and noting that I hadn’t received any monitor tweets in a while from my Raspberry Pi, I thought it was a good time to login and make sure everything was running as before.