How to delete multiple entries from history?

Joined
Apr 16, 2023
Messages
149
Reaction score
16
Credits
1,460
I'm studying from linux-training.be and Nehra Classes. And they teach that doing
Code:
history -d {1..95}
will delete the history completely. But it's not happening. Can you guys tell me what's the proper way to delete multiple history?

I could do history -d 1 2 3 4 5 upto 95 but that's not very fun to do.
 


  1. Open your web browser (e.g., Google Chrome, Mozilla Firefox, Microsoft Edge).
  2. Access the browser's settings or preferences. This can usually be done by clicking on the menu icon (three vertical dots or horizontal lines) located at the top-right corner of the browser window.
  3. Look for the "History" or "Privacy & Security" section within the settings.
  4. Within the history settings, you should find an option to clear browsing data or delete history. Click on it.
  5. A new window will appear with various options to choose from. Select the time range for which you want to delete the history. If you want to delete everything, choose "All time" or a similar option.
  6. Make sure the option to delete browsing history or similar items is selected. You can also choose to delete other types of data such as cookies, cached images, and passwords if desired.
  7. Click on the "Clear" or "Delete" button to initiate the process.
  8. Depending on the amount of data to be deleted, it may take a few moments to complete. Once finished, you will receive a confirmation message.
Please note that deleting your browsing history will remove all traces of the websites you visited, cookies, saved passwords, and other browsing data. This action cannot be undone, so be cautious before proceeding.
you must be joking right? I'm talking about linux command line history.
 
Sorry to bring this up, but here is where a tiny bit of shell scripting experience can help. Try this command in a single line:

Code:
for i in {95..1}; do history -d "$i"; done

-> Pay attention to the reverse order of the loop. The history values renumber themselves after you delete an entry. It happens on each iteration of the loop. (If you run with increasing values, and delete history item #1, then history item 2 becomes 1, then 3 becomes 2, ... then the loop increments, skips one, and deletes the wrong next value.)

I am not a good shell script-er myself, so I wrote it and tested it on a terminal in Mint and noticed the renumbering issue.
 
you must be joking right? I'm talking about linux command line history.
Yeah, they were so eager to respond quickly that they failed to read the original question properly or check their answer to see if it addresses the original question.


P.S. (added later):
I saw your other thread about "How relevant are these materials to RHCSA certification?" and noticed the first line which said, "Chapter 16. shell history".
-> The first thing that popped into my head was, "Well, there was the Bourne shell and then C shell and then Zsh, then bash, and then ..." (That order of appearance may not be correct, by the way.)
 
Last edited:
I have this in my notes:

Code:
cat /dev/null > ~/.bash_history

I don't actually clean my bash history. I typed those commands on purpose. I intentionally don't store commands in my bash history that I don't want in my bash history. So, it's a pretty curated file.

But, that command should do it for you.

If you have a 2nd terminal open, you might want to close it first. (Which might be OP's original problem.)

If there's a 2nd terminal open, that will retain some (?all - I'd have to check again) of the history and write the history back when closed.
 
I have this in my notes:

Code:
cat /dev/null > ~/.bash_history

I don't actually clean my bash history. I typed those commands on purpose. I intentionally don't store commands in my bash history that I don't want in my bash history. So, it's a pretty curated file.

But, that command should do it for you.

If you have a 2nd terminal open, you might want to close it first. (Which might be OP's original problem.)

If there's a 2nd terminal open, that will retain some (?all - I'd have to check again) of the history and write the history back when closed.
This will clean all of bash history. What if I just want to clear a range of command history?
 
The following will delete the entries in the history file from 1997 to 2004.
Code:
history -d 1997-2004
The history command shows the history as a list of commands numbered consecutively. The user can select a range to delete in accordance with the template: history -d <start>-<end>

See: man bash, and search for: history -d start-end.
 
Hi,

here is a simple way:
Code:
#disabel history for current session
unset HISTFILE
#open history file and delete the lines you want for example user johndoe
vim /home/jondoe/.bash_history
#close current shell
exit
 
S
The following will delete the entries in the history file from 1997 to 2004.
Code:
history -d 1997-2004
The history command shows the history as a list of commands numbered consecutively. The user can select a range to delete in accordance with the template: history -d <start>-<end>

See: man bash, and search for: history -d start-end.
so the teacher was right? Thanks.
 
The following will delete the entries in the history file from 1997 to 2004.
Code:
history -d 1997-2004
S

so the teacher was right? Thanks.
You can also look this up in "man bash"
history[n]
-d start-end
Delete the history entries between positions start and end, inclusive. Positive and negative values for start and end are interpreted as described above.
 
This will clean all of bash history. What if I just want to clear a range of command history?

I thought that was what you wanted - just to delete those entries - but you said "will delete the history completely." So, I gave you that.

I probably should have asked for clarification. I did not.
 

Members online


Top