# How to Repeat Your Last Command in Linux?

LinuxOpen SourceOperating System

#### TCP/IP Socket Programming HandsOn-Windows & Linux in C & C++

29 Lectures 9 hours

Linux terminal allows us to execute a variety of commands and sometimes it so happens that we find ourselves in a scenario where we might want to use a certain command more than once.

There are many occurrences where we would want to repeat the command that we just ran. In Linux, this can be done with the help of different commands, and in this tutorial, we will explore all such commands with the help of different examples.

For reference and simplicity, I will run a single command, say 'ls -ltr' and then use the different methods to search what was the last command that I ran.

The most basic approach to run the command that we just ran is to press the UPPER arrow key of the keyboard, and we will get the last command back in the terminal. However, there are multiple other ways with which we can repeat the last command. Let's understand them one by one with the help of examples.

## Repeating the Last Command Using Exclamation Marks

Yes, by using the exclamation marks, you can repeat the last command that you just typed on the terminal. Consider the command shown below.

!!


We just need to write the above command in the terminal, and the terminal will automatically output the last command that we used before the above command.

For reference, let's just suppose that we used the command 'ls –ltr', and then enter the above command in the terminal. Then, the output that we get after running the above command is shown below.

root@imml97:~# !!
ls -ltr
total 0


Notice how it tells us what command we used the last, and also gives us the output of that command as well.

## Using Hyphen and Prefix of Command

Now we will use the hyphen symbol and will also pass the number which will tell the compiler to give us the last command that we just used. Consider the following command −

!-1


We just need to write the above command in the terminal, and the terminal will automatically output the last command that we used before the above command.

## Using CTRL + P

You can use a combination of command (CTRL + P) to output the last command that you used in the terminal. You can use this combination multiple times to traverse through the entire set of commands that you used previously.

## Using CTRL + P and CTRL + R

You can use a combination of 'CTRL + P' and 'CTRL + R' to output the last command that you used on the terminal. And then, if you press the <Enter> button after this combination, you will also be able to run that command as well.

## Using 'fc' Command

You can also use the following 'fc' command to output the last command that you used on the terminal.

fc -s


We just need to write the above command on the terminal, and the terminal will automatically output the last command that we ran before the above command.

## Conclusion

In this tutorial, we used multiple examples to demonstrate how you can use different commands to get the last command that we used in the terminal.

Updated on 19-Jul-2022 12:41:58