Linux history remove line

How to delete a single command from history on a Linux/Unix Bash shell

Table of contents

How do I view history with line number?

Simply type the history command:
$ history
Sample outputs:

Fig.01: Bash history command with line number on a Linux, OS X, and Unix

By default, history is stored in

/.bash_history file. However, the bash variable named $HISTFILE defines the file’s name in which command history is saved. If unset, the command history is not saved when a shell exits. Use the printf command/echo command to display the current settings:

How to delete a single command number 1013 from history in Linux

Verify it:
$ history

Remove the bash history entry at position OFFSET. Negative offsets count back from the end of the history list.

How do I delete all the history?

Want to remove all bash history? Try the following syntax:
$ history -c
Add above command to your

/.bash_logout file to clean when you logout:
$ cat /dev/null >

/.bash_history && history -c
The -c passed to the history command to clear the history list by deleting all of the entries $.

Tip: Control bash history like a pro

First, you can increase your bash history size by appending the following config option in

  • No ads and tracking
  • In-depth guides for developers and sysadmins at Opensourceflare✨
  • Join my Patreon to support independent content creators and start reading latest guides:
    • How to set up Redis sentinel cluster on Ubuntu or Debian Linux
    • How To Set Up SSH Keys With YubiKey as two-factor authentication (U2F/FIDO2)
    • How to set up Mariadb Galera cluster on Ubuntu or Debian Linux
    • A podman tutorial for beginners – part I (run Linux containers without Docker and in daemonless mode)
    • How to protect Linux against rogue USB devices using USBGuard

Join Patreon

Save and close the file.

Where to find more information about history command?

Read bash man page by typing the following man command:
$ man bash
Another option is to type the following help command:
$ help history
Here is what I see on Bash version 5.0.17:

Conclusion

In this quick tutorial, you learned to remove a single command from bash history running on a Linux, macOS, *BSD, or Unix-like systems. I would suggest reading the bash man page for more information here.

🐧 Get the latest tutorials on Linux, Open Source & DevOps via

Tutorial requirements
Requirements Bash on Linux/Unix
Root privileges No
Difficulty Easy
Est. reading time 4 mintues
Category List of Unix and Linux commands
Documentation help • mandb • man • pinfo
Disk space analyzers df • duf • ncdu • pydf
File Management cat • cp • less • mkdir • more • tree
Firewall Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04
Linux Desktop Apps Skype • Spotify • VLC 3
Modern utilities bat • exa
Network Utilities NetHogs • dig • host • ip • nmap
OpenVPN CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04
Package Manager apk • apt
Processes Management bg • chroot • cron • disown • fg • glances • gtop • jobs • killall • kill • pidof • pstree • pwdx • time • vtop
Searching ag • grep • whereis • which
Shell builtins compgen • echo • printf
Text processing cut • rev
User Information groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w
WireGuard VPN Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04

Comments on this entry are closed.

Occasionally I will mistakenly paste a bunch of garbage on the command line, such as as some text that had been selected earlier. Sometimes this happens when copy/paste is not working correctly, or maybe I just made a mistake.

As I don’t want that junk in the command history, I will do the following:

history -w myhist
Edit myhist to remove unwanted lines
history -r myhist

Another use of this sequence of commands is to prime the history from a particular history file for a particular project. This saves a fair amount of typing at times.

I forgot to mention for those that may not be familiar with it, this is in the bash shell.

Hi,
Thanks a lot…
we can change history file format and add time of executed commands by HISTTIMEFORMAT variable….

example:
HISTTIMEFORMAT=”%D %T”

Yes. You can change the the file format.

I wonder. How do you intercept dangerous parameters on certain commands from ever being run. For instance could you block “rm -rf” ?

You may wan to try https://github.com/dvorka/hstr which brings easy history management (apart to suggest-box style navigation).

This doesn’t work on macOS 10.12.x

You may be using ZShell instead of Bash shell.

Источник

How to Clear BASH Command Line History in Linux

The bash history keeps a record of all commands executed by a user on the Linux command line. This allows you to easily run previously executed commands by using the “up arrow” or “down arrow” keys to scroll through the command history file.

In this article, we will show you two simple ways to clear your command-line history on a Linux system.

The major reason for removing command-line history from the Linux terminal is to prevent another user, who could be using the same account.

For instance if you have typed a command that contained a password in plain-text and you don’t want another system user or an attacker to see this password, you need to delete or clear the history file.

Take a look at the command below, here the user aaronkilik has typed the database server password on the command line.

If you look into th bash history file towards the end, you will see the password typed above in there.

Check Last Executed Commands

The bash_history file is normally located in a user’s home directory /home/username/.bash_history.

To remove a single line from the history file, use the -d option. For example, if you want to clear a command where you entered clear-text password as in the scenario above, find the line number in the history file and run this command.

To delete or clear all the entries from bash history, use the history command below with the -c option.

Alternatively, you can use the command below to delete history of all last executed commands permanently in the file.

Note: A normal user can only view his/her own command history, but the root user can view the command history of all other users on the system.

You can learn more about the bash history file and useful history commands here: The Power of Linux “History Command” in Bash Shell.

Always remember that all commands you run are recorded in a history file, so do not type plain-text passwords on the command line. If you have questions or thoughts to share with us, make use of the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

We are thankful for your never ending support.

Источник

How do I clear the terminal History?

I am using Linux Mint 17.1 Rebecca for about 2 days and accidentally typed my password into the terminal which is now displayed in the history list of commands I have previously typed.

