LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   how to edit history to remove few commands executed (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-edit-history-to-remove-few-commands-executed-817122/)

g_ramesh 06-30-2010 02:36 AM

how to edit history to remove few commands executed
 
Hi

suppose when I issue history command it is showing 100 entries with number attached to each command executed.

please tell me how to edit the history to remove few commands executed by me so as to protect the system from other users.

acid_kewpie 06-30-2010 02:46 AM

That's a pretty odd request, and only really goes to highlight someone trying to hide the fact that they have done stuff they don't want anyone else finding out about... Other users should be other user accounts, so as long as they don't have root access, they can't see your bash history file. you can just mess with the ~/.bash_history file if you really have to, but note that it's not written to until you log out, so recently run commands will not be in there yet.

g_ramesh 06-30-2010 05:44 AM

Quote:

Originally Posted by acid_kewpie (Post 4019049)
That's a pretty odd request, and only really goes to highlight someone trying to hide the fact that they have done stuff they don't want anyone else finding out about... Other users should be other user accounts, so as long as they don't have root access, they can't see your bash history file. you can just mess with the ~/.bash_history file if you really have to, but note that it's not written to until you log out, so recently run commands will not be in there yet.

Hi acid_kewpie

my intention is not that. I only want to save the system from users who are enthusiastic but not knowing what they are doing?

unSpawn 06-30-2010 06:14 AM

Quote:

Originally Posted by g_ramesh (Post 4019045)
when I issue history command it is showing 100 entries with number attached to each command executed.

The command history list has line numbers so you can call up a history entry by number.


Quote:

Originally Posted by g_ramesh (Post 4019045)
how to edit the history to remove few commands

'man bash' (as in please do consult the manual) says "history -d offset: Delete the history entry at position offset".


Quote:

Originally Posted by acid_kewpie (Post 4019049)
you can just mess with the ~/.bash_history file if you really have to, but note that it's not written to until you log out, so recently run commands will not be in there yet.

You can if you set 'shopt -s histappend; export PROMPT_COMMAND='history -a';' in your ~/.bashrc.


Quote:

Originally Posted by g_ramesh (Post 4019188)
my intention is not that.

Don't they all say that.. ;-p


Quote:

Originally Posted by g_ramesh (Post 4019188)
I only want to save the system from users who are enthusiastic but not knowing what they are doing?

Then education is in order, not deletion. Also see 'man bash' for HISTIGNORE.

alli_yas 06-30-2010 06:18 AM

Code:

I only want to save the system from users who are enthusiastic but not knowing what they are doing?
Well if that is the case then you can limit the privileges of those "enthusiastic" users by removing them from your /etc/sudoers; and playing with the permissions of the various applications you don't want them to run/modify.

blueskiesokie 07-07-2010 10:49 AM

so no one else has accidentalyy typed there password in the wrong window or box, only to have it show in history. And would not like to delete all history, or change favorite password?

hmmmm.

acid_kewpie 07-07-2010 12:50 PM

Quote:

Originally Posted by blueskiesokie (Post 4026415)
so no one else has accidentalyy typed there password in the wrong window or box, only to have it show in history. And would not like to delete all history, or change favorite password?

hmmmm.

hmmmm? What's hmmm about that?? :confused:

Bratmon 07-07-2010 01:37 PM

Quote:

Originally Posted by blueskiesokie (Post 4026415)
so no one else has accidentalyy typed there password in the wrong window or box, only to have it show in history. And would not like to delete all history, or change favorite password?

hmmmm.

That shouldn't be needed, as the default permission on .bash_history is 600.

Tinkster 07-07-2010 01:47 PM

Quote:

Originally Posted by blueskiesokie (Post 4026415)
... or change favorite password?

hmmmm.

Favourite password sounds dangerous :}

herthh 12-20-2010 12:29 PM

Not the right answer... Need information, not acid.
 
Quote:

Originally Posted by acid_kewpie (Post 4019049)
That's a pretty odd request, and only really goes to highlight someone trying to hide the fact that they have done stuff they don't want anyone else finding out about... (snip)

Pardon me, but the question is very legitimate and you put it on a bad spin ("odd request", "trying to hide").

The most important reason for deleting a command is that it might have been a typo, or even a wrong operation or a destructive operation (like a bad rm), and to recall such over and over again is one of the most infuriating things in the Linux/Unix command line history. I have sessions with several shells sharing the history and to find some odd ancient command popping up after every logout and a login is to put it simply, a waste of time.

I have also repeatedly been looking for a tool allowing me to selectively remove some commands from my history. On Windows we have this great developer shell "take command" (former 4nt) by JP Soft. There I can flexibly adjust the way how history works (append or merge a command, reorder to be last etc, etc) and I can selectively remove some commands by selecting them and pressing "del". I do such command cleanup on a regular base, foremost when a phase of a project is over.

Lack of it is a titanic inconvenience with 'fc' on Unix. On several occasions I used strings on .sh_history and recreated this file by executing commands which matter because this recalling of what I thought should have been replaced by more recent commands many times over, but it was not. The problem is that when more than one shell shares the history file, I think that it will be overwritten by the last logout, and its history. But I am not sure, such erratic are the results. Logout last from the least used window, and all the recent stuff is lost. This happens to me over and over again. Totally inconvenient.

So the question is:
Do we have a tool of a kind, or any simple method to selectively delete some commands from the history? Offset like the -d option does not cut it.

g_ramesh 12-28-2010 12:05 AM

Quote:

Originally Posted by herthh (Post 4198004)
Pardon me, but the question is very legitimate and you put it on a bad spin ("odd request", "trying to hide").

So the question is:
Do we have a tool of a kind, or any simple method to selectively delete some commands from the history? Offset like the -d option does not cut it.

Ohhhh thank you herthh for the kind words!

Kenhelm 12-29-2010 01:03 AM

This script displays bash history lines and asks for a list of line numbers for deletion. It then displays the edited history and asks for more line numbers. It exits this loop if nothing is entered.
The line numbers must be from the current display because when commands are deleted the commands after them move down to new line numbers.
The lines are deleted in memory not from the history file. The current history in memory can be written to the history file with 'history -w'
The script doesn't use any external commands, only bash. It has to be sourced into the terminal i.e. preceded by a dot and a space:
. scriptname
Code:

# This bash script must be used with 'source' or '.'
# and so it does not need to be made executable.
# . scriptname          Display last 35 history lines (default)
# . scriptname n        Display last n history lines
# The lines are displayed with their line numbers.
# Enter a list of the lines to be deleted
# An example of a line list is 90 45-56 78-
# 90    Delete line 90
# 45-56  Delete lines 45 to 56 (can also use 56-45)
# 78-    Delete line 78 to the last line
# Number ranges can be in any order in the list and can overlap.
# The lines are deleted in memory, not from the history file.

displaylines=35  # default
#displaylines=    # if unset all of the history will be displayed
if [ "$1" ];then displaylines=$1;fi # or use $1 instead of default

lastline=$(echo $(history 1))  # Last line, with leading spaces removed
lastlinenum=${lastline%%[* ]*}  # Extract line number from the last line
# Note: '*' after a line number in bash history indicates a modified line

# history -d$lastlinenum # Delete the command which ran this script (optional)
history $displaylines  # Display history
while true;do
  if [ "${displaylines//[0-9]/}" ];then break;fi # If it contains non-integer characters.
  read -ep "Lines to delete (e.g. 67-89 45 103- ) or enter nothing to quit: " numbers
  if ! [ "$numbers" ];then break;fi    # exit if nothing entered
  numbers="$numbers "                  # Append a space to the line number list
  numbers=${numbers//- /-$lastlinenum } # e.g change '23- ' to '23-lastlinenumber '
  numbers=$(echo $numbers)              # Remove unnecessary spaces

  # Check the entered number list is valid:
  errors=$(echo "${numbers// /
}"|while read line;do
  # [ "not just digits and -" OR "more than one -" OR "starts with -" ] && echo 'error'
    [  "${line//[0-9-]/}" -o  "${line%%*-*-*}" = '' -o "${line:0:1}" = '-' ] && echo 'error'
  done)
  if [ "$errors"  ];then echo "Bad line number list, try again:";continue;fi

  # Expand line ranges n-m to a list of individual line numbers
  numbers=$(echo "${numbers// /
}" | while read line;do
      if [ "${line#*-}" != "$line" ];then # Found a line containing '-'  (a range 'n-m')
        n=${line%-*}
        m=${line#*-}
        if [ $n -lt $m ];then line="$m-$n";fi # Descending order for faster reverse sorting
        line="{${line/-/..}}"  # Change 'n-m' to '{n..m}'
      fi
      eval echo $line  # '{n..m}' will be expanded to a list of numbers (brace expansion)
    done)
  a=( $numbers ) # Put line numbers into an array for reverse unique sorting
  sorted="no"
  while [ "$sorted" = "no" ];do
    unset sorted
    for ((i=0;i<${#a[@]}-1;i++));do
      if (( ${a[i]} < ${a[i+1]} ));then  # Sort by comparing/swapping adjacent elements
        tmp=${a[i]}
        a[i]=${a[i+1]}
        a[i+1]=$tmp
        sorted="no"
      fi
    done
  done
  for ((i=0;i<${#a[@]}-1;i++));do # Remove duplicate line numbers from the sorted array
    if (( ${a[i]} == ${a[i+1]} ));then
      a[i]=
    fi
  done
  a=( ${a[@]} ) # Remove empty array elements created by removing duplicates
  i=${#a[@]}    # Number of array elements
  # Check that the reverse sorted list is in the range  $lastlinenum to 1
  if [ ${a[0]} -gt $lastlinenum -o ${a[i-1]} -lt 1 ];then
    echo "Line number list is out of range, try again:"
    continue
  fi
  echo "Deleting these lines from the above history: ${a[@]}"
  echo "***********************************************************************"
  for ((i=0;i<${#a[@]};i++));do
    a[i]="history -d${a[i]};"  # Create the delete commands e.g. history -d67; ....
  done
  eval ${a[@]}                    # Execute all the 'history -d' commands
  lastline=$(echo $(history 1))    # New last line, with leading spaces removed
  lastlinenum=${lastline%%[* ]*}  # Extract the line number from the new last line
  history $displaylines            # Refresh the displayed history
done
unset a errors i lastline lastlinenum line numbers sorted tmp # Finished; tidy up


lwv962 06-02-2015 09:06 AM

ow to edit history to remove few commands executed
 
This has puzzled me for ages,I came across this solution today:
on the command line run: for i in {344..334}; do history -d $i; done
Note the selections of lines are in reverse order.

This is where I found it http://lists.gnu.org/archive/html/bu.../msg00056.html

Hope this helps !

DJ Shaji 06-02-2015 05:09 PM

note that ~/.bash_history is just a text file. open it up and delete any entry you want.

lwv962 06-03-2015 02:42 AM

Hello,
Yes DJ Shaji, I know this and have been editing ~/.bash_history with vim
for years.This code I gave yesterday is far faster and more efficient, if
for example you just want remove say more than a couple of entries; by the
time you have opened vim or another text editor,deleted what you wanted to,
done :wq in vim to close and write to disk, you could have run this code
without opening any editor, job done!
Q.E.D.


All times are GMT -5. The time now is 07:07 AM.