

#WINDOWS BASH GREP TO SEARCH HISTORY PASSWORD#
In this example the user accidentally left their MySQL password in the bash history at line 121, which we then remove with the -d option and specify the line number to remove. We could also delete or otherwise remove the contents of the ~/.bash_history file, however keep in mind that the current history is written to the file at log out, so if you delete the file then log out the history of your current session will still be saved.Ĭlearing the whole history file may be overkill, we can instead delete a specific line number from the history file with the -d ~]# history | grep passwordġ21 Sun 03:33:11 AM PDT mysql -u root -p oops_this_is_my_passwordġ22 Sun 03:33:19 AM PDT history | grep ~]# history -d ~]# history | grep passwordġ21 Sun 03:33:19 AM PDT history | grep passwordġ23 Sun 03:33:29 AM PDT history | grep password bash_history file immediately by running ‘history -w’ afterwards. Note that this will only clear the history in memory, the changes will be written when the user logs out however we can save the changes to the. We can clear all contents of the history file with the -c ~]# history -c We can force the current history to write to the users ~/.bash_history file with the -w ~]# history -w Usually the history file is written to upon logout, therefore if you have an SSH session that has timed out you will not have your history from that session when you log back in. By outputting to grep we can search for commands that have been run ~]# history | grep httpd When piping into less we can scroll through the output of the history file rather than having it all output to the terminal. We can of course pipe the output of the history command into many other useful commands, such as less or grep. This has not actually performed the restart, it merely displays the command. You can run this with ‘:p’ on the end to instead print the command rather than execute it straight ~]# !systemctl:p While useful, this can obviously be dangerous if the last command is actually different from what you expect. This is done with !string, where string is the start of a previously executed ~]# systemctl start ~]# systemctl stop ~]# systemctl restart ~]# !systemctlĪs shown the most recent command that started with ‘systemctl’ has been run again. We can repeat the last command starting with a specified string. Note that the line numbers can change, especially if your history file fills up, so don’t rely on the same number always pointing to the same command.

In this example, the ‘date’ command was the 101st line in the history file, and we can run it again with ‘!101’. It is possible to repeat a command by specifying its line ~]# history 2

The most recent command can be executed simply by entering ~]# dateĪlternatively you can simply press the ‘up’ arrow key to display the last command and then press enter to execute it.Īs shown above, the bash history command displays line numbers. While the default is to print all history lines, you can specify a number after the history command to output this amount of the most recent ~]$ history 3

You could also run ‘cat ~/.bash_history’ which is similar but does not include the line numbers or formatting. The history is stored in the ~/.bash_history file by default. Commands are numbered, with older commands at the top and newer commands at the ~]$ history In its most simple form, you can run the ‘history’ command by itself and it will simply print out the bash history of the current user to the screen.
#WINDOWS BASH GREP TO SEARCH HISTORY HOW TO#
How To Use Bash History – Command Examples It’s also useful if you’ve run something before and forgot the command, I can’t begin to tell you the number of times that I’ve done this! The ‘history’ command available in Bash can be used to simply display your shell history, however there’s also a whole lot more that you can do with it, which we’ll demonstrate here.īash history allows us to quickly see what has been executed previously on a system, allowing you to hold users at least somewhat accountable for their actions (more on this later).
