LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 06-30-2010, 02:36 AM   #1
g_ramesh
LQ Newbie
 
Registered: Jun 2010
Posts: 29

Rep: Reputation: 0
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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 06-30-2010, 02:46 AM   #2
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
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.
 
Old 06-30-2010, 05:44 AM   #3
g_ramesh
LQ Newbie
 
Registered: Jun 2010
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by acid_kewpie View Post
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?
 
0 members found this post helpful.
Old 06-30-2010, 06:14 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by g_ramesh View Post
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 View Post
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 View Post
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 View Post
my intention is not that.
Don't they all say that.. ;-p


Quote:
Originally Posted by g_ramesh View Post
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.
 
2 members found this post helpful.
Old 06-30-2010, 06:18 AM   #5
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
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.
 
Old 07-07-2010, 10:49 AM   #6
blueskiesokie
LQ Newbie
 
Registered: Jul 2010
Posts: 1

Rep: Reputation: 0
Question

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.
 
Old 07-07-2010, 12:50 PM   #7
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
Quote:
Originally Posted by blueskiesokie View Post
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??
 
Old 07-07-2010, 01:37 PM   #8
Bratmon
Member
 
Registered: Jul 2009
Location: 75.126.162.205:80
Distribution: Arch / Mint 17
Posts: 297
Blog Entries: 3

Rep: Reputation: 50
Quote:
Originally Posted by blueskiesokie View Post
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.
 
Old 07-07-2010, 01:47 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by blueskiesokie View Post
... or change favorite password?

hmmmm.
Favourite password sounds dangerous :}
 
Old 12-20-2010, 12:29 PM   #10
herthh
LQ Newbie
 
Registered: Dec 2010
Posts: 2

Rep: Reputation: 0
Not the right answer... Need information, not acid.

Quote:
Originally Posted by acid_kewpie View Post
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.
 
Old 12-28-2010, 12:05 AM   #11
g_ramesh
LQ Newbie
 
Registered: Jun 2010
Posts: 29

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by herthh View Post
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!
 
0 members found this post helpful.
Old 12-29-2010, 01:03 AM   #12
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
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
 
Old 06-02-2015, 09:06 AM   #13
lwv962
Member
 
Registered: Feb 2011
Location: Wales
Distribution: MX Linux 18
Posts: 36

Rep: Reputation: 0
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 !

Last edited by lwv962; 06-02-2015 at 09:08 AM. Reason: forgot to end.
 
Old 06-02-2015, 05:09 PM   #14
DJ Shaji
Member
 
Registered: Dec 2004
Location: Yo Momma's house
Distribution: Fedora Rawhide, ArchLinux
Posts: 518
Blog Entries: 15

Rep: Reputation: 106Reputation: 106
note that ~/.bash_history is just a text file. open it up and delete any entry you want.
 
Old 06-03-2015, 02:42 AM   #15
lwv962
Member
 
Registered: Feb 2011
Location: Wales
Distribution: MX Linux 18
Posts: 36

Rep: Reputation: 0
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Commands to remove particular few lines in the history file cartti Linux - Newbie 1 06-16-2010 12:57 PM
History of Commands Executed by Users ajayan Linux - Newbie 2 04-16-2010 11:38 AM
ssh record executed commands??? joangopan Linux - Newbie 4 05-13-2009 10:05 AM
How to Display Commands to be Executed with At lrt Linux - Software 1 04-11-2008 11:26 AM
removing only most previously executed command from history? jagroop mand Linux - Newbie 2 01-19-2005 05:39 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration