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 the Linux history command

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

Share

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

This article mainly introduces the relevant knowledge of "how to use the Linux history command". The editor shows you the operation process through an actual case. The operation method is simple, fast and practical. I hope this article "how to use the Linux history command" can help you solve the problem.

The history command is used to display a specified number of command commands, read directories in the history command file into the history command buffer, and write directories in the history command buffer to the command file. When this command is used alone, only historical commands are displayed, and symbols can be used on the command line! Executes the history command of the specified sequence number.

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) executes 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 uses the HISTSIZE control history command to record the total number of lines

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 uses HISTFILE to change the history file name

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 uses 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-destroy 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 uses 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-ordered 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 uses 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 uses 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 replaces 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] that's all for "how to use the Linux history command". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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