how to edit history to remove few commands executed
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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?
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
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
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
my intention is not that.
Don't they all say that.. ;-p
Quote:
Originally Posted by g_ramesh
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.
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.
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?
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?
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.
Not the right answer... Need information, not acid.
Quote:
Originally Posted by acid_kewpie
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.