Table of Contents
This tip is about the how to Utilise History Command on Linux. So read this free guide, How to Utilise History Command on Linux step by step. If you have query related to same article you may contact us.
How to Utilise History Command on Linux – Guide
Most Linux server administrators spend a lot of time on the command line. So the history command is one of the essential commands for them. BASH stands for (Born Again Shell) is the standard command line shell used on most major Linux distributions to interact with the operating system. If you spend a lot of time on the command line, viewing a history of commands you’ve run before can be helpful. feature that can make your day-to-day a lot easier and improve your productivity. we will talk about the history command, which allows you to view a list of commands already executed, search the list and manipulate the history file.
The GNU history command maintains a list of all other commands that have been run from that terminal session and allows you to reproduce or reuse those commands instead of retyping them. If you’re a seasoned terminal user, you know the power of history, but for us beginners or new system administrators, history is an immediate productivity gain.
First, the history command is not really a command. You can see this for yourself by looking 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 an internal keyword of your shell. As 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 and so on. 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 in your Linux installation and type:
$ history
Here’s the answer I got:
clearls -alsudo dnf update -yhistory
The history command shows a list of commands entered since you started the session. The joy of the story is that you can now play any of them using a command like:
$ !3
The !3 command at the prompt tells the shell to rerun the command on line 3 of the history list. I could also access this command by typing:
$ !sudo dnf
This asks the history to look for the last command that matches the given pattern (in this case, that pattern is dnf) and execute it.
search history
You can also use history to rerun the last command typed by typing !!. By pairing it with grep you can look for commands that match a pattern of text, or using it with tail you can find the last commands executed. For example:
$ history | grep dnfsudo dnf update -yhistory | grep dnf
$ history | tail -n 3history | grep dnfhistory | tail -n 3
Another way to access 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-i-search)`’:
Now you can start typing a command and the corresponding commands will be displayed for you to execute by pressing Return or Enter.
Changing 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 from the prompt:
$ ^dnf^ssh^
The command runs again, but with dnf replaced with ssh. In others 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 exclude a specific command, type history -d
Final note
I hope you like the guide How to Utilise History Command 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.