Teach you to hide Linux command line history _unix Linux

Source: Internet
Author: User

The Linux operating system defaults to the historical command of user actions, and if you are a user of the Linux command line, sometimes you may not want certain commands to be recorded in your command line history. There may be many reasons, for example, when you hold a position in a company, you have some privileges that you do not want to be abused by others. Or some particularly important commands that you don't want to perform incorrectly while browsing the history list. However, there are ways to control which commands go into the history list and which ones do not enter? Or in other words, can we open a browser-like pattern in a Linux terminal? The answer is yes, and there are many ways to do it, depending on the specific goals you want.

In this article, we will discuss some effective methods.

Note : All the commands presented in this article have been tested under Ubuntu .

Different feasible methods

The previous two methods have been described in a previous article. If you already know, this part can be skipped. However, if you do not understand, suggest careful reading.

1. Insert a space before the command

Yes, I did not see it wrong. Inserting a space in front of the command is ignored by the shell, which means it does not appear in the history. But this method has a premise, only if your environment variable Histcontrol set to "Ignorespace" or "Ignoreboth" will not work. In most cases, this is the default value.

So, like the following command (LCTT: here [space] means to enter a blank):

[Space]echo "This are a top secret"

If you have previously executed the following command to set the environment variable, the above command will not appear in the history.

Export Histcontrol = Ignorespace

The screenshot below is an example of this approach.

The fourth "echo" command is not recorded in history because it has a space in front of it.

2. Disable all history of the current session

If you want to disable all history of a session, you can simply clear the value of the environment variable histsize before you start the command line work. Perform the following command to clear its value:

Export histsize=0

Histsize represents the number (number of rows) of commands that can be saved in the history list of a bash session. By default, it sets a value of not 0, for example, on my computer, it has a value of 1000.

So the command mentioned above sets its value to 0, and the result is that until you close the terminal, nothing is stored in the history record. Remember, too, you can't see the previously executed command by pressing the UP ARROW key or by running the history command.

3. Clear the entire history after the end of the work

This can be seen as another implementation of the scenario proposed in the previous section. The only difference is that you execute this command after you have done all the work. Here is the order that you just mentioned:

History-cw

As mentioned earlier, this and Histsize methods have the same effect.

4. Close history only for your work

Although the methods described previously (2 and 3) can be used to achieve the goal, they can erase the entire history, and in many cases some may not be what we expect. Sometimes you may want to save the history of your work until you start the command line. For such a requirement, you begin to perform the following command before you work:

[Space]set +o History

Note: [space] denotes spaces. And because of the space, the command itself will not be recorded.

The above command temporarily disables the history feature, which means that all of the actions you perform after this command are not recorded in history, but everything before this command is recorded as it is in the history list.

To reopen the history feature, execute the following command:

[Space]set-o History

It restores the environment to its original state, that is, you have completed your work, and commands that follow these commands will appear in history.

5. Delete the specified command from the history record

Now assume that the history contains some commands that you don't want to record. What are we going to do in this situation? very simple. Delete them directly. Remove by using the following command:

History | grep "part of command", want to remove

The command above will output a matching command in the history, with a number in front of each bar.

Once you have found the command you want to delete, execute the following command to delete the specified entry from the history record:

history-d [num]

Here is a screenshot of this example.

The second ' echo ' command was successfully deleted.

(LCTT: If you do not want the above order to be recorded in history, you can add a space before the above command)

Similarly, you can use the UP arrow to go back and look at the historical record. When you find that the command that you are interested in appears on the terminal, press Ctrl + U to clear the entire row, and also delete it from the history record.

Summarize

The above is a summary of the command-line history of hiding Linux, and there are a number of different ways to manipulate the history of the Linux command line to meet your needs. But keep in mind that hiding or deleting commands from history is often not a good habit, although in essence this is not a mistake. But you have to know what you're doing, and what the consequences might have been. So please be careful with the small partners who use these commands!

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.