History (History) command usage: 15 examples

Source: Internet
Author: User

If you often use Linux Command lines, using the history (History) command can effectively improve your efficiency. This article describes the 15 Use Cases of the History command.

  1. Display timestamp with histtimeformat

    After you execute the history command from the command line, only the serial number of the executed command and the command itself are displayed. To view the timestamp of the command history, run the following command:

    # Export histtimeformat = '% F % t'
    # History | more
    1 2008-08-05 19:02:39 service network restart
    2 19:02:39 exit
    3 2008-08-05 19:02:39 ID
    4 2008-08-05 19:02:39 CAT/etc/RedHat-release

  2. Use Ctrl + R to search for history

    CTRL + R is a frequently used shortcut key. This shortcut allows you to search for command history, which is useful when you want to execute a command repeatedly. After finding the command, you can usually press the Enter key to execute the command. If you want to adjust the 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. Repeat the previous command

    You can run the previous command again in four ways:

    1. Use the up arrow key and press enter to execute.
    2. Press !! And press enter to execute.
    3. Enter! -1 and press enter to execute.
    4. Press Ctrl + P and press Enter.
  4. Execute a specified command from the command history

    In the following example, if you want to repeat 4th commands, You can execute them! 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. Execute previous commands by specifying keywords

    In the example below, enter! PS and press Enter. The following command is executed to start with PS:

    #! PS
    PS aux | grep yp
    Root 16947 0.0 0.1 36516 1264? SL ypbind
    Root 17503 0.0 0.0 4124 740 pts/0 S + grep yp

  6. Use histsize to control the total number of historical Command records

    Append the following two lines to the. bash_profile file and log on to the bash shell again. The number of command history records will change to 450:

    # Vi ~ /. Bash_profile
    History Size = 450
    Histfilesize = 450

  7. Use histfile to change the name of a historical file

    By default, the command history is stored in ~ /. Bash_history file. Add the following content to the. bash_profile file and log on to the bash shell again. The. commandline_warrior command history will be stored:

    # Vi ~ /. Bash_profile
    Histfile =/root/. commandline_warrior

  8. Use histcontrol to remove consecutive duplicate entries from the command history

    In the following example, the PWD command is executed three times in a row. After history is executed, 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 history of the entire command

    In the previous example, ignoredups can only remove consecutive duplicate entries. To clear duplicate entries in the history of the entire command, you can set histcontrol to erasedups:

    # Export histcontrol = erasedups
    # Pwd
    # Service httpd stop
    # History | tail-3
    38 pwd
    39 service httpd stop
    40 history | tail-3
    # Ls-LTR
    # Service httpd stop
    # History | tail-6
    35 Export histcontrol = erasedups
    36 pwd
    37 history | tail-3
    38 LS-LTR
    39 service httpd stop
    [Note that the previous service httpd stop after PWD got erased]
    40 history | tail-6

  10. Use histcontrol to force history not to remember specific commands

    Set histcontrol to ignorespace and enter a space before the command 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 -367  ls -ltr68  pwd69  history | tail -3
  11. Use the-C option to clear all command history

    To clear all command history, run the following command:

    # History-C

  12. Command replacement

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

    # Ls anaconda-ks.cfg
    Anaconda-ks.cfg
    # Vi !! : $
    VI anaconda-ks.cfg

    In the following example ,! ^ Obtain 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. Replaces the specified parameter with a specific command.

    In the example below ,! CP: 2 search for a command starting with CP from the command history and obtain 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: $ get the last parameter of the CP command:

    # Ls-l! CP: $
    Ls-L/really/A/very/long/path/long-filename.txt

  14. Disable 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, commands such as PWD, ls, and LS-LTR are ignored:

    # Export histignore = "PWD: ls-LTR :"
    # Pwd
    # Ls
    # Ls-LTR
    # Service httpd stop
    # History | tail-3
    79 export histignore = "PWD: ls-LTR :"
    80 service httpd stop
    81 history
    [Note that history did not record PWD, ls and LS-LTR]

(Responsible editor: A6)

Link: http://linuxtoy.org/archives/history-command-usage-examples.html

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.