Advertisements

How to Manage Command History on Linux Latest Information 2023

How to Manage Command History on Linux Latest Information 2023: The command history allows you to examine commands that you’ve previously executed in the terminal. With Linux, here’s how to manage your command history.

Advertisements

Every command you type in Linux is recorded in the history file. The history command allows you to read and reuse previous commands without having to input them again. The Up and Down keys are used to browse through the history list. Yet, there may be situations when you want to erase some commands from the past or stop the history storing feature completely.

Thankfully, Linux allows you to manage command logging features such as temporarily or permanently deactivating it, erasing the whole history, or preventing certain commands from being kept in the history list. Let’s have a look at how.

Manage Command History on Linux

Delete Entire Command History on Linux

In Linux, use the history command with the -c option to remove the whole command history from the beginning:

This will clear the history of commands you’ve already performed.

Delete Specific Commands From the History

You may choose to eliminate individual commands rather than erasing the entire history. In this scenario, you must find the number linked with that command in the history list. You may accomplish this by just typing:

You can select to delete certain commands rather than the full history. In this case, you must go through the history list for the number associated with that command. You may do this by just typing:

Execute a Command Without Including It in the History

Just put a Space before the command to execute it without it being noted in the history. To enable this functionality, use the following command to set the HISTCONTROL environment variable to ignorespace:

Just enter a Space before the command to perform it without it being recorded in the history. Use the following command to set the HISTCONTROL environment variable to ignore space to enable this functionality.

How to Enable/Disable Command History on Linux

Just type a Space before the command to execute it without it being saved in the history. To activate this feature, use the following command to set the HISTCONTROL environment variable to ignore space:

Ignore Certain Commands From Being Recorded in the History

To run the command without storing it to the history, simply place a Space before it. To use this functionality, use the following command to set the HISTCONTROL environment variable to ignorespace.

Control Your Bash Command History on Linux

The beauty of history is that you may now relive any of them by executing a command like:

How to Enable/Disable Command History on Linux

Just type a Space before the command to execute it without it being saved in the history. To activate this feature, use the following command to set the HISTCONTROL environment variable to ignorespace:

Ignore Certain Commands From Being Recorded in the History

To run the command without storing it to the history, simply place a Space before it. To use this functionality, use the following command to set the HISTCONTROL environment variable to ignorespace.

Control Your Bash Command History on Linux

The beauty of history is that you may now relive any of them by executing a command like:

$ !3

The!3 command at the prompt instructs the shell to execute the command on line 3 of the history list once more. I could also get to that command by typing:

$ !sudo dnf

This instructs history to look for the most recent command that fits the pattern you specified (in this example, dnf) and execute it.

Searching history

You may also utilise history by entering!! to replay the last command you entered. You may use it with grep to search for commands that match a text pattern, or you can use it with tail to locate the last few commands you did. As an example.

$ history | grep dnf
3  sudo dnf update -y
5  history | grep dnf

$ history | tail -n 3
4  history
5  history | grep dnf
6  history | tail -n 3

Another way to access this search feature is to use Ctrl-R, which will launch a recursive search of your command history. When you type this, the prompt changes to:

(reverse-i-search)`':

Now you may start entering a command and it will be shown for you to execute by hitting Return or Enter.

Changing an executed command

History can also be used to restart a command with alternative syntax. History may be used to revise history. For example, if I wish to replace the preceding command history | grep dnf to history | grep ssh, I may type at the prompt:

$ ^dnf^ssh^

The command is performed, but this time dnf is substituted with ssh. In other words, the following instruction is executed:

$ history | grep ssh

Removing history

You may wish to remove any or all of the commands from your history file at some point. If you wish to remove a specific command, use history -d line number>. Execute history -c to clear the full contents of the history file.

The history file is saved in a file that you may also alter. Users of the Bash shell will find it in their home directory as.bash history.

Next steps

There are several additional things you may do with history:

Change your history buffer size to a certain amount of instructions.

Make a note of the date and time for each line in history.

Stop certain orders from being logged in history.

Check out Seth Kenlon’s articles on parsing history, history search modifiers, and the GNU Bash Manual for additional information on the history command and other fascinating things you can do with it.

For More Informative Content Visit

Govt Pk

Leave a Comment