Table of Contents
This tip is about the how to Use Command History on Linux. So read this free guide, How to Use Command History on Linux step by step. If you have query related to same article you may contact us.
How to Use Command History on Linux – Guide
Each of our administrations is currently running on Linux. On Linux, there is an exceptionally valuable command to show you each of the last commands that have been used recently. The command is just called history, but it can also be obtained by taking a look at your .bash_history in your home folder. By default, the history command will show the last 500 commands entered.
In that sense, today we’re going to look at a command that allows us to view each of the commands we’ve already used and use them again. From the title, you would have effectively thought about what the command is, wouldn’t you?
First, the history command is not really a command. You can see this for yourself by looking for the command on your system:
$ which history where: no history (/ 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. As it’s written in the shell you’re using, there may be some variation in how the history behaves depending on whether you’re using Bash, tcsh, Zsh, dash, fish, ksh, and so on. This article is based on the Bash implementation of history, so some functions may not work in other shells. However, most of the basic functions are the same.
Story 101
To see history in action, open a terminal program in your Linux installation and type:
$story
Here’s the answer I got:
1 clear2 ls -al3 sudo dnf update -y4 history
The history command shows a list of commands entered since the beginning of the session. The joy of the story is that you can now play any of them using a command like:[[[[
$! 3
The command! 3 at the prompt tells the shell to rerun the command in line 3 of the history list. I could also access this command by typing:
$! sudo dnf
This makes the history look up the last command that matches the given pattern (in this case, that pattern is dnf) and execute it.
Searching history
You can also use history to rerun the last command 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 executed commands. For example:
$history | grep dnf3 sudo dnf update -y5 history | grep dnf
$history | tail -n 34 history5 history | history of grep dnf6 | tail -n 3
Another way to get this search functionality is by typing Ctrl-R to invoke a recursive search of your command history. After typing this, the prompt changes to:
(reverse search) `’:
Now you can start typing a command and the corresponding commands will be displayed for you to execute by pressing Return or Enter.
Change an executed command
You can also use history to rerun a command with different syntax. You can review the story with the story. For example, if I want to change my previous command history | grep dnf for history | grep ssh, I can run the following at the prompt:
$^dnf^ssh^
The command is run again, but with dnf replaced by ssh. In another words, this command is executed:
$history | grep ssh
Removing history
There may come a time when you want to remove some or all of the commands from your history file. If you want to delete a specific 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 like .bash_history.
Final note
I hope you like the guide How to Use Command History on Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.