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

How to adjust command history on Linux

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to adjust the command history on Linux. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

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:

$echo $HISTSIZE 500

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

$export HISTSIZE=1000$ 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 $HISTSIZE variable controls how much history is displayed, while the $HISTFILESIZE variable controls how many commands are retained in your .bash _ history file.

$echo $HISTSIZE 1000$ echo $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 you enter history, use the history-c (clear) command:

$history | tail-3 209 uname-v 210 date 211 man chage $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:

$echo try this $date $date $date $pwd $history

Your history command should report like this:

$history $echo try this $date $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:

$export HISTIGNORE= "history:cd:exit:ls:pwd:man"

This setting 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.

$echo 'HISTIGNORE= "history:cd:exit:ls:pwd:man" > .bashrc

This setting just 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 you view 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.

This is the end of the article on "how to adjust the command history on Linux". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please 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

Servers

Wechat

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

12
Report