15 Examples of mastering the Linux command line history

Source: Internet
Author: User

15 Examples of mastering the Linux command line history

When you use Linux command lines frequently, using history effectively can increase productivity. In fact, once you have the following 15 examples, you will find it more interesting and interesting to use a command line.

1. Use Histtimeformat to display timestamps

Typically, when you type history from the command line, it displays # and commands. For auditing purposes, it is helpful to display timestamp with the command, as shown below.

# export HISTTIMEFORMAT=‘%F %T ‘# history | more1  2018-08-05 19:02:39 service network restart2  2018-08-05 19:02:39 exit3  2018-08-05 19:02:39 id4  2018-08-05 19:02:39 cat /etc/redhat-release
2. Use Ctrl+r Search History

This is probably the most common historical feature. When you execute a long command, you simply use the keyword search history and re-execute the same command without having to type the command completely. Press ctrl+r and type the keyword . In the following example, I searched for "Red", and the command history shows the previous command "Red", "cat/etc/redhat-release "

# [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-releaseCentOS release 6.5 (Final)

Sometimes you want to be able to edit commands from the command history before executing the command. For example, you can search for httpd, which displays the service httpd stop in the command history, and select this command to change stop to start, as shown below.

# [Press Ctrl+R from the command prompt,which will display the reverse-i-search prompt](reverse-i-search)`httpd‘: service httpd stop[Note: Press either left arrow or right arrow key when you see yourcommand, which will display the command for you to edit, before executing it]# service httpd start
3. Use 4 different methods to quickly execute the previous command.

Sometimes you can repeat the previous commands for a variety of reasons. Here are 4 different ways to repeat the last command executed.

    1. Use up arrow to view previous commands, and then press ENTER to execute
    2. Type !! and press the ENTER key in the command to execute
    3. Type !-1 and press the ENTER key in the command to execute
    4. Press Control + P to display the previous command, and then press ENTER to execute
4. Execute specific commands from the history of the command

As shown below, if you want to repeat the command # # #, you can type ! 4 execution.

# history | more1  service network restart2  exit3  id4  cat /etc/redhat-release# !4cat /etc/redhat-releaseCentOS release 6.5 (Final)
5. Execute the previous command that starts with a specific keyword

Input! Then there are a few letters from the beginning of the command you want to re-execute. In the following example, type!ps and enter to execute the previous command that begins with PS, which is ' PS aux | grep YP '.

# !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
6. Use Histsize to control the total number of history records

Attach the following two lines to the. Bash_profile and log back in to the bash shell again to see the changes. In this example, only 450 of the commands are stored in bash history.

# vi~ / .bash_profileHISTSIZE = 450HISTFILESIZE = 450
7. Change the history file name using Histfile

By default, history is stored in the ~/.bash_history file. Add the following line to the. Bash_profile and log back in to the bash shell to store the history commands in the. commandline_warrior file instead of the. bash_history file. I haven't found the actual use yet. This is useful when you want to keep track of the commands executed from different terminals using different historical filenames.

# vi~ / .bash_profile HISTFILE = / root / .commandline_warrior
8. Use Histcontrol to eliminate continuous repeated input of history

In the following example, PWD is typed three times, and when you perform a history, you can see that all of its records appear 3 times in a row. To eliminate duplication, set Histcontrol to Ignoredups as shown below.

# pwd# pwd# pwd# history | tail -444  pwd45  pwd46  pwd [Note that there are three pwd commands in history, afterexecuting pwd 3 times as shown above]47  history | tail -4# 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 afterexecuting pwd 3 times as shown above]58  history | tail -4

Note: The same command must be satisfied with this condition continuously

9. Erase duplicates from the entire history using Histcontrol

The ignoredups shown above will remove duplicates only if they are sequential commands. To eliminate duplicates in the entire history, set Histcontrol to Erasedups as shown below.

# export HISTCONTROL=erasedups# pwd# service httpd stop# history | tail -338  pwd39  service httpd stop40  history | tail -3# 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
10. Use Histcontrol to force history don't remember specific commands

When you execute a command, you can ignore the command by setting Histcontrol to ignore the space and typing an empty glyd in front of the command, as shown below. I can see that many junior system administrators are excited about this because they can hide historical commands. A good understanding of how ignorespace works. But, as a best practice, don't have a destination to hide anything in history.

# 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 option-C to clear all previous history command records
# history -c
12. Get parameters from the history command

When searching history, you might want to execute different commands, but use the same parameters in the command you just searched for.

In the following example, the !!:$ next to the VI command gets the parameters of the previous command to the current command.

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

In the following example, the !^ next to the VI command gets the first parameter to the current command (that is, the VI command) from the previous command (that is, the CP command).

# cp anaconda-ks.cfg anaconda-ks.cfg.bakanaconda-ks.cfg# vi  !^vi anaconda-ks.cfg
13. Replace specific parameters for a specific command.

In the following example,!cp:2 The previous command that begins with CP in the search history and gets the second parameter of the CP and replaces it with the Ls-l command, as shown below.

# 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:$ searches for the previous command in the history that begins with the CP, and gets the last parameter of the CP (in this case, the second parameter shown above) and replaces it with the Ls-l command as shown in.

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

If you want to disable history and do not want the bash shell to remember the command you typed, set Histsize to 0, as shown below.

# export HISTSIZE=0# history# [Note that history did not display anything]
15. Use Histignore to ignore specific commands in the history

Sometimes you may not want to confuse your history with basic commands like PWD and LS. Use Histignore to specify all commands to be ignored from the history. Note that adding LS to histignore only ignores LS and not ls-l. Therefore, you must provide the exact command that you want to ignore from the history record.

# export HISTIGNORE="pwd:ls:ls -ltr:"# pwd# ls# ls -ltr# service httpd stop# history | tail -379  export HISTIGNORE="pwd:ls:ls -ltr:"80  service httpd stop81  history[Note that history did not record pwd, ls and ls -ltr]

15 Examples of mastering the Linux command line history

Related Article

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.