Use the history command in Linux

  • 2020-10-23 20:23:43
  • OfStack

Use the powerful history command to make your command line prompt more efficient.

As I spent more and more time in the terminal, I felt like I was constantly looking for new commands to make my daily tasks more efficient. The history command of GNU was a command that really changed my routine.

The GNU history command holds a list of all other commands that were run from that terminal session, and then allows you to replay or reuse those commands without having to re-enter them. If you're a veteran player, you know the power of history, but for those of us who are half-timers or novice system administrators, history is an immediate productivity gain.

The history of 101

To view the command history, open the terminal program in Linux and type:

$ history

Here's the response I got:

[

clear
ls -al
sudo dnf update -y
history

]

The history command displays a list of commands entered since the beginning of the session. What's interesting about history is that you can replay any one command using the following command:

$ !3

In the prompt! 3 tells shell to rerun the third command in the history list. I can also type the following command to use:


linuser@my_linux_box: !sudo dnf

history will search for the last command that matches the pattern you provide and run it.

Search history

You can also type in!! Rerun the last command in the command history. Also, by pairing with grep, you can search for commands that match the text pattern, or by using with tail 1, you can find the last few commands you execute. Such as:


$ history | grep dnf
 sudo dnf update -y
 history | grep dnf
$ history | tail -n 3
 history
 history | grep dnf
 history | tail -n 3

Another way to do this is to type ES56en-ES57en to invoke a recursive search of your command history. After input, the prompt changes to:

(reverse-i-search)`':

Now you can start typing 1 command, and the matching command will be displayed. Press enter to execute.

Change the command that has been executed

history also allows you to rerun commands with a different syntax. For example, if I want to change my previous command history | grep dnf to history | grep ssh, I can execute the following command at the prompt:

$^dnf^ssh^

history will rerun the command, but replace dnf with ssh, and execute it.

Delete history

Sometimes you want to delete some or all of your history. To delete a specific command, type ES88en-ES89en < The line Numbers > . To clear the history, execute ES92en-ES93en.

The history file is stored in one file that you can modify. bash shell users can find.bash_history in their home directory.

Next step 1

There are many other things you can do with history:

Sets the history buffer to a fixed number of 1 Record the date and time of each line in history Prevents certain commands from being recorded in history

For more information about history command and other interesting things, please refer to the https: / / www gnu. org/software/bash manual /.

conclusion


Related articles: