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 use history command in linux system

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

Share

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

This article mainly introduces how to use the history command in the linux system, has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.

The history command is used to display executed commands, or you can re-execute the required commands based on the displayed commands.

Use HISTTIMEFORMAT to display timestamps

When you execute a history command from the command line, it usually only displays the sequence number of the executed command and the command itself. If you want to see the timestamp of the command history, you can execute:

# export HISTTIMEFORMAT='%F% T'# history | more1 2008-08-05 19:02:39 service network restart2 2008-08-05 19:02:39 exit3 2008-08-05 19:02:39 id4 2008-08-05 19:02:39 cat / etc/redhat-release

Note: this feature can only be used when the HISTTIMEFORMAT environment variable is set to those newly executed bash commands will be stamped correctly. All commands prior to this will be displayed as the time to set the HISTTIMEFORMAT variable.

Use Ctrl+R to search for history

Ctrl+R is one of the keyboard shortcuts I often use. This shortcut allows you to search the command history and is very useful when you want to repeat a command. When a command is found, you can usually press enter to execute the pre command, and if you want to adjust the found command before executing it, you can press the left or right arrow keys.

# [Press Ctrl+R from the command prompt, which will display the reverse-i-search prompt] (reverse-i-search) `red': cat / etc/redhat-release [Note: Press enter when you see your command, which will execute the command from the history] # cat / etc/redhat-releaseFedora release 9 (Sulphur)

Quickly repeat the previous command

There are four ways to repeat the previous command:

1. Use the up arrow keys and press enter to execute. two。 Press! And enter to execute. 3. Enter!-1 and enter to execute. 4. Press Ctrl+P and press enter to execute.

Execute a specified command from the command history

In the following example, if you want to repeat the fourth command, you can do it! 4:

# history | more1 service network restart2 exit3 id4 cat / etc/redhat-release#! 4cat / etc/redhat-releaseFedora release 9 (Sulphur)

Execute previous commands by specifying keywords

In the following example, enter! ps and enter, and the command that starts with ps will be executed:

#! psps aux | grep yproot 16947 0.0 0.1 36516 1264? Sl 13:10 0:00 ypbindroot 17503 0.0 4124 740 pts/0 S + 19:19 0:00 grep yp

Use HISTSIZE to control the total number of rows recorded by the history command

Append the following two lines to the .bash _ profile file and log in to bash shell again, and the number of records of command history will be 450:

# vi ~ / .bash_profileHISTSIZE=450HISTFILESIZE=450

Change the history file name using HISTFILE

By default, command history is stored in the ~ / .bash_history file, add the following to the .bash _ profile file and log in to bash shell again, and .commandline _ warrior is used to store the command history:

# vi ~ / .bash_profileHISTFILE=/root/.commandline_warrior

Use HISTCONTROL to remove consecutive repeated entries from the command history

In the following example, the pwd command is executed three times in a row. After executing history, you will see three duplicate entries. To remove these duplicate entries, you can set HISTCONTROL to ignoredups:

# pwd# pwd# pwd# history | tail-444 pwd45 pwd46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above] 47 history | tail-attached export HISTCONTROL=ignoredups# pwd# pwd# pwd# history | tail-356 export HISTCONTROL=ignoredups57 pwd [Note that there is only one pwd command in the history, even after executing pwd 3 times as shown above] 58 history | tail-4

Use HISTCONTROL to clear duplicate entries throughout the command history

The ignoredups in the above example can only eliminate consecutive duplicate entries. To clear the duplicate entries throughout the command history, you can set HISTCONTROL to erasedups:

# export HISTCONTROL=erasedups# pwd# service httpd stop# history | tail-338 pwd39 service httpd stop40 history | tail-attached ls-ltr# service httpd stop# history | tail-635 export HISTCONTROL=erasedups36 pwd37 history | tail-338 ls-ltr39 service httpd stop [Note that the previous service httpd stop after pwd got erased] 40 history | tail-6

Use HISTCONTROL to force history not to remember specific commands

Set HISTCONTROL to ignorespace and enter a space before the command you don't want to remember:

# export HISTCONTROL=ignorespace# ls-ltr# pwd# service httpd stop [Note that there is a space at the beginning of service, to ignore this command from history] # history | tail-367ls-ltr68 pwd69 history | tail-3

Use the-c option to clear all command history

If you want to erase all command history, you can execute:

# history-c

Command replacement

In the following example,!: $will get the parameters of the previous command for the current command:

# ls anaconda-ks.cfganaconda-ks.cfg# vi!: $vi anaconda-ks.cfg

Add: use! $can achieve the same effect, and easier. In the following example,! ^ gets the first parameter from the previous command:

# cp anaconda-ks.cfg anaconda-ks.cfg.bakanaconda-ks.cfg# vi-5! ^ vi anaconda-ks.cfg

Replace the specified parameters for a specific command

In the following example,! cp:2 searches the command history for a command that starts with cp and gets its second argument:

# cp ~ / longname.txt / really/a/very/long/path/long-filename.txt# ls-l! cp:2ls-l / really/a/very/long/path/long-filename.txt

In the following example,! cp:$ gets the last argument of the cp command:

# ls-l! cp:$ls-l / really/a/very/long/path/long-filename.txt

Disable history using HISTSIZE

If you want to disable history, you can set HISTSIZE to 0:

# export HISTSIZE=0# history# [Note that history did not display anything]

Use HISTIGNORE to ignore specific commands in history

The following example ignores commands such as pwd, ls, ls-ltr, and so on:

# export HISTIGNORE= "pwd:ls:ls-ltr:" # pwd# ls# ls-ltr# service httpd stop# history | tail-379export HISTIGNORE= "pwd:ls:ls-ltr:" 80 service httpd stop81 history [Note that history did not record pwd, ls and ls-ltr] Thank you for reading this article carefully. I hope the article "how to use history commands in linux system" shared by the editor will be helpful to you. At the same time, I hope you will support it and pay attention to the industry information channel. More related knowledge is waiting for you to learn!

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