How To Search Shell Command History

How To Search Shell Command History

To search the command history in a Bash shell, you can use the history command with the grep command.

For example, to search for all commands that contain the string foo, you can use the following command:

history | grep foo
Sou‮cr‬e:www.lautturi.com

This will print a list of all commands in the history that contain the string foo.

You can also use the -i option with grep to perform a case-insensitive search. For example:

history | grep -i foo

This will print a list of all commands in the history that contain the string foo, regardless of the case.

You can also use the -C option with grep to display a context around the matching lines. For example:

history | grep -C 2 foo

This will print two lines before and two lines after each matching line, to provide context around the matching lines.

To search the command history in the current shell session only, you can use the HISTCONTROL variable to disable storing commands in the history file. For example:

HISTCONTROL=ignoreboth

This will disable storing commands in the history file and in the memory of the current shell session. You can then use the history command to search the current shell session's history without interference from the history file.

Keep in mind that the history command only works for the current user and does not include the history of other users. To search the history of other users, you will need to have access to their history files, which are typically stored in the ~/.bash_history file.

Created Time:2017-10-29 22:08:31  Author:lautturi