This tutorial is about How to Use Command History on Linux. Recently I updated this tutorial and will try my best so that you understand this guide. I hope you guys like this blog, How to Use Command History on Linux. If your answer is yes after reading the article, please share this article with your friends and family to support us.
Check How to Use Command History on Linux
Each of our administrations currently runs on Linux. On Linux, there is an exceptionally valuable command to show you each of the latest commands that have been used lately. The command is simply called history, but it can also be accessed by taking a look at your .bash_history in your home folder. By default, the history command will show you the last 500 commands you have entered.
In this sense, today we will examine a command that allows us to see each of the commands that we have already used and to use them again. From the title, you would have effectively thought about what the command is, right?
First of all, the history command is not actually a command. You can see this for yourself by searching for the command on your system:
$ which historywhich: no history in (/ usr / local / bin: / usr / bin: / bin: / usr / games: / usr / local / sbin)
Your computer cannot find the history command because it is a keyword built into your shell. Because it is written in the shell you are using, there may be some variation in how the history behaves depending on whether you are using Bash, tcsh, Zsh, dash, fish, ksh, etc. This article is based on the Bash history implementation, so some functions may not work in other shells. However, most of the basic functions are the same.
Story 101
To see the history in action, open a terminal program on your Linux installation and type:
$ history
This is the answer I got:
1 clear2 ls -al3 sudo dnf update -y4 history
The history command displays a list of the commands entered since the session started. The joy of the story is that now you can play any of them using a command like:[[[[
$! 3
The command! 3 at the prompt tells the shell to rerun the command on line 3 of the history list. You could also access that command by entering:
$! sudo dnf
This asks the history to find the last command that matches the pattern you provided (in this case, that pattern is dnf) and run it.
Looking for history
You can also use history to rerun the last command you entered by typing !!. By pairing it with grep, you can search for commands that match a text pattern or, using it with tail, you can find the last commands you ran. For instance:
$ history | grep dnf3 sudo dnf update -y5 history | grep dnf
$ history | tail -n 34 history5 history | grep dnf6 history | queue -n 3
Another way to access this search function is by typing Ctrl-R to invoke a recursive search of your command history. After writing this, the message changes to:
(reverse-i-lookup) ”:
Now you can start typing a command, and the matching commands will be displayed for you to execute by pressing Enter or Enter.
Change an executed command
You can also use history to rerun a command with a different syntax. You can review the story with the story. For example, if I want to change my previous command history | grep dnf to history | grep ssh, I can run the following at the prompt:
$ ^ dnf ^ ssh ^
The command is rerun, but dnf is replaced by ssh. In other words, this command is run:
$ history | grep ssh
Deleting history
There may come a time when you want to delete some or all of the commands in your history file. If you want to remove a particular command, enter history -d
The history file is stored in a file that you can also modify. Bash shell users find it in their home directory as .bash_history.
Final remarks: How to Use Command History on Linux
I hope you understand this article, How to Use Command History on Linux. If your answer is no, you can ask anything via the contact forum section related to this article. And if your answer is yes, please share this article with your friends and family to give us your support.