Teaches you to hide the command line history of Linux

Source: Internet
Author: User

If you are Linux command-line users, sometimes you may not want certain commands to be recorded in your command-line history. There may be many reasons, for example, you have a position in a company where you have some privileges that you do not want to be abused by others. Or some particularly important commands that you do not want to be executed incorrectly when you browse through the history list.

However, is there a way to control which commands go to the History list and which do not? Or in other words, can we Open a browser-like pattern in the Linux terminal? The answer is yes, and there are many ways to achieve it, depending on the target you want. In this article, Brother Lian Education www.lampbrother.net

Will discuss some effective methods with you.

Note: All the commands that appear in this article are Ubuntu under test.

Different possible methods

The previous two methods have been described in the previous article. If you already know, this part can be skipped. However, if you do not understand, it is recommended to read carefully.

1. Insert a space before the command

Yes, it's not wrong. Inserting a space before the command, which is ignored by the shell , means that it does not appear in the history. But this method has a premise, only in your environment variable histcontrol set to "Ignorespace" or "Ignoreboth" will only work. In most cases, this is the default value.

So, like the following command ( LCTT here: [space] indicates that a space is entered):

[Space]echo "This is a Topsecret"

The above command will not appear in the history if you have previously executed a command that sets environment variables as follows.

Export Histcontrol = Ignorespace

The following is an example of this approach.

Fourth One "echo" command because there are spaces in front of it, it is not recorded in history.

2. Disable all history for the current session

If you want to disable all history for a session, you can simply clear the environment variables before starting the command line work histsize the value can be. Execute the following command to clear its value:

Export histsize=0

histsize indicates that for Bash The number of commands (lines) that can be saved in the session's history list. By default, it sets a value other than 0, for example on my computer, which has a value of $ .

so the command mentioned above sets its value to 0 , and the result is that nothing is stored in the history until you close the terminal. Remember also that you cannot see the previously executed commands by pressing the UP ARROW keys or running the history command.

3. Clear the whole history after the work is over

This can be seen as another implementation of the scheme mentioned in the previous section. The only difference is that you execute the command after you have done all the work. Here are the commands you just said:

History-cw

mentioned just now, this and histsize method has the same effect.

4. Turn off history only for your work

Although the method described earlier ( 2 and the 3 can achieve the purpose, they can clear the entire history, in many cases, some may not be what we expect. Sometimes you may want to save the history until you start the command line work. For such a requirement, you begin to execute the following command before you work:

[Space]set +o History

remark: [space] represents a space. And because of the space, the command itself is not recorded.

The above command temporarily disables the history feature, which means that everything you do after this command is not recorded in history, but everything before the command is recorded in the History list as is.

To turn the history feature back on, execute the following command:

[Space]set-o History

It restores the environment, that is, you have done your work, and commands that follow the above command will appear in history.

5. Delete the specified command from the history record

Now assume that the history contains some commands that you do not want to record. What do we do in this situation? Very simple. Remove them directly from the manual. Delete by using the following command:

History | grep "Part of the command youwant to remove"

The command above will output a matching command in the history, and there will be a number in front of each one.

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

history-d [num]

a second ' Echo ' The command was successfully deleted.

Similarly, you can use the UP arrow to go back and look at the history. When you find that the command you are interested in appears on the terminal, pressing Ctrl + Uclears the entire row and removes it from the history.

Summarize

There are a number of different ways to manipulate Linux command-line history to meet your needs. Keep in mind, however, that hiding or deleting commands from history is often not a good habit, although it is not wrong in nature. But you have to know what you are doing and the possible consequences.



Teaches you to hide the command line history of Linux

Related Article

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.