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
- 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
May be interested
- How to set up Bluetooth in Linuxother distributions like fedora and arch linux require you to do some setup before you can fully enjoy the benefits of bluetooth on your computer. here is how you can set up bluetooth in linux.
- How to install Putty SSH Client on Ubuntu 20.04 LTSin this article, tipsmake.com will explain how to install putty on ubuntu 20.04 lts system. the same process can be used for debian, mint and earlier ubuntu releases.
- How to fix the problem of being unable to enter Terminal in Linuxnothing is perfect and sometimes, even terminal can stop responding or ignore what you type. if your terminal crashes and you can't enter it, here are some of the most common causes and solutions for them.
- How to monitor, clean and optimize Linux with Stacerlooking for ways to optimize your linux system and make it run faster? there are tons of ways and tools for you to do this, and stacer includes everything you need to monitor, clean, and optimize your linux installation.
- How to hide desktop icons on Ubuntu 20.04if you find hiding the top toolbar and side panel isn't enough and want everything to disappear from your ubuntu desktop, then you may want to get rid of those annoying icons from its surface.
- How to use ZFS Snapshots in Ubuntu 20.04ubuntu support for zfs is nothing new in version 20.04 lts. what's new is how easy it is to use one of the most important zfs features - snapshots.