I want to clear the terminal history completely. I have tried using the following commands in the terminal which I thought would clear the history forever but they do not:

The above commands «will» clear the history from the terminal but when I exit and bring up a new one all my previous history is still there and can all be listed again using the — history command and also by pressing the UP arrow on my keyboard. I do not want this to happen until I have totally cleared my history, then I want to continue using it.

How can I clear my terminal history completely — forever and start fresh?

Please Note: I do not want to exit the terminal without saving history just clear it forever in this one instance.

/.bash_history && history -c && exit

7 Answers 7

reset or tput reset only does things to the terminal. The history is entirely managed by the shell, which remains unaffected.

history -c clears your history in the current shell. That’s enough (but overkill) if you’ve just typed your password and haven’t exited that shell or saved its history explicitly.

When you exit bash, the history is saved to the history file, which by default is .bash_history in your home directory. More precisely, the history created during the current session is appended to the file; entries that are already present are unaffected. To overwrite the history file with the current shell’s history, run history -w .

Instead of removing all your history entries, you can open .bash_history in an editor and remove the lines you don’t want to keep. You can also do that inside bash, less conveniently, by using history to display all the entries, then history -d to delete the entries you don’t want, and finally history -w to save.

Note that if you have multiple running bash instances that have read the password, each of them might save it again. Before definitively purging the password from the history file, make sure that it is purged from all running shell instances.

Note that even after you’ve edited the history file, it’s possible that your password is still present somewhere on the disk from an earlier version of the file. It can’t be retrieved through the filesystem anymore, but it might still be possible (but probably not easy) to find it by accessing the disk directly. If you use this password elsewhere and your disk gets stolen (or someone gets access to the disk), this could be a problem.

Источник

Remove a certain line from Bash history file

How can I remove a certain line from history ‘s database?

12 Answers 12

You need to log out and back in or run history -a so the current history is committed to disk.

Then just edit the file

/.bash_history but rather $.

/.bash_history ) with a space; commands starting with a space usually don’t get logged (see $HISTCONTROL ).

You can achieve removal from the history file using the commandline in two steps:

  1. Typing history -d
  2. deletes a specified line from the history in memory.
  3. Typing history -w writes the current in-memory history to the

The two steps together remove the line permanently from the in-memory history and from the .bash_history file as well.

To prevent a command from being added to the history in the first place, make sure that the environment variable HISTCONTROL contains among its colon-separated values the value ignorespace , for example (add e.g. to .bashrc ):

This will prevent any command with a leading space from being added to the history. You can then clear the history completely by running

First of all, if the command you’re about to issue is sensitive, unsafe, or you just don’t need it cluttering up your history, it is best/quickest to just prevent it from entering the history in the first place. Make sure that $HISTCONTROL contains ignorespace :

Then proceed any command you don’t want in your history with a space:

If you accidentally put an unwanted command into history, providing that your bash session is still open, the command hasn’t yet touched the disk. To delete the previous command in history, issue:

Note the leading space; this command requires ignorespace , otherwise it’ll just delete itself!

If you want to delete the last few commands, find the last and first history number:

In this case 602 and 599. Then issue:

(Without ignorespace , it would be 603..599.)

If you don’t want any history from your current session to hit the disk, exit using:

The approach so far is to not even let sensitive history items be written to disk for extra security, because in theory data deleted from non-volatile media can still be recovered.

If, however, the command(s) you wish to remove are from a previous session, they will have already been appended to the $HISTFILE on exit. Manipulating the history with the above commands will still only append the remaining new items to the $HISTFILE , on exit. To overwrite the $HISTFILE with the current session’s view of the entire history, right now, issue:

Of course for history items already on disk, the alternative to editing the history with history -d commands then issuing history -w , is to edit the $HISTFILE with a text editor.

Prevent sensitive information from being stored in the history file

If you’ve entered some password on a command line, then realize that all commands are logged, you could either:

Force exit the current session without saving history:

This will drop all current history.

Type ↑ (up arrow) in the open bash session until the sensitive information is shown, then use line editing keystrokes like Ctrl + W to delete the sensitive info, and then ↓ (down arrow) until a new empty line is prompted, before typing Enter .

Delete sensitive information from the history file

If you realize that sensitive information is already stored, and you want to delete it, but not your entire history:

A simple sed command could do the job:

but, as you type this, you create another history line containing the search pattern (sensitive info) you are trying to delete. So you could:

This will run head -n1 with input from the terminal. It will appear that your terminal is hung (there won’t be a prompt); just type the information that you want to delete from the file. This is a trick to let you enter (part of) a command without actually typing it into the command line, thus making it ineligible for inclusion in the history record. Then sed will use the text that you typed to search .bash_history and delete all lines containing the sensitive info. Note: if your sensitive information pattern contains slash(es), you must escape them with backslashes, or else change the sed command to use this syntax to specify a delimiter that does not appear in the pattern:

Another way could be to delete only the sensitive info, but keep the commands that contain the information. For this, you could simply replace sensitive info with substitute text of your choice:

Delete sensitive information from any file in a specific tree

Finally, to be sure that this won’t stay in another forgotten file:

will list all concerned files.

will replace all occurrences of sensitive info in all files in the directory tree rooted at . . Note: even though this command uses xargs -0 , it will not handle files with newlines in their names.

Источник

Читайте также:  Файловая система для windows 64 bit
Оцените статью