Table of Contents
How to Re-Run Last Executed Commands in Linux: Remember that one command that made everything better? It could have been cat, less, more, wc, or something else. When we’re at the terminal, we can type a lot of commands to solve a problem, and our Linux OS keeps track of them in a file called “history.”
In this how-to, we’ll look at different ways to search our command history and use it again. As you get used to these commands, you should make sure you don’t accidentally issue a command that could cause problems. Take your time when using these new techniques, and double-check the details before pressing enter.
Repeat Last Command using Exclamation Marks
- This will execute the last command. Sample output would be:
- $ !!
Re-execute Previous Commands using reverse-search
You can also use CTRL+R to search through your command line history. To look through the command line history, press the CTRL and R keys together. This is the way I like to do things. It searches the history in a way that you can control, which I think is safer than just blindly running commands from the BASH history.
Take a look at the example below. In the following example, we searched for “ost,” which brought up the last command “sudo netctl restart ostechnixjio” in the history that had the word “ost” in it. Then we press ENTER to run the command right away, or I can use the right arrow key to finish the command and then press ENTER to run it.
Add Last Used Arguments in a Command using ALT+period Key
Re-Run Last Executed Commands in Linux, As one of our readers said in the comments section, we can also use the ALT+. keys (press ALT+period) to get the last command without running it. But there is one thing that can’t be done. When you press ALT+., you can only get the last argument used in the command. For example, if your last command was “uname -r,” it will only bring back the “-r” argument and not the whole command.
FAQs
To enter this mode you simply press ctrl and r. You can then enter a search term and use repeat presses of ctrl and r to step back through the list of previous commands containing that term. When you find a command you want to reissue press enter.
One easy way to reuse a previously entered command (one that’s still on your command history) is to type the beginning of the command. If the bottom of your history buffers looks like this, you could rerun the ps command that’s used to count system processes simply by typing just ! p.
Explanation: Using (.) operator, we can simply repeat the last command that was used. The principle behind repeat command is, use the actual command once and then repeat it at other places with dot (.)