How to Use the history command in linux

Source: Internet
Author: User

The History command is mainly used to display the History Command records and issue commands in the History records.
1> History command syntax:
[Test @ linux] # history [n]
[Test @ linux] # history [-c]
[Test @ linux] # history [-raw] histfiles
Parameters:
N: Number. List the last n commands.
-C: removes all history content in the current shell.
-A: adds the newly added history command to histfiles. If histfiles is not added,
Write by default ~ /. Bash_history
-R: Read the content of histfiles to the history Memory of the current shell.
-W: Write the Current history Memory into histfiles
In Linux, when you enter and execute commands in shell (console), shell will automatically record your commands to the history list, which is generally stored in the user directory. bash_history file. By default, 1000 entries are saved. You can change this value.
If you type history, history will show you the first 1000 historical commands you are using and mark them, you will see a list of numbers quickly rolled out from the screen. You may not need to view all the projects in the 1000 commands. You can also add a number to list the latest n commands.
In linux, The history command not only allows us to query historical commands, but also allows us to execute commands using related functions.
2> run specific historical commands
History lists all history commands saved by bash and signs them. We can run specific historical commands in the way of "Exclamation mark and number.
Syntax description:
[Test @ linux] # [! Number] [! Command] [!]
Parameter description:
Number: the meaning of the instruction;
Command: the beginning of a command.
! : The meaning of the previous command!
3> History command practice
List all history records:
[Test @ linux] # history
Only the last 10 records are listed:
[Test @ linux] # history 10 (note that there are spaces between history and 10)
Use the Command record number to execute the command and execute the 99th commands in the History List
[Test @ linux] #! 99 (! There is no space in the middle of "and" 99)
Repeat the previous command
[Test @ linux] #!
Run the last command starting with rpm (!? ? It indicates a String, which can be input at will. Shell searches forward from the last historical command, and the first matched command will be executed .)
[Test @ linux] #! Rpm
List all history records on a screen:
[Test @ linux] # history | more
Clear all history commands of history now
[Test @ linux] # history-c
In addition to the history command, you can also use the up and down arrow keys to read the command history at the shell or GUI terminal prompt (the down arrow will read forward) until you find the required command. This allows us to easily edit a previous command without having to repeat similar commands.
The History command is really useful! But be careful about security issues! Especially the root history file, which is a favorite of hackers! Because the careless root will record a lot of important information during execution ~ /. In bash_history, if this file is parsed, the consequences will be unimaginable!

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.