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 use the last command in Linuxwant to know who, what time and which device to access your linux computer? please read the following article.
- How to use ss command on Linuxthe ss command is a new alternative to the classic netstat. you can use it on linux to get data about network connections. here's how to work with this helpful tool.
- 11 uses of ps command in Linuxfor system administrators, ps is a frequently used tool. the ps command is used to list the processes currently running on the system, with many available filtering and display modes via flags and arguments.
- Instructions for using zforce command on Linuxthe gzip command is a popular tool used to compress / decompress files in linux. tipsmake.com presents the basics of this tool in the article: some basic terminal commands in linux mint 11.
- How to limit access to su command in Linuxif you have added linux to your data center or are just using a single linux machine for your business, you need to make sure it is as secure as possible.
- 12 things Linux is easier to do in the command line than graphical softwaregraphical user interfaces (guis) were invented to make life easier for computer users, so it's very common to use them to perform most everyday tasks.
- 11 df commands in Linux (with example)with the df command in linux, you can easily check your system for details about disk space and file size. these 12 examples of df commands will show you how to do them in detail.
- How to use the Linux command line on Android with Termuxandroid is a very operating system 'capacity with more and more desktop accessibility applications. however, sometimes you want to make some things on android that can be as easy as desktop. fortunately, you can use the termux tool, which builds on the existing infrastructure and provides a command line environment that allows you to install real linux applications on your android device.
- Instructions for using find command in Linuxthe find command is one of the most important and handy commands on a linux system. as its name suggests, the command can find files on a linux pc based on a variety of conditions and variables you set.
- Instructions for using pstree command on Linuxpstree is a powerful and useful command to display processes running in linux. like the ps command, it shows all the processes that are currently active on your login system. the main difference is that when running the pstree command, processes are organized into tree sorting instead of lists like using the ps command.