History (historical) command Usage 15 cases

Source: Internet
Author: User

Guide If you use the Linux command line frequently, then using the history command can effectively improve your efficiency, this article will introduce you to the 15 uses of the historical command in the form of an instance.

displaying timestamps using Histtimeformat

When you execute the history command from the command line, you will usually only show the ordinal of the executed command and the command itself, and if you want to see the timestamp of the command historical, you can do it:

# 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 new Bash command that was set after the HISTTIMEFORMAT environment variable is put on the correct timestamp. All previous commands will be displayed as a time to set the Histtimeformat variable.

Search History using Ctrl+r

Ctrl+r is a shortcut that I use frequently, which allows you to search the history of a command, which is useful when you want to execute a command repeatedly. When the command is found, you can usually press ENTER to execute the pre command, and if you want to make adjustments to the found command, you can click the left or right arrow keys.

# [Press ctrl+r from the command prompt, which would display the Reverse-i-search prompt] (reverse-i-search) ' Red ': cat/etc/ Redhat-release[note:press Enter when you see your command, which'll execute the command from the history]# cat/etc/red Hat-releasefedora Release 9 (sulphur)
quickly repeat the previous command

There are 4 ways to repeatedly execute the previous command:


1. Use the UP ARROW key and enter to execute.
2. Press!! and enter the execution.
3. Enter!-1 and return to execute.
4. Press Ctrl+p and enter to execute.

Executes a specified command from the command history

In the example below, if you want to execute the 4th command repeatedly, you can do it! 4:

# History | More1  Service Network restart2  exit3  id4  cat/etc/redhat-release#!4cat/etc/redhat-releasefedora Release 9 (sulphur)
to execute a previous command by specifying a keyword

In the following example, enter!ps and return to the command that starts with PS:

#!psps aux | grep yproot     16947  0.0  0.1  36516  1264?        Sl   13:10   0:00 ypbindroot     17503  0.0  0.0   4124   740 pts/0    s+   19:19   0:00 grep YP
the total number of rows recorded using the Histsize Control history command

Append the following two lines of content to the. bash_profile file and re-login to the bash shell, the number of records in the command history becomes 450:

# VI ~/.bash_profilehistsize=450histfilesize=450
Change the history file name using Histfile

By default, the command history is stored in the ~/.bash_history file, adding the following to the. bash_profile file and re-logging into the bash shell, which uses the. Commandline_warrior to store the command history:

# VI ~/.bash_profilehistfile=/root/.commandline_warrior
use Histcontrol to remove successive duplicates from the command history

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

# pwd# pwd# pwd# History | tail-444  pwd45  pwd46  pwd [Note that there is three PWD commands in history, after executing PWD 3 times as S Hown above]47 History  | tail-4# export histcontrol=ignoredups# pwd# pwd# pwd# history | tail-356  Export Histcont Rol=ignoredups57  pwd [Note that there was only a PWD command in the ' history ', even after executing pwd 3 times as show n above]58 History  | tail-4
use Histcontrol to clear duplicate entries in the entire command history

The ignoredups in the above example can only reject successive duplicates, and to clear the duplicate entries in the entire command history, you can set Histcontrol to erasedups:

# Export histcontrol=erasedups# pwd# service httpd stop# History | tail-338  pwd39  service httpd stop40 History  | tail-3# ls-ltr# service httpd stop# | tail-635  E Xport histcontrol=erasedups36  pwd37 History  | tail-338  ls-ltr39  service httpd stop[note Previous service httpd stop after pwd got erased]40  | tail-6
use Histcontrol to force history to not remember specific commands

Set Histcontrol to Ignorespace and enter a space before the command that you do not want to remember:

# export histcontrol=ignorespace# ls-ltr# pwd#  service httpd Stop [Note that there was a space at the beginning of ser Vice, to ignore the command from history]# | tail-367  ls-ltr68  pwd69 History  | tail-3
Clear All command history with the-C option

If you want to clear all the command history, you can do it:

# history-c
command Substitution

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

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

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

# CP anaconda-ks.cfg anaconda-ks.cfg.bakanaconda-ks.cfg# vi-5!^vi anaconda-ks.cfg
to replace a specified parameter with a specific command

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

# 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 parameter of the CP command:

# ls-l!CP: $ls-L/really/a/very/long/path/long-filename.txt
Disabling history with Histsize

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

# export histsize=0# history# [Note that the history does not display anything]
use Histignore to ignore specific commands in history

In the following example, the PWD, LS, ls-ltr, and so on commands are ignored:

# Export histignore= "pwd:ls:ls-ltr:" # pwd# ls# ls-ltr# service httpd stop# History | tail-379  Export histignore= "pwd:ls:ls-ltr:"  service httpd stop81  history[note Record pwd, LS and ls-ltr]
Guide If you use the Linux command line frequently, then using the history command can effectively improve your efficiency, this article will introduce you to the 15 uses of the historical command in the form of an instance.

displaying timestamps using Histtimeformat

When you execute the history command from the command line, you will usually only show the ordinal of the executed command and the command itself, and if you want to see the timestamp of the command historical, you can do it:

# 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 new Bash command that was set after the HISTTIMEFORMAT environment variable is put on the correct timestamp. All previous commands will be displayed as a time to set the Histtimeformat variable.

Search History using Ctrl+r

Ctrl+r is a shortcut that I use frequently, which allows you to search the history of a command, which is useful when you want to execute a command repeatedly. When the command is found, you can usually press ENTER to execute the pre command, and if you want to make adjustments to the found command, you can click the left or right arrow keys.

# [Press ctrl+r from the command prompt, which would display the Reverse-i-search prompt] (reverse-i-search) ' Red ': cat/etc/ Redhat-release[note:press Enter when you see your command, which'll execute the command from the history]# cat/etc/red Hat-releasefedora Release 9 (sulphur)
quickly repeat the previous command

There are 4 ways to repeatedly execute the previous command:


1. Use the UP ARROW key and enter to execute.
2. Press!! and enter the execution.
3. Enter!-1 and return to execute.
4. Press Ctrl+p and enter to execute.

Executes a specified command from the command history

In the example below, if you want to execute the 4th command repeatedly, you can do it! 4:

# History | More1  Service Network restart2  exit3  id4  cat/etc/redhat-release#!4cat/etc/redhat-releasefedora Release 9 (sulphur)
to execute a previous command by specifying a keyword

In the following example, enter!ps and return to the command that starts with PS:

#!psps aux | grep yproot     16947  0.0  0.1  36516  1264?        Sl   13:10   0:00 ypbindroot     17503  0.0  0.0   4124   740 pts/0    s+   19:19   0:00 grep YP
the total number of rows recorded using the Histsize Control history command

Append the following two lines of content to the. bash_profile file and re-login to the bash shell, the number of records in the command history becomes 450:

# VI ~/.bash_profilehistsize=450histfilesize=450
Change the history file name using Histfile

By default, the command history is stored in the ~/.bash_history file, adding the following to the. bash_profile file and re-logging into the bash shell, which uses the. Commandline_warrior to store the command history:

# VI ~/.bash_profilehistfile=/root/.commandline_warrior
use Histcontrol to remove successive duplicates from the command history

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

# pwd# pwd# pwd# History | tail-444  pwd45  pwd46  pwd [Note that there is three PWD commands in history, after executing PWD 3 times as S Hown above]47 History  | tail-4# export histcontrol=ignoredups# pwd# pwd# pwd# history | tail-356  Export Histcont Rol=ignoredups57  pwd [Note that there was only a PWD command in the ' history ', even after executing pwd 3 times as show n above]58 History  | tail-4
use Histcontrol to clear duplicate entries in the entire command history

The ignoredups in the above example can only reject successive duplicates, and to clear the duplicate entries in the entire command history, you can set Histcontrol to erasedups:

# Export histcontrol=erasedups# pwd# service httpd stop# History | tail-338  pwd39  service httpd stop40 History  | tail-3# ls-ltr# service httpd stop# | tail-635  E Xport histcontrol=erasedups36  pwd37 History  | tail-338  ls-ltr39  service httpd stop[note Previous service httpd stop after pwd got erased]40  | tail-6
use Histcontrol to force history to not remember specific commands

Set Histcontrol to Ignorespace and enter a space before the command that you do not want to remember:

# export histcontrol=ignorespace# ls-ltr# pwd#  service httpd Stop [Note that there was a space at the beginning of ser Vice, to ignore the command from history]# | tail-367  ls-ltr68  pwd69 History  | tail-3
Clear All command history with the-C option

If you want to clear all the command history, you can do it:

# history-c
command Substitution

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

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

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

# CP anaconda-ks.cfg anaconda-ks.cfg.bakanaconda-ks.cfg# vi-5!^vi anaconda-ks.cfg
to replace a specified parameter with a specific command

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

# 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 parameter of the CP command:

# ls-l!CP: $ls-L/really/a/very/long/path/long-filename.txt
Disabling history with Histsize

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

# export histsize=0# history# [Note that the history does not display anything]
use Histignore to ignore specific commands in history

In the following example, the PWD, LS, ls-ltr, and so on commands are ignored:

# Export histignore= "pwd:ls:ls-ltr:" # pwd# ls# ls-ltr# service httpd stop# History | tail-379  Export histignore= "pwd:ls:ls-ltr:"  service httpd stop81  history[note Record pwd, LS and ls-ltr]

History (historical) command Usage 15 cases

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.