Using the history command in Linux to improve efficiency

  



Whether you are a Linux command line newbie or a veteran, these tips can help you improve, save time, improve efficiency, increase Interest ….

History Commands

These techniques are mostly using the history command. We first enter
history

and then you will see something like the following < Br>


To execute a command in history, just use ! plus the line number before the command.
!510


You You can also see when a command was executed. Let's see what the commands are before the 3 commands.
!-3


Quick replacement

Assuming you I want to execute the previous command. Just enter two exclamation points directly. (Translator's Note: This may not be as convenient as the arrow keys, but the following is the real effect). When you execute a command, you find that you forgot to add sudo. This command works.
sudo !!


Assuming you want to run a command, the parameters of this command are already in the previous command, but you can type it again. It’s too much trouble, well, use it directly!$ You can use it.
cd !$


If you have already run a command with two parameters, but you want to use the first parameter, you can use !^ Command, this command is very useful when you back up a file and then want to edit the original file again.
nano !^

(note the last command in the figure)


< Next, let us search for a specific command from your history. Press Ctrl+R, when you type, it will automatically search. You can use the arrow keys to find the command you want.


If you know the specific keywords of the commands you have run, you can skip the search step and find them directly.
!keyword(keyword can be changed to your keyword)


Clear History

If you want to clear the history, use the following command
history –c

If you want to disable the history command, use the following command
HISTSIZE=0

If you want to enable it again, you can change 0 to a certain value. (The default value is usually 500 or 1000.) Use the following command to make the history ignore commands starting with spaces.
HISTCONTROL=ignorespace

Under In the fourth line, the cd is preceded by a space, so the history ignores


Remove the duplicate

Use the following command to ignore the repeat command.
HISTCONTROL=ignoredups


If you want to ignore spaces and want to ignore duplicates, use the following command
HISTCONTROL=ignoreboth

Copyright © Windows knowledge All Rights Reserved