To search for a command in your history and run it, type the following. Or, for example, you could rerun the previous command and pipe its output to grep to search for some text. This is particularly useful if you forget to add sudo before running a command. For example, you can type the following command to rerun the last command you ran through sudo, giving it root privileges. You can add anything you like before or after !! or any of the other expressions in this section. This expansion works anywhere on the line. For example, !-2 would run the second to last command you ran. You can also refer to a command a certain number of lines back.
For example, you could pipe it to the grepcommand to search your command history. You can do anything you like with the output. RELATED: Become a Linux Terminal Power User With These 8 Tricks The command with a “1” next to it is the oldest command in your bash history, while the command with the highest number is the most recent. You’ll see a list of all the commands in your bash history, along with a number to the left of each.
You can print your entire bash history to the screen by running a single command: history Ctrl+G: Leave the history searching mode without running a command.Ctrl+O: Run the command you found with Ctrl+R.Press this shortcut and start typing to search your bash history for a command. Ctrl+R: Recall the last command matching the characters you provide.Alt+R: Revert any changes to a command you’ve pulled from your history if you’ve edited it on the current line.īash also has a special “recall” mode you can use to search for commands you’ve previously run, rather than scrolling through them one by one.
Press the key multiple times to walk forwards through the commands you’ve used.