Skip to main content
deleted 41 characters in body; edited tags
Source Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266

To prevent logging "dangerous" commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log "dangerous" commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible. Any comment would be greatly appreciated.

Update and clarification:

The main reason why this is a problem for me is that I am usually connected to my machine from several terminals, and any command executed on one terminal is immediately read into history of other terminals. This is achieved by

PROMPT_COMMAND="history -a; history -c; history -r" 

LetsLet's imagine I have two terminals open. In one I have some cat /dev/foo > file.out process running. In the second, I check the progress with ls -lAhF. I keep repeating ls by pressing Up and ENTER (that is, last command from history). As soon as the first command finishes, the last command from history is no longer ls, but cat /dev/foo > file.out. If I am not careful, I will start cat again and overwrite file.out.

What I would like to achieve is that the cat command would be preceded with a #, so that it would not be executed. I would, however, still see it in history and can reuse it (if it is a long command) by un-commenting it.

To prevent logging "dangerous" commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log "dangerous" commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible. Any comment would be greatly appreciated.

Update and clarification:

The main reason why this is a problem for me is that I am usually connected to my machine from several terminals, and any command executed on one terminal is immediately read into history of other terminals. This is achieved by

PROMPT_COMMAND="history -a; history -c; history -r" 

Lets imagine I have two terminals open. In one I have some cat /dev/foo > file.out process running. In the second, I check the progress with ls -lAhF. I keep repeating ls by pressing Up and ENTER (that is, last command from history). As soon as the first command finishes, the last command from history is no longer ls, but cat /dev/foo > file.out. If I am not careful, I will start cat again and overwrite file.out.

What I would like to achieve is that the cat command would be preceded with a #, so that it would not be executed. I would however still see it in history and can reuse it (if it is a long command) by un-commenting it.

To prevent logging "dangerous" commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log "dangerous" commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible.

Update and clarification:

The main reason why this is a problem for me is that I am usually connected to my machine from several terminals, and any command executed on one terminal is immediately read into history of other terminals. This is achieved by

PROMPT_COMMAND="history -a; history -c; history -r" 

Let's imagine I have two terminals open. In one I have some cat /dev/foo > file.out process running. In the second, I check the progress with ls -lAhF. I keep repeating ls by pressing Up and ENTER (that is, last command from history). As soon as the first command finishes, the last command from history is no longer ls, but cat /dev/foo > file.out. If I am not careful, I will start cat again and overwrite file.out.

What I would like to achieve is that the cat command would be preceded with a #, so that it would not be executed. I would, however, still see it in history and can reuse it (if it is a long command) by un-commenting it.

Notice removed Authoritative reference needed by Martin Vegter
Bounty Ended with Stéphane Chazelas's answer chosen by Martin Vegter
added 4 characters in body
Source Link
Martin Vegter
  • 848
  • 81
  • 257
  • 449

To prevent logging "dangerous" commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log "dangerous" commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible. Any comment would be greatly appreciated.

Update and clarification:

The main reason why this is a problem for me is that I am usually connected to my machine from several terminals, and any command executed on one terminal is immediately read into history of other terminals. This is achieved by

PROMPT_COMMAND="history -a; history -c; history -r" 

Lets imagine I have two terminals open. In one I have some cat /dev/foo > file.out process running. In the second, I check the progress with ls -lAhF. I keep repeating ls by pressing Up and ENTER (that is, last command from history). As soon as the first command finishes, the last command from history is no longer ls, but cat /dev/foo > file.out. If I am not careful, I will start cat again and overwrite file.out.

What I would like to achieve is that the cat command would be preceded with a #, so that it would not be executed. I would however still see it in history and can reuse it (if it is a long command) by un-commenting it.

To prevent logging "dangerous" commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log "dangerous" commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible. Any comment would be greatly appreciated.

To prevent logging "dangerous" commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log "dangerous" commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible. Any comment would be greatly appreciated.

Update and clarification:

The main reason why this is a problem for me is that I am usually connected to my machine from several terminals, and any command executed on one terminal is immediately read into history of other terminals. This is achieved by

PROMPT_COMMAND="history -a; history -c; history -r" 

Lets imagine I have two terminals open. In one I have some cat /dev/foo > file.out process running. In the second, I check the progress with ls -lAhF. I keep repeating ls by pressing Up and ENTER (that is, last command from history). As soon as the first command finishes, the last command from history is no longer ls, but cat /dev/foo > file.out. If I am not careful, I will start cat again and overwrite file.out.

What I would like to achieve is that the cat command would be preceded with a #, so that it would not be executed. I would however still see it in history and can reuse it (if it is a long command) by un-commenting it.

added 4 characters in body
Source Link
Martin Vegter
  • 848
  • 81
  • 257
  • 449

To prevent logging dangerous"dangerous" commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log dangerous"dangerous" commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible. Any comment would be greatly appreciated.

To prevent logging dangerous commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log dangerous commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible. Any comment would be greatly appreciated.

To prevent logging "dangerous" commands in bash history, I have added following line to my .bashrc file:

HISTIGNORE='rm *:mv *:cp *:cat*>*:pv*>*' 

this works well, but it has a side effect: I cannot see the complete history of commands executed on a machine. Let's say I have several machines for experiments, and I want to be able to see all commands executed. I would use the bash internal history to display executed commands, and perhaps grep for today's date:

history | grep Sep-28 

What I would like do have is to log "dangerous" commands as well, but put a # at the beginning of the line, so that if I happen to execute the command from history by mistake, no damage would be done.

I have no idea if this is possible. Any comment would be greatly appreciated.

Notice added Authoritative reference needed by Martin Vegter
Bounty Started worth 100 reputation by Martin Vegter
Tweeted twitter.com/#!/StackUnix/status/383882861499412481
edited title
Link
Martin Vegter
  • 848
  • 81
  • 257
  • 449
Loading
Source Link
Martin Vegter
  • 848
  • 81
  • 257
  • 449
Loading