Learn Linux commands every day--history

Source: Internet
Author: User

  The function of the history command is to display and number the used commands. The following commands are the different operations of the history:

History n shows the most recently used n commands.

History-c clears the history of the current shell.

history-d 801 Delete the command with number 801.

History-a append the latest command to the history file.

History-n shows a history that has not yet been read from the history file.

History-r the record in the history file as the history of the current shell.

History-w writes the current record to the history file, overwriting the original content.

The system default History command is difficult to meet our needs, we can modify the/etc/profile file to the hostory command

To modify the display effect:

(1) Show the time of the command execution

Add at the end of the/etc/profile file: Export histtimeformat= "%F%T ' WhoAmI ':" (note: Here WhoAmI both sides of the symbol is not a single quotation mark, but the TAB key above the symbol of the key, that is ~ the symbol below.) )

(2) Displays the source IP of the user executing the command

First get login source ip:user_ip= ' who-u am I 2>/dev/

null | awk ' {print &nf} ' | Sed-e ' s/[()]//g "

If ["&user_ip" = ""]

Then

User_ip= ' hostname '

Then export Histtimeformat, user_ip as a parameter to write Histtimeformat,

Export histtimeformat= "%F%T &user_ip: ' WhoAmI ':"

Combining the above tips, we can get the following script:

user_ip= ' who-u am I 2>/dev/null | awk ' {print &nf} ' |sed-e ' s/[()]//g '

If ["&user_ip" = ""]

Then

User_ip= ' hostname '

Fi

if [!-d/opt/history]

Then

Mkdir/opt/history

chmod 777/opt/history

Fi

if [!-d/opt/history/&{logname}]

Then

Mkdir/opt/history/&{logname}

chmod 300/opt/history/&{logname}

Fi

Export histsize=4096

Export histtimeformat= "[%y-%m-%d%h:%m:%s]"

Export histfile= "/opt/history/${logname}.history"

chmod 600/opt/history/*history* 2>/dev/null

Append the above code to the end of the/etc/profile file and log in again, and you will find that the display format of the history and the storage file are changed.

Learn Linux commands every day--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.