Linux under the history command usage

Source: Internet
Author: User
Tags add numbers

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 usages of the history command in the form of an instance.

    1. displaying timestamps using Histtimeformat

      When you execute the history command from the command line, it usually shows only the ordinal of the executed command and the command itself. If you want to see the timestamp of the command history, you can do it:

      # export HISTTIMEFORMAT=‘%F %T ‘
      # history | more
      1 2008-08-05 19:02:39 service network restart
      2 2008-08-05 19:02:39 exit
      3 2008-08-05 19:02:39 id
      4 2008-08-05 19:02:39 cat /etc/redhat-release

      Note : This feature can only be used when the HISTTIMEFORMAT environment variable is set, after which the newly executed bash command will be hit with the correct timestamp. All previous commands will be displayed as a time to set the Histtimeformat variable. [Thanks to NightOwl reader Supplement]

    2. Search History Using Ctrl+r

      Ctrl+r is a shortcut key that I often use. This shortcut lets you search the history of a command, which is useful when you want to execute a command repeatedly. When the command is found, it is usually possible to press ENTER again to execute the command. If you want to adjust the found command and then execute it, you can click 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-release
      Fedora release 9 (Sulphur)

    3. 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. By!! and enter the execution.
      3. Enter!-1 and return to execute.
      4. Press Ctrl+p and enter to execute.
    4. 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 | more
      1 service network restart
      2 exit
      3 id
      4 cat /etc/redhat-release
      # !4
      cat /etc/redhat-release
      Fedora release 9 (Sulphur)

    5. To execute a previous command by specifying a keyword

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

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

    6. 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_profile
      HISTSIZE=450
      HISTFILESIZE=450

    7. Change the history file name using Histfile

      By default, the command history is stored in the ~/.bash_history file. Add the following to the. bash_profile file and log back in to the bash shell, which will use the. Commandline_warrior to store the command history:

      # vi ~/.bash_profile
      HISTFILE=/root/.commandline_warrior

    8. 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 Histcontrol to ignoredups:

      # pwd
      # pwd
      # pwd
      # history | tail -4
      44 pwd
      45 pwd
      46 pwd [Note that there are three pwd commands in history, after executing pwd 3 times as shown above]
      47 history | tail -4
      # export HISTCONTROL=ignoredups
      # pwd
      # pwd
      # pwd
      # history | tail -3
      56 export HISTCONTROL=ignoredups
      57 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

    9. use Histcontrol to clear duplicate entries in the entire command history

      The ignoredups in the previous example can only reject successive duplicates. To clear duplicate entries throughout the command history, you can set Histcontrol to erasedups:

      # Export histcontrol= Erasedups
      # pwd
      # service httpd stop
      # History | tail-3
      $ PWD
      Service httpd stop
      History | tail -3
      # ls-ltr
      # service httpd stop
      # History | tail-6
      + export histcontrol=erasedups
      pwd
      Notoginseng history | Tail-3
      ls-ltr
      Service httpd Stop
      [Note that the previous service httpd stop after PWD got erased]
      + H istory | Tail-6

    10. 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 is a space at the beginning of service, to ignore this command from history]
      # history | tail -3
      67 ls -ltr
      68 pwd
      69 history | tail -3
    11. Clear all command history with the-C option

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

      # history -c

    12. Command substitution

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

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

      add : Using!$ can achieve the same effect, and easier. [Thanks to Wanzigunzi reader Supplement]

      In the following example,!^ obtains the first parameter from the previous command:

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

    13. 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:2
      ls -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

    14. Disabling history with Histsize

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

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

    15. 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 -3
      79 export HISTIGNORE=”pwd:ls:ls -ltr:”
      80 service httpd stop
      81 history
      [Note that history did not record pwd, ls and ls -ltr]


If you use Linux commands frequently, then using the history command will undoubtedly improve your productivity.
The history command is used primarily to display historical instruction records and to release instructions in the history record.
1>history command syntax:
[[Email protected]]# history [n]
[Email protected]]# history [-c]
[Email protected]]# history [-raw] Histfiles
Parameters:
N: number, to list the most recent n-pen commands
-C: Eliminate all history content in the current shell
-A: Add the current New History command to Histfiles, without adding histfiles,
The default write ~/.bash_history
-r: Read Histfiles's contents into the current Shell's history memory
-W: Writes current history memory content to Histfiles
Linux system when you enter and execute commands in the shell (console), the shell automatically logs your commands to the history list, which is typically saved in the. bash_history file in the user directory. Save 1000 By default, and you can change this value.
If you type history, it will show you the top 1000 historical commands you used, and give them a number, and you'll see a numbered list quickly rolled over the screen. You may not need to see all the items in the 1000 commands, but you can also add numbers to list the most recent n-pen commands.
The history command in Linux not only allows us to query historical commands. We can also use the relevant features to help us execute commands.
2> run a specific history command
History lists all the historical commands that bash has saved and gives them numbers, and we can run specific historical commands in the same way as the exclamation mark number.
Syntax Description:
[Email protected]]# [!number] [!command] [!]
Parameter description:
Number: The meaning of the first instruction;
Command: The first few letters of the instruction
! : The meaning of the previous instruction!
3>history Command Combat
List all the history records:
[[email protected]] # History
Only the last 10 records are listed:
[[email protected]] # History 10 (note, history and 10 have spaces in between)
Execute the command using the command record number, and execute the 99th command in the history list
[[email protected]] #!99 (! And 99 no spaces in the middle)
Repeat the previous command
[[email protected]] #!!
Execute the last command that starts with RPM (!?). is a string, which can be lost randomly, the shell will search forward from the last history command, and the first matching command will be executed. )
[[email protected]] #!rpm
List all history on a per-screen basis:
[[Email protected]]# History | More
Immediately clears the history of all current historical commands
[[email protected]] #history-C
In addition to using the history command, you can also use the UP and DOWN ARROW keys to scroll through the command past in the shell or GUI terminal prompts, until you find the command you want. This allows us to easily edit one of the preceding commands without having to enter similar commands repeatedly.
The use of the history command is really great! But you need to be careful about security! Especially the root history file, this is the hacker's favorite! Because the careless root will be a lot of important information in the process of execution will be recorded in the ~/.bash_history, if the file is parsed, the consequences are unimaginable!

Linux under the history command usage

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.