Typing the same command again and again can become tiresome. Here are a number of ways you can make repeating commands – or repeating commands but with some changes – a lot easier than you might expect.
Rerunning commands on the Linux command line isn’t hard, but there are a number of ways to do it. This post provides eight examples of how to repeat commands on Linux, with and without changes.
1. Repeat a previous command
Rerunning a previously entered command by typing !! is clearly the easiest way to repeat that command. You don’t have to type anything additional if you want to run the same command again. Here are a couple examples:
$ pwd
/home/myacct
$ !!
/home/myacct
$ ls -l | wc -l
218
$ !!
ls -l | wc -l
218
2. Repeat the previous command with a change
If you want to rerun the previous command, but with a change, you can specify the change that you want to make by supplying the original and the replacement portions of the command between ^ characters as shown in the example below.
$ echo this is a test
this is a test
$ ^test^trial^
echo this is a trial
this is a trial
3. Make use of command history
To rerun an earlier command from your command history, use the history command (which generally retains the last 1,000 commands that you’ve run) to list your history and then type ! followed by the number associated with that command. Here’s an example:
$ history | tail -2
1001 show_exit_codes
1002 ls -l | wc -l
$ !1002
ls -l | wc -l
218
You can also rerun a previous command by backing up to it in your command history: Press the up arrow key, and when you get to the command you want to rerun, simply press the return key to rerun it.
4. Modify and then rerun a command
After backing up to a previous command using the up arrow key, you can make changes to it before running it again. You can back up with the backspace key and then type the replacement portion of the command.
You can move to the beginning of the command using ^a or to the end using ^e. In the example below, I forgot to use the echo command and then went back to the beginning of the command with ^a and inserted the command.
$ Have a happy and very profitable day!
bash: Have: command not found...
$ echo Have a happy and very profitable day!
You can also use the left and right arrow keys to move left and right. Move back to the command with the up arrow key, and move left and right as needed. Once you’ve made your changes, press the return key to run the command you’ve just modified.
^a moves cursor to the beginning of the command, and ^e moves the cursor to the end of the command.
5. Use only the last string in the previous command
If you want to reuse the final argument from the previous command, you can refer to it as !$ in the next command. Here is an example:
$ echo this is not a test
this is not a test
$ echo !$
echo test
test
6. Use a reverse search to find a command you’ve run previously
To find a command in your command history that you want to rerun, you can hold the Ctrl key and press r. You will then be prompted to enter the search text. The most recent command containing that string will be run. The string can include more than a single word. The text of the original command will appear to the right as you type.
(reverse-i-search)`happy': echo Have a happy and very profitable day!
(reverse-i-search)`very profitable': alias byebye='echo "Have a happy and very profitable day!"'
7. Turn complex commands into aliases
When you run a command that you’re likely to use frequently, you might consider turning it into an alias. You can create an alias with a command like the one below, but this will only keep it working until you log out. Add it to your .bashrc file to make it available every time you log in. Here are some examples:
alias myprocs="ps -ef | grep `whoami`"
alias c="clear"
alias big5="du -h | sort -h | tail -5"
alias recent="history | tail -10"
Aliases not only save you time but also avoid typos.
8. Gauge your command history storage
To determine how many commands your history buffer will retain, run the command shown below.
$ echo $HISTSIZE
1000
You can change this by adding a command like this to your .bashrc file. The default value is stored in /etc/profile.
HISTSIZE=1234
Wrap-up
Reusing commands from your command history can save you a lot of time, especially when you’re running complex and somewhat repetitious commands.
More Linux tips: