How to use the history command in Linux
The history command holds a list of all the other commands that have been run since that Terminal session, then allows you to view or reuse those commands instead of re-entering them. If you are an experienced Terminal user, you will know the power of the history command. Let's find out more details through the following article!
See a list of the commands used
To see how the history command works, open a Terminal and type:
$ history
The response you receive should be something like this:
1 clear 2 ls -al 3 sudo dnf update -y 4 history
The history command holds a list of all other commands that have been run since the Terminal session
The history command shows you a list of commands entered since you started the session. The advantage is that you can now re-execute any option using the command:
$ !3
Command ! 3 at the prompt tells the shell to re-run the command on line 3 of the history list. You can also access that command by typing:
$ !sudo dnf
This will prompt the history to find the closest command matching the pattern you provided (in this case, that template is dnf ) and run the command.
Execute used commands
You can also use history to re-run the last command entered by typing !!. By pairing it with grep , you can either search for commands that match a sample of the text, or by using it with tail , you can find a couple of commands that are closest to you that you have executed. For example:
$ history | grep dnf 3 sudo dnf update -y 5 history | grep dnf $ history | tail -n 3 4 history 5 history | grep dnf 6 history | tail -n 3
Another way to access this search is to type Ctrl + R to invoke a recursive search for the command history. Once imported, the prompt will change to:
(reverse-i-search)`':
Now you can start typing a command, and the matching commands will be displayed for you to execute by pressing Return or Enter.
Executed command changes
You can also use history to re-run a command with a different syntax. You can edit history with the history command. For example, if you want to change the previous command history history | grep dnf to history | grep ssh , you can execute the following command at the prompt:
$ ^dnf^ssh^
The command is run again, but with dnf replaced with ssh. In other words, the command to be run is:
$ history | grep ssh
Clear history
There may be times when you want to delete some or all of the commands in your history file. If you want to remove a specific command, enter:
history -d row_number=""
To delete the entire contents of the history file, execute:
history -c
The history file is also stored in a file that you can modify. The Bash shell user found it in his home directory as .bash_history.
Some other uses
There are a few other things you can do with the history command:
- Sets the size of the history buffer to a certain number of instructions
- Record the date and time for each line in history
- Prevent some commands from being recorded in history
You should read it
- How to use the command history function in Command Prompt
- How to use the which command in Linux
- 20+ essential Linux security commands
- How to back up and restore Linux Terminal history
- How to view command history from previous PowerShell sessions in Windows 10
- How to use the stat command on Linux
- The Cat command in Linux
- What do you know about Linux distros?
- Tail command in Linux
- Del command in Windows
- The dd command in Linux, How to use the dd command
- 11 uses of ps command in Linux
Maybe you are interested
The Surprising History Behind Popular Halloween Costumes
How to Fix Clipboard History Error in Windows 11 Latest Update
8 Ways to Fix Clipboard History Stopped Working in Windows 11
The Little-Known Dark History of Time Capsules
How to Check FaceTime Call History on iPhone
10 strangest military vehicles in world history