Table of Contents
This guide explains how to use the History Command in Linux, with clear steps, practical tips, and important checks before you begin.
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
FAQ
What should I know about History Command in Linux?
Focus on the key features, requirements, limitations, and practical use cases explained in this guide.
How do I get the best results with History Command in Linux?
Follow the recommended steps, use current software or information, confirm compatibility, and review settings before major changes.
Are there any risks or limitations?
Potential limitations depend on compatibility, data quality, cost, privacy, support, and how the product or method is used.
Reader Comments 0
Sign in with email or Google to join the discussion.