Linux history clear one command

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 to Clear Linux Command Line History

You may want to clear the history file and the screen for security reasons. Some Linux distributions may clear the screen when you logout but others do not. Many programs read input as a single line at a time.

The GNU history library is able to keep track of those lines, associate arbitrary data with each line, and utilize information from previous lines in composing new ones. Bash and other shells may use this history library. The default file is

Bash’s history feature

bash’s history function depends on a variable called HISTFILE, normally set to the current user’s .bash_history file (located in the user’s home directory). When echoed, it returns the full path and name of the user’s history file, like so:

1) Remove Linux history command

History can be reset with some command but after the operation, if you logout and login in your shell, you will see the same history

a. Delete the previous commands

You can use history -c command to clear the previous history command 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.

The example below shows our current history

Now let’s use the command.

When you exit bash, the history is saved to the history file. The history created during the current session is appended to the file, entries that are already present are unaffected. Let’s check the new history

Let us add some command to our history.

To overwrite the history file with the current shell’s history, run history -w after history -c command

After login again, let’s us check our history

You can see that our history begin at history -w entry command.

b. Delete a single command

You can delete the history’s entries which you don’t want with the -d option. This will delete the history entry at position offset. But when you close terminal and open it again histories are not deleted. So we finally use history -w to save the changes.

Now, if you want to delete the sixth entry which is mkdir command just use:

You can see that we didn’t have mkdir command entry above.

2) Clear bash completely

To clear the bash history completely on the server, an alternative solution is to link

/.bash_history to /dev/null

However, one annoying side-effect is that the history entries is linked to the memory and it will flush back to the file when you log out. To workaround this, you can use the following command:

3) Turn off bash history

You can stop logging history using one of the two ways: turn it off for all users, or turn off logging history for a single user.

a. Turn off for all users

You can turn off the bash history for all user adding unset HISTFILE line in /etc/profile file. This line deactivate the history file of each user on the system

You need to have the permission to apply the command above

b. Turn off for a specific user

The command above, it is possible to turn off the bash history of a specific user. You just need to indicate his bash_profile file.

Every the user will login, his history will be reset as below. All his history command will save until user logout

c. Edit .bashrc

You can remove the history command by editing two values of history command parameters.

  • HISTSIZE which is the number of lines or commands that are stored in memory in a history list while your bash session is ongoing
  • HISTFILESIZE which saves the amount of lines used for the history stack when it’s written to the history file.

To do it, edit your .bashrc and add

Now you can successfully delete the bash history and even stop logging to bash history using any of the above-listed commands.

4) Delete some entries lines

You can use history -d offset built in to delete a specific line from the current shell’s history. It’s not really practical if you want to remove a range of lines since it only takes one offset as an argument, but you could wrap it with a loop.

Just this one liner in the command prompt will help.

for i in <1..N>; do history -d START_NUM; done

Where START_NUM is starting position of entry in history N is no of entries you may want to delete.

Let’s check the last 15 entries of our history command

Now let us delete 13 entries beginning at the line 986

for i in <1..13>; do history -d 986; done

Now let’s check the result

You can see that we don’t have the same result.

When a user logs in with either a login or interactive/non-login shell, the user’s .bash_history file is opened. Now can operate on this file with some useful commands. The history is useful to retrieve commands used before but you can need to delete some entries of theses used commands. The

/.bash_history file does not record what you type in response to other programs’ prompts, just what you type at the bash prompt itself.

Источник

Читайте также:  Отличие windows samsung 860 evo jn 860 pro
Оцените статью