
{"id":76,"date":"2013-07-01T05:02:54","date_gmt":"2013-07-01T05:02:54","guid":{"rendered":"http:\/\/ubuntuhandbook.org\/?p=76"},"modified":"2024-04-01T06:52:46","modified_gmt":"2024-04-01T06:52:46","slug":"how-to-run-the-history-commands-quickly-and-easily","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2013\/07\/how-to-run-the-history-commands-quickly-and-easily\/","title":{"rendered":"How to Re-Run History Commands Quickly in Ubuntu"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-37909\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/03\/terminal-logo.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>Most Linux remembers the commands that you ran in terminal or command console. It&#8217;s useful for finding out what you&#8217;ve done to the system, especially for server.<\/p>\n<p>For frequently used command with a long code, you don&#8217;t need to type all the code again and again. You can re-run then using history commands option, by just a few key presses.<\/p>\n<h3>List history commands<\/h3>\n<p>To find out what commands you&#8217;ve run in your Linux, simply run command:<\/p>\n<pre>history<\/pre>\n<p>In the output, you&#8217;ll see a large list of commands that you ran before. And, each command has number at its beginning.<br \/>\n<a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-command.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46019\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-command-700x475.webp\" alt=\"\" width=\"610\" height=\"414\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-command-700x475.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-command-300x203.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-command-768x521.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-command.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>If you have too much history commands, it may be hard to browse and find out a certain command. In the case, add <code>grep<\/code> option to filter. For example, run command below to find out history commands that include <code>apt install<\/code>.<\/p>\n<pre>history | grep \"apt install\"<\/pre>\n<p>For choice, you may also add <code>-E<\/code> option after <code>grep<\/code> to filter with regular expressions.<br \/>\n<a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-grep.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46020\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-grep-700x494.webp\" alt=\"\" width=\"610\" height=\"430\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-grep-700x494.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-grep-300x212.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-grep-768x542.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-grep.webp 858w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Re-Run a History Command<\/h3>\n<p>Once you got the history command, that&#8217;s too long or you don&#8217;t remember, you can simply re-run it by typing <b>!number<\/b>, where number is the number in front of that command in history output.<\/p>\n<p>For example, run command below will re-run <code>sudo apt install gimp<\/code>, according to last screenshot. And, in terminal window it outputs what&#8217;s going to run immediately after you hitting Enter. If it&#8217;s a <code>sudo<\/code> command, it will also ask for password authentication.<\/p>\n<pre>!1057<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/rerun1057.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46021\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/rerun1057-700x263.webp\" alt=\"\" width=\"610\" height=\"229\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/rerun1057-700x263.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/rerun1057-300x113.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/rerun1057-768x288.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/rerun1057.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>To avoid careless mistakes, you may use the command below instead by adding <code>:p<\/code> in end to first preview that history command:<\/p>\n<pre>!1057:p<\/pre>\n<p>Then re-run <code>!1057<\/code> if you confirm it is indeed the command you want.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-preview.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-46022\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-preview-700x314.webp\" alt=\"\" width=\"610\" height=\"274\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-preview-700x314.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-preview-300x135.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-preview-768x345.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/history-preview.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<pre><\/pre>\n<p>For the last command, without finding out the history number, just use command below will re-run it:<\/p>\n<pre>!!<\/pre>\n<p>And, you can use it along with other commands. For example, re-run last command with <code>sudo<\/code> permission:<\/p>\n<pre>sudo !!<\/pre>\n<p>Or re-run last command, then poweroff:<\/p>\n<pre>!! &amp;&amp; poweroff<\/pre>\n<p>There are also more choices, for example, re-run the last command start with given string:<\/p>\n<pre>!sudo<\/pre>\n<p><i>The command will re-run the last command start with <code>sudo<\/code>.<\/i><\/p>\n<p>And, all the commands above can follow with <code>:p<\/code> to preview command before running.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/more-history-commands.webp\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/more-history-commands-700x427.webp\" alt=\"\" width=\"610\" height=\"372\" class=\"aligncenter size-large wp-image-46023\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/more-history-commands-700x427.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/more-history-commands-300x183.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/more-history-commands-768x468.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2013\/07\/more-history-commands.webp 786w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>Most Linux remembers the commands that you ran in terminal or command console. It&#8217;s useful for finding out what you&#8217;ve done to the system, especially for server. For frequently used command with a long code, you don&#8217;t need to type all the code again and again. You can re-run then using history commands option, by [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":37909,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11],"tags":[19],"class_list":["post-76","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-command-skills","tag-command-skill"],"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/76","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=76"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/76\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/37909"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=76"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=76"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=76"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}