This article will give you an overview of the history command in Linux
preface
Have you ever run a long command while developing with Linux and then have to re-type it later to execute the command?
You probably know that in Linux, the arrow up ↑ can be used to find the previous command, but this command is difficult to find if it has been executed for a long time.
If you are using an Xsheel client, you won’t find it when the client is closed.
So how to solve this problem!
Linux command history operations
1. Use history to view historical records
When you run the history command, 1000 previous commands are displayed.
But 1000 is the default value if you can save more than 1000 commands without logging out or shutting down.
Of course, this value can be changed, if you want to change it, you can run the following command.
echo "HISTSIZE=2000" >> .bash_profile
echo "HISFILETSIZE=2000" >> .bash_profile
source~/.bash_profile (make it valid)Copy the code
If the server is in charge and you need to see what command was executed at what point in time, just execute the following command
export HISTTIMEFORMAT="%F %T"
Copy the code
Then you can see the time after executing the history command
2. Use Ctrl + R to search for execution records directly
For example to perform a test for a correct command nginx configuration usr/local/nginx/sbin/nginx – t
To find a command, press Ctrl+ R and type usr
3. Repeat the previous command
There are several ways to execute the previous command, and a common one is probably ↑.
But today to daji said is, can use!! To operate
Of course you can use it too! 1 to perform
4. Run the command corresponding to the serial number
Where does this sequence number come from? This is the command history mentioned in the first article. The sequence number of each command is displayed after execution.
I want to execute command 1020, so I can do it like this! 1020.
In fact, this command has a bit of programming basis should be able to figure out, in the third section we use!! To execute the last command, which is actually the last record in history.
The same goes for this command! 1020, so the memory of this command is regular.
5. Modify the total number of historical records
In the first section, the modification of the total number of lines is also briefly mentioned, but not personally performed, the next operation.
In this command, HISTSIZE controls the number of displays in the history command
And HISTFILETSIZE controls the amount of storage in the file
1039 the 2021-03-02 22:29:37echo "HISTSIZE=2000" >> .bash_profile
1040 2021-03-02 22:29:58 echo "HISTFILETSIZE=2000" >> .bash_profile
1041 2021-03-02 22:30:20 source ~/.bash_profile
Copy the code
Since this command can change the number of history displays, it can also change the number of history stored in the file.
Imagine if you didn’t want the server to record history!
I’m just going to change both of them to 0.
Can try by oneself ha!
6. Clear historical records
To clear historical records, run history -c
Second, the summary
- To view history, use history
- Search history using CTRL + R
- Repeat the previous command!! Or!!! – 1
- Execute the corresponding serial number command! 1020
- HISTSIZE is the number displayed when executing history
- HISTFILETSIZE Number of historical records saved
- Use history-c to clear the history
This is the history command in Linux. Execute the previous command and search the command history with another command, CTRL + R.
Adhering to learning, blogging and sharing is the belief that Kakha has been upholding since she started her career. I hope the articles on the Internet can bring you a little help. I’m Kaka. See you next time.