How to avoid duplicate entries in .bash_history

1

I’m trying to prevent bash from saving duplicate commands to my history. Here’s what I’ve got:

shopt -s histappend export HISTIGNORE='&:ls:cd ~:cd ..:[bf]g:exit:h:history' export HISTCONTROL=erasedups export PROMPT_COMMAND='history -a'

This works fine while I’m logged in and .bash_history is in memory. For example:

$ history 1 vi .bashrc 2 vi .alias 3 cd /cygdrive 4 cd ~jplemme 5 vi .bashrc 6 vi .alias $ vi .bashrc $ history 1 vi .alias 2 cd /cygdrive 3 cd ~jplemme 4 vi .alias 5 vi .bashrc $ vi .alias $ history 1 cd /cygdrive 2 cd ~jplemme 3 vi .bashrc 4 vi .alias $ exit

But when I log back in, my history file looks like this:

$ history 1 vi .bashrc 2 vi .alias 3 cd /cygdrive 4 cd ~jplemme 5 vi .bashrc 6 vi .alias 7 vi .bashrc 8 vi .alias

What am I doing wrong?

EDIT: Removing the shopt and PROMPT_COMMAND lines from .bashrc does not fix the problem.

Can you prevent a command from going into the bash...

0 0
2
I

‘m working in Ubuntu bash terminal application and remotely on a RHEL server in cloud platform. I typed the wrong and dangerous command. I no longer wish to remember dangerous command in the history file. How can I remove or delete a single command from bash history file?


You can use the

history command to clear all history

or selected command line. In this tutorial,

you will learn how to clear a specific command from bash history in Linux, MacOS, and Unix-like systems

.

How do I view history with line number?

Simply type the history command:
$ history
Sample outputs:

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

## Delete the bash history entry at offset OFFSET ## history -d offset history -d number history -d 1013

Verify it:
$ history

How do I delete all the history?

The syntax is:
history -c
Add above command to your ~/.bash_logout file to clean...

0 0
3

I'm writing my PROMPT_COMMAND="history -a;$PROMPT_COMMAND" but I still get some duplicates from different terminal sessions. It seems I still will get some duplicates but I get less using the command PROMPT_COMMAND="history -w;$PROMPT_COMMAND".

I know I can do the history -a;history -c; history -r but I don't want them all synced. I only want that to occur when I call history -n. I'm basically down to using either history -a or history -w but I can't seem to find the difference between the two. Which one would be better to have to avoid as many duplicates as possible.

You stated two questions:

What is the difference between history -a and history -w?

history -a will append your current session history to the content of the history file.

history -w will replace the content of the history file with your current session history.

Which one avoids more duplicates?

Theoretically neither. Neither -a nor -w checks for duplicates. Practically...

0 0
4
Related to : How to remove duplicate line by perl or bash?how to remove duplicate words from a line of a file using perl Programming Languages

I am new to perl and looking for a perl solution for a relatively simple problem. I want to remove duplicate words from a line of a file using perl. All the examples on web deal with duplicate lines, but I am looking for removing duplicate words. Any help is greatly appreciated.

Here is my file


lot=list("XI_PBIAS.mMN_VDD_CAP1_BM" "XIBIAS2.mNCS_BIAS_BM" "XIBIAS2.mNCS_BIAS_BM" "XIBIAS1.mNCS_PCAS_BIAS_BM" "XIBIAS1.mNCS_PCAS_BIAS_BM" "XI_PBIAS.mNCS_PCS_BIAS_BM" )
filename = "tx_dac_bias_tb"
openResults(strcat(filename ".raw"))

I want the strings "XIBIAS2.mNCS_BIAS_BM" and "XIBIAS1

Remove new line character (merge two line into one) in specific line in bash Programming Languages

I have a file coming from 3rd party system which I need to correct a little bit. From time to time upcoming file...

0 0
5

I'm trying to prevent bash from saving duplicate commands to my history. Here's what I've got:

shopt -s histappend export HISTIGNORE='&:ls:cd ~:cd ..:[bf]g:exit:h:history' export HISTCONTROL=erasedups export PROMPT_COMMAND='history -a'

This works fine while I'm logged in and .bash_history is in memory. For example:

$ history 1 vi .bashrc 2 vi .alias 3 cd /cygdrive 4 cd ~jplemme 5 vi .bashrc 6 vi .alias $ vi .bashrc $ history 1 vi .alias 2 cd /cygdrive 3 cd ~jplemme 4 vi .alias 5 vi .bashrc $ vi .alias $ history 1 cd /cygdrive 2 cd ~jplemme 3 vi .bashrc 4 vi .alias $ exit

But when I log back in, my history file looks like this:

$ history 1 vi .bashrc 2 vi .alias 3 cd /cygdrive 4 cd ~jplemme 5 vi .bashrc 6 vi .alias 7 vi .bashrc 8 vi .alias

What am I doing wrong?

EDIT: Removing the shopt and PROMPT_COMMAND lines from .bashrc does not fix the problem.

-------------Problems...

0 0
6

A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. A value of erasedups causes all previous lines matching the current line to be removed from the history list before that line is saved. Any value not in the above list is ignored. If HISTCONTROL is unset, or does not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIGNORE. The second and subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of...

0 0
7

If you're using the default terminal shell, you might be annoyed by the fact that repeating the same command with duplicate the entry in the history file. ie: If you were to type the following commands:

echo hello world echo hello work echo hello work echo hello work

You would have to press the up arrow four times to get back to hello world. I do a bunch of redundant work in the terminal, so this bugs me. Here's how to fix it: edit your ~/.cshrc file and add the following line:

set histdup='all'

Restart your terminal, and you should be good to go. With this setting, all duplicate entries will be ommitted from history (run the command

history

to test this if you want). Note that you can also

set histdup='prev'

to ommit only immediate duplicates (ie it won't add the command if it was the previous command).

Hope this...

0 0
8

In this article I will show you some bash history magic. With the following settings added in your ~/.bashrc file, you will make your history:

Remember everything from every terminal Is instanly accesible from every terminal Doesn’t skip random commands if there are spaces at the front of the commands

So, if you are that guy that always has more than 3 terminals open, after you add these lines in your ~/.bashrc file no command will be forgotten from your history, and you will be able to ls in one terminal and than press the up arrow on another runnig terminal and get your ls command.

# avoid duplicates
export HISTCONTROL=ignoredups:erasedups
# big big history
export HISTSIZE=100000
export HISTFILESIZE=100000
# append history entries
shopt -s histappend
# After each command, save and reload history
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

Share and...

0 0