Skip to content

Instantly share code, notes, and snippets.

@BlogBlocks
Last active December 19, 2017 00:36
Show Gist options
  • Select an option

  • Save BlogBlocks/5e133f13aad9552497a74c3b31822ba9 to your computer and use it in GitHub Desktop.

Select an option

Save BlogBlocks/5e133f13aad9552497a74c3b31822ba9 to your computer and use it in GitHub Desktop.
"""
Description: find terminal command in bash history
Purpose created: I had run several files using the word 'cfdg' in the command.
This was several months ago. I wanted to retrieve all lines in my bash history
where that command was used.
If used remember to change the user to your name.
This script will create a file 'using-(whatever search term used).txt'
in this case using-cfdg.txt. The first line of the print will be the filename generated
again in this case it will print- FILENAME IS : using-cfdg.txt
then it will start printing the history line by line pausing slightly because of the sleep(.5)
At at time this may be run, even with the same search term. The old file will be replaced
and updated if the search term is reused.
Example: I wanted to find every occurance of docker used in a terminal command.
Just enter docker in the input box.
My return was.
FILENAME IS : using-docker.txt
gedit jack-install-docker.html
sudo nano /etc/apt/sources.list.d/docker.list
sudo apt-get purge lxc-docker
apt-cache policy docker-engine
sudo apt-get install docker-engine
"""
from time import sleep
# To use only one search term that does not require an input
# The ' search = raw_input("Search History :") ' line maybe commented out
# then use something simialar to the lines below.
#search = 'cfdg'
#search = 'docker'
search = raw_input("Search History :")
fileout = "using-"+search+".txt"
file0 = open(fileout, "w");file0.close()
file0 = open(fileout, "a")
#for history in open('/home/user/.bash_history', "r")
for history in open('/home/jack/.bash_history', "r"):
lines = history.split("/n")
for line in lines:
if search in line:
file0.write(line)
fileout = "using-"+search+".txt"
print "FILENAME IS : ",fileout,"\n"
file0 = open(fileout, "r")
lines = file0.readlines()
for line in lines:
sleep(.5)
print line
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment