{"id":17623,"date":"2022-06-04T05:21:02","date_gmt":"2022-06-04T05:21:02","guid":{"rendered":"https:\/\/linuxways.net\/?p=17623"},"modified":"2022-09-05T18:06:37","modified_gmt":"2022-09-05T18:06:37","slug":"how-to-check-your-command-history-in-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/how-to-check-your-command-history-in-ubuntu-20-04\/","title":{"rendered":"How to Check Your Command History in Ubuntu 20.04"},"content":{"rendered":"<p>In their day-to-day operations, system administrators can execute countless commands on the servers to accomplish their goals. They could be running hundreds of commands in each session, and each session could differ from the last. This makes it very hard to remember all of the commands you\u2019ve executed in the past. But fret not, unknown to many people, whenever they are typing away at their terminal, Linux is recording all of the executed commands in a file.<\/p>\n<p>In this guide, I will walk you through the commands needed to look up our command history and reuse commands from there. While you get used to these commands, ascertain that you don\u2019t unintentionally execute a command that could cause issues for you. That said, take your time with these commands and practice them in a sandbox or virtual machine, if possible before you execute them in a live environment.<\/p>\n<p>Though these commands will work with most Linux distributions, for the purposes of this guide, I will show you these commands using Ubuntu 20.04.<\/p>\n<h2>Scrolling through recent commands<\/h2>\n<p>The simplest way to lookup your commands is to use the up and the down arrow keys in the terminal. You can cycle through recent commands like this.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"739\" height=\"489\" class=\"wp-image-17624\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-1.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-1.png 739w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-1-300x199.png 300w\" sizes=\"auto, (max-width: 739px) 100vw, 739px\" \/><\/p>\n<p>When you find your desired command, all you have to do is press \u201cEnter\u201d.<\/p>\n<h2>\u201cHistory\u201d command<\/h2>\n<p>Running the \u201chistory\u201d command in your terminal simply lists the last thousand commands executed in the terminal. You get each command prefixed with a number.<\/p>\n<pre><strong><em>$history<\/em><\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"379\" height=\"96\" class=\"wp-image-17625\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-2.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-2.png 379w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-2-300x76.png 300w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/p>\n<p>Running the history command with the \u201cx\u201d number after it will list the last \u201cx\u201d commands executed. Like if we run the following, we get the last 2 commands executed.<\/p>\n<pre><strong><em>$history 2\u00a0<\/em><\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"293\" height=\"56\" class=\"wp-image-17626\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-3.png\" \/><\/p>\n<p>You can change the number to any number you desire, e.g., 20, 30, 40 or anything.<\/p>\n<h2>Executing a previous command<\/h2>\n<p>Now that we know about the \u201chistory\u201d command, you can simply re-execute a command using the number before it, as such. Let&#8217;s run history.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"379\" height=\"517\" class=\"wp-image-17627\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-4.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-4.png 379w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-4-220x300.png 220w\" sizes=\"auto, (max-width: 379px) 100vw, 379px\" \/><\/p>\n<p>Now out of all these commands, say I want to run the second command. All I need to do is get the number and run it like this:<\/p>\n<pre><strong><em>$!2<\/em><\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"483\" height=\"225\" class=\"wp-image-17628\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-5.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-5.png 483w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-5-300x140.png 300w\" sizes=\"auto, (max-width: 483px) 100vw, 483px\" \/><\/p>\n<p>Please remember that there is no space between the exclamation mark and the number.<\/p>\n<h2>Enhanced search using grep<\/h2>\n<p>Up till now, we have been using simple commands as an example. Let\u2019s say you are working with lengthy commands and you only partially remember them. In order to find out all of the commands using a specific keyword, you can use the grep command in conjunction with history to pull up all relevant commands.<\/p>\n<p>Let\u2019s try pulling up commands where our command contains \u201c.com\u201d in it somewhere:<\/p>\n<pre><strong><em>$history | grep .com<\/em><\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"388\" height=\"133\" class=\"wp-image-17629\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-6.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-6.png 388w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-6-300x103.png 300w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/p>\n<p>Here you will see your keyword highlighted in red.<\/p>\n<h2>Using the built-in reverse search<\/h2>\n<p>Another way you can search for previously used commands is through the reverse search built into the terminal. To get to it, all you need to do is just press \u201cCtrl + r\u201d.<\/p>\n<p>Doing so, you will see your terminal get updated to:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"272\" height=\"100\" class=\"wp-image-17630\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-7.png\" \/><\/p>\n<p>Now you can start typing your desired keyword and you will see the terminal returning with relevant matches. So, let\u2019s try to go back to the start and pull up hostname related commands.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"350\" height=\"79\" class=\"wp-image-17631\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-8.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-8.png 350w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-8-300x68.png 300w\" sizes=\"auto, (max-width: 350px) 100vw, 350px\" \/><\/p>\n<p>By pressing \u201cCtrl + r\u201d again you will cycle through all the results.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"550\" height=\"88\" class=\"wp-image-17632\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-9.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-9.png 550w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-9-300x48.png 300w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><\/p>\n<p>Now in order to run any of these commands, you can press the \u201cEnter\u201d key to execute the command of your choice.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"481\" height=\"203\" class=\"wp-image-17633\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-10.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-10.png 481w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-10-300x127.png 300w\" sizes=\"auto, (max-width: 481px) 100vw, 481px\" \/><\/p>\n<h2>Quickly executing the previous command<\/h2>\n<p>With practice comes enough confidence to remember the last command you\u2019ve executed. If you do remember the last command and know that it won\u2019t create any issues for you, you can quickly run it again with:<\/p>\n<pre><strong><em>$!!<\/em><\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"661\" height=\"151\" class=\"wp-image-17634\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-11.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-11.png 661w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-11-300x69.png 300w\" sizes=\"auto, (max-width: 661px) 100vw, 661px\" \/><\/p>\n<p>As you can see here I ran a simple \u201cls\u201d. Running \u201c!!\u201d tells you which command was run last and then executes it again. Now at times, it might be a command which requires elevated privileges, you can run that command as:<\/p>\n<pre><strong><em>$sudo !!<\/em><\/strong><\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"97\" class=\"wp-image-17635\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-12.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-12.png 649w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-12-300x45.png 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<h2>Excluding commands from history<\/h2>\n<p>Now if you want to keep your commands from being included in the terminal history, you can add a space before it and the terminal will not save it.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"649\" height=\"358\" class=\"wp-image-17636\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-13.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-13.png 649w, https:\/\/linuxways.net\/wp-content\/uploads\/2022\/06\/word-image-17623-13-300x165.png 300w\" sizes=\"auto, (max-width: 649px) 100vw, 649px\" \/><\/p>\n<p>As you can see, I executed \u201cls\u201d after \u201chostnamectl\u201d, but when I pulled up the history, the \u201cls\u201d command at the second last place is not included in the history.<\/p>\n<h2>Conclusion<\/h2>\n<p>With some practice, these commands become instinctive to use in your daily operations and can make your time with the terminal more effective and efficient. Through these commands, you get the ability to identify and re-execute a rarely used command, or a command that was difficult to arrange\/remember in the first place.<\/p>\n<p>As always, if you run into any issues with these commands or want to know more about them, feel free to drop us a comment below and we\u2019ll walk you through any issue or error.<\/p>","protected":false},"excerpt":{"rendered":"<p>In their day-to-day operations, system administrators can execute countless commands on the servers to accomplish their goals. They could be running hundreds of commands in each session, and&hellip;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-17623","post","type-post","status-publish","format-standard","hentry","category-centos"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17623","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=17623"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/17623\/revisions"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=17623"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=17623"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=17623"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}