Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the specific use of history commands in Linux system?

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces what is the specific use of history commands in the Linux system, the content is very detailed, interested friends can refer to, hope to be helpful to you.

When you execute a command in Linux, the commands you execute are recorded in the history command, and you don't have to enter the command every time you use the history command properly.

View your command history

To see the commands that have been run before, you just type history. You may see a long list of commands. The number of commands you remember depends on the environment variable named $HISTSIZE set in the ~ / .bashrc file, but if you want to save more or fewer commands, you can change this setting according to your needs.

To view the history, use the history command:

$history 209 uname-v 210 date 211 man chage... ...

To view the maximum number of commands that will be displayed:

HISTSIZE 500

You can change $HISTSIZE and make it permanent by running a command like this:

Echo "HISTSIZE=1000" > > ~ / .bashrc

There is also a difference between how much history is kept for you and how much history is displayed when you type history. The variable controls how much history is displayed, while the HISTFILESIZE variable controls how many commands are retained in your .bash _ history file.

HISTSIZE 1000 HISTFILESIZE 2000

You can validate the second variable by counting the number of lines in the history file:

$wc-l .bash _ history 2000 .bash _ history

It is important to note that commands entered in the login session will not be added to your .bash _ history file before logging out, although they will immediately appear in the history command output.

Usage history

There are three ways to resend the commands you found in history. The easiest way, especially if the command you want to reuse is recently run, is usually to enter one! It is followed by enough initials in the command to uniquely identify it.

$! U uname-v # 37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020

Another simple way to repeat a command is to simply press the up arrow key until the command is displayed, and then press enter.

In addition, if you run the history command and see that the command you want to rerun is listed, you can type one! This is followed by the serial number displayed on the left side of the command.

$! 209 uname-v # 37-Ubuntu SMP Thu Mar 26 20:41:27 UTC 2020

Hide history

If you want to stop recording commands for a period of time, you can use this command:

$set + o history when you type history, the commands you enter will not be displayed, and when you exit the session or terminal, they will not be added to your .bash _ history file.

To cancel this setting, use set-o history

To make it permanent, you can add it to your .bashrc file, although it is usually not a good idea not to use command history.

$echo 'set + o history' > > ~ / .bashrc to temporarily clear the history, so that only commands entered later are displayed when entering history. You can use the history-c (clear) command:

History-c $history 1 history

Note: commands entered after history-c are not added to the .bash _ history file.

Control history

The setting of the history command on many systems includes a variable named $HISTCONTROL by default to ensure that even if you run the same command seven times in a row, it will only be remembered once. It also ensures that commands that follow after you first enter one or more spaces will be ignored from your command history.

$grep HISTCONTROL .bashrc HISTCONTROL=ignoreboth

Ignoreboth means "ignore duplicate commands and commands that start with a space". For example, if you enter these commands:

Date date history

Your history command should report like this:

Echo try this history

Notice that successive date commands are reduced to one line, and commands indented by spaces are omitted.

Ignore history

To ignore certain commands so that they will not appear when you type history and will not be added to your .bash _ history file, use the $HISTIGNORE setting. For example:

The setting of $export HISTIGNORE= "history:cd:exit:ls:pwd:man" will cause all history, cd, exit, ls, pwd, and man commands to be ignored from the output of your history command and the .bash _ history file.

If you want to make this setting permanent, you must add it to your .bashrc file.

The $echo 'HISTIGNORE= "history:cd:exit:ls:pwd:man" > .bashrc setting simply means that when you look back at previously run commands, the list will not be disturbed by commands you don't want to see when viewing the command history.

Remember, ignore and forget past commands

Command history is useful because it helps you remember recently used commands and reminds you of recent changes. It also makes it easier for you to rerun commands, especially those that have a string of parameters but you don't necessarily want to recreate them. Customizing your history settings can make your use of command history easier and more efficient.

On the Linux system on the specific use of history commands is shared here, I hope that the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report