Linux command line for history

Source: Internet
Author: User

1. Use Histtimeformat to display timestamps
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

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 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 "The history]
# Cat/etc/redhat-release
Fedora Release 9 (sulphur)

Quickly repeat the previous command
There are 4 ways to repeatedly execute the previous command:

Use the UP ARROW key and enter to execute.
By!! and enter the execution.
Enter!-1 and return to execute.
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 | More
1 Service Network restart
2 exit
3 ID
4 Cat/etc/redhat-release
#! 4
Cat/etc/redhat-release
Fedora 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:

#!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

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

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

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
Pwd
Pwd
PWD [Note that there is three PWD commands in history, after executing pwd 3 times as shown above]
History | Tail-4
# Export Histcontrol=ignoredups
# pwd
# pwd
# pwd
# History | Tail-3
Histcontrol=ignoredups Export
3 pwd [Note that there was only a pwd command in the history, and even after executing pwd The times as shown above]
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. To clear duplicate entries in the entire command history, you can set Histcontrol to erasedups:

# Export Histcontrol=erasedups
# pwd
# Service HTTPD Stop
# History | Tail-3
The PWD
Service httpd Stop
History | Tail-3
# ls-ltr
# Service HTTPD Stop
# History | Tail-6
Export Histcontrol=erasedups
Pwd
Panax Notoginseng | Tail-3
Ls-ltr
Service httpd Stop
[Note that the previous service httpd stop after PWD got erased]
History | 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 are a space at the beginning of service, to ignore this command from history]
# History | Tail-3
Ls-ltr
Pwd
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.cfg
Anaconda-ks.cfg
# VI!!:$
VI anaconda-ks.cfg

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

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

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-3
histignore= Export "pwd:ls:ls-ltr:"
Service httpd Stop
Bayi History
[Note that the history does not the record PWD, LS and ls-ltr]

Linux command line for history

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.