Detailed explanation of how to adjust Linux command history

Detailed explanation of how to adjust Linux command history

The bash history command in Linux system helps to remember the commands you have run previously and repeat them without retyping them.

If you can, you'll definitely be happy not having to read a dozen pages of manual pages and list your files again every once in a while, but instead being able to view previously run commands by typing history. In this post, we'll explore how to make the history command remember what you want it to remember, and forget commands that may not have much "historical value".

View your command history

To view previously run commands, you can simply type history. You may see a long list of commands. The number of commands remembered depends on the environment variable named $HISTSIZE set in your ~/.bashrc file, but you can change this setting according to your needs if you want to save more or less commands.

To view the history, use the history command:

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

To see 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 of the 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 verify 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 during a login session are not added to your .bash_history file until you log out, although they will immediately show up in the history command output.

History of use

There are three ways to reissue commands you find in the history. The easiest way, especially if the command you want to reuse was recently run, is usually to type a ! followed by enough of the command's first letters to uniquely identify it.

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

Another easy way to repeat a command is to simply press the Up Arrow key until the command is displayed and then press Enter.

Alternatively, if you run the history command and see a command listed that you want to rerun, you can type a ! followed by the number shown to the left of the command.

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

Hidden History

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

$ set +o history

The commands you enter will not be displayed when you type history, nor will they be added to your .bash_history file when you exit your session or quit Terminal.

To unset this setting, use set -o history

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

$ echo 'set +o history' >> ~/.bashrc

To temporarily clear the history so that only commands entered afterwards are displayed when history is typed, 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 entering history -c will not be added to the .bash_history file.

Control History

The default settings for the history command on many systems include a variable called $HISTCONTROL 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 any command you enter followed by one or more spaces will be omitted from your command history.

$ grep HISTCONTROL .bashrc
HISTCONTROL=ignoreboth

ignoreboth means "ignore duplicate commands and commands starting with whitespace". For example, if you enter these commands:

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

Your history command should report something like this:

$ history
$ echo try this
$ date
$ history

Note that consecutive date commands are reduced to one, and commands indented with spaces are omitted.

Ignoring history

To ignore certain commands so that they do not appear when you type history and are not 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 from your .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 simply means that when you look back at previously run commands, the list won't be cluttered with commands you don't want to see when viewing your command history.

Remember, Ignore, and Forget Past Commands

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

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • How to view and execute historical commands in Linux
  • Teach you to hide Linux command line history
  • Shell history command recording function in Linux
  • How to Communicate with Other Users on the Linux Command Line
  • There is no make command in Linux (make: *** No target specified and no makefile or make command installation method found)
  • Use of Linux telnet command
  • Linux gzip command compression file implementation principle and code examples

<<:  Solution to MySQL IFNULL judgment problem

>>:  Web interview Vue custom components and calling methods

Recommend

Summary of various methods for Vue to achieve dynamic styles

Table of contents 1. Ternary operator judgment 2....

Vue+js realizes video fade-in and fade-out effect

Vue+js realizes the fade in and fade out of the v...

About the problems of congruence and inequality, equality and inequality in JS

Table of contents Congruent and Incongruent congr...

Example tutorial on using the sum function in MySQL

Introduction Today I will share the use of the su...

WeChat applet tab left and right sliding switch function implementation code

Effect picture: 1. Introduction Your own applet n...

Apache Log4j2 reports a nuclear-level vulnerability and a quick fix

Apache Log4j2 reported a nuclear-level vulnerabil...

Use image to submit the form instead of using button to submit the form

Copy code The code is as follows: <form method...

MySql 5.7.20 installation and configuration of data and my.ini files

1. First download from the official website of My...

Problem analysis of using idea to build springboot initializer server

Problem Description Recently, when I was building...

Vue implements three-level navigation display and hiding

This article example shares the specific code of ...

Getting Started Guide to Converting Vue to React

Table of contents design Component Communication ...