Getting work done faster on the command line is one of the never changing goals of Unix sysadmins. And one way to do this is to find easy ways to reuse commands that you have entered previously \u2013 particularly if those commands are complex or tricky to remember. Some of the ways we do this include putting the commands in scripts and turning them into aliases. Another way is to reissue commands that you have entered recently by pulling them from your command history and reusing them with or without changes.\n\n\u00a0\n\nThe easiest and most intuitive way to reissue commands is by using the up and down arrows on your keyboard to scroll through previously entered commands. How far back you can scroll will depend on the size of your history buffer. Most people set their history buffers to hold something between 100 and 1,000 commands but some go way beyond that. Hitting the up arrow 732 times might try your patience, but there are are fortunately easy ways to get what you need without wearing out your finger tip! To make this post a little easier to follow, I'm using a modest HISTSIZE setting. You can view your current history queue size using the command shown below\n\n\n$ echo $HISTSIZE\n1000\n\n\nThe easiest thing to do with command history is repeating your previous command. Either pressing the up arrow or typing !! will repeat do that. Most of the time, however, this little trick is not the most useful.\n\n\n$ date\nFri Sep 30 11:46:06 EDT 2016\n$ !!\ndate\nFri Sep 30 11:46:08 EDT 2016\n\n\nYou can also rerun your previous command but with a change by specifying the old and new strings between circumflex (^) marks like this:\n\n\n$ du -sk temp\n9168 temp\n$ ^temp^bin^\ndu -sk bin\n36 bin\n\n\nThis trick can be particularly useful when the command you want to reuse is long and complicated.\n\n\nYou can always press the up arrow on your keyboard multiple times to get to back to previous commands but, if the command you want to reuse was one you entered 1,234 commands ago, it might be easier to use the first part of the command following an exclamation point like this:\n\n\n# !tail\ntail -2 \/var\/log\/messages\nSep 30 15:56:14 ip-172-33-0-28 su: (to root) shs on pts\/0\nSep 30 15:57:54 ip-172-33-0-28 su: (to root) shs on pts\/0\n\n\nYou can rerun the last cat command by simply typing !cat and the last grep command by typing !grep and the last command you issues through sudo by typing !sudo.\n\n\n$ !sudo\nsudo tail -12 \/var\/log\/messages | grep bound\nSep 30 14:44:44 ip-172-30-0-28 dhclient: bound to 172.30.0.28 -- renewal in 1568 seconds.\nSep 30 15:10:54 ip-172-30-0-28 dhclient: bound to 172.30.0.28 -- renewal in 1459 seconds.\nSep 30 15:35:15 ip-172-30-0-28 dhclient: bound to 172.30.0.28 -- renewal in 1705 seconds.\nSep 30 16:03:42 ip-172-30-0-28 dhclient: bound to 172.30.0.28 -- renewal in 1712 seconds.\n\n\nHow much you need to type will depend on how many commands you've typed since the target command and how unique the command is. If the command you want to reuse is one you issued a couple days ago, you will probably need to reenter more of it to make sure you repeat the correct command.\n\n\nYou can also reissue any command that's in your history buffer by typing ! followed by the command number.\n\n\n$ history\n 2 echo 4\n 3 echo 5\n 4 echo 6\n 5 echo 7\n 6 echo 8\n 7 echo 9\n 8 echo 10\n 9 history\n 10 vi .bashrc\n 11 history\n$ !7\necho 9\n9\n\n\nWhile !! makes it easy to repeat your previous command, you can also rerun previous commands by specifying how far back into your history you want to go. The command preceding your last command would be rerun by entering !-2.\n\n\n$ uptime\n 12:06:03 up 418 days, 2:36, 1 user, load average: 0.00, 0.01, 0.05\n$ who\nec2-user pts\/0 2016-09-30 12:02 (22.214.171.124)\n$ !-2\nuptime\n 12:06:11 up 418 days, 2:36, 1 user, load average: 0.00, 0.01, 0.05\n\n\nIf you can remember what command you typed 11, 123, or some other number of commands ago, you're a lot better at that than I. For most of us, it's probably a lot easier to use history to find the command you want to reuse and then running at again with its command number.\n\n\n$ history | grep sudo\n 12 sudo tail -4 \/var\/log\/messages\n 13 history | grep sudo\n$ !12\nsudo tail -4 \/var\/log\/messages\nSep 30 15:57:54 ip-172-33-0-28 su: (to root) shs on pts\/0\nSep 30 16:03:40 ip-172-33-0-28 dhclient: DHCPREQUEST on eth0 to 126.96.36.199 port 67 (xid=0x7e717059)\nSep 30 16:03:40 ip-172-33-0-28 dhclient: DHCPACK from 188.8.131.52 (xid=0x7e717059)\nSep 30 16:03:42 ip-172-33-0-28 dhclient: bound to 184.108.40.206 -- renewal in 1712 seconds.\n\n\nYou can also rerun the last command that contained a particular string \u2013 maybe a file name or a username \u2013 by preceding that string with !?.\n\n\n$ !?messages\nsudo tail -12 \/var\/log\/messages | grep bound\n\n\nThat particular trick can come in very handy when you want to issue multiple commands against the same file, for example.\n\n\nThe syntax for reusing only the first argument from a previous command is a bit tricky, but here'a an example. In this command sequence, we run a command and then use a history trick to reuse just the first argument in another command:\n\n\n$ touch file1 file2 file3\n$ cat !touch:^\ncat file1\na:one:1\nb:two:2\nc:three:3\n\n\nAs you can see, we put the string you are looking for between the ! and :^.\n\n\nWe can do something similar for 2nd, 3rd, etc. arguments by using the syntx !command:# where # is the number of the argument you want to reuse.\n\n\n$ touch file1 file2 file3\n$ tail !touch:2\ntail file2\na:one:1\nb:two:2\nc:three:3\nd:four:4\ne:five:5\n$ tail !touch:3\ntail file3\nthis file is empty\n\n\nSaving significant time by reusing commands through history tricks is great but you have to remember some syntax which is anything but obvious.