On a UNIX system (using bash, though this probably applies to other shells too), the
history command not only displays your shell history for you, but it also comes with a suite of options for manipulating it. Though I’ve used UNIX systems here and there for years, I never really knew about this command, and I always figured you would just edit the
.bash_history file manually if you needed to make changes. But when I tried to edit it or delete it, I found that it didn’t work the way I thought.
It turns out this is because your command history is actually stored as a working copy in memory, and that
.bash_history is the final saved out transcript upon shell exit (or whenever you specify).
First of all, why would you ever want to edit your history, you ask? Well, say you
su over to
root (or any other shared user) and you’ve done a bunch of fumbling around to get something working. You may or may not want all that idiocy hanging around in the history. Or perhaps you made a typo, and every time you go back into your history, that glaring typo is waiting for you as click-bait. And so on…
Now it goes without saying that anything I’m about to post can be figured out from the
man pages of the
history command itself. But sometimes that can be overwhelming 😛 I had to Google around to figure out how to edit specific lines based on pattern matching.
history shows your shell history:
[delrocco@localhost ~]$ history 1 la 2 cd ../etc/ 3 la 4 cd profile.d/ 5 la 6 less ldm.sh 7 su - root 8 sudo vi custom.conf 9 less /etc/gdm/custom.conf 10 less Init 11 cd /etc/gdm 12 cd Init/ 13 less Default 14 cd ../PreSession/ 15 la 16 less custom.conf 17 la 18 cd ldm/ 19 la 20 sudo cd ldm
history does provide the option
-d for removing a specific line by number:
[delrocco@localhost ~]$ history -d 19 [delrocco@localhost ~]$ history 1 la 2 cd ../etc/ 3 la 4 cd profile.d/ 5 la 6 less ldm.sh 7 su - root 8 sudo vi custom.conf 9 less /etc/gdm/custom.conf 10 less Init 11 cd /etc/gdm 12 cd Init/ 13 less Default 14 cd ../PreSession/ 15 la 16 less custom.conf 17 la 18 cd ldm/ 19 sudo cd ldm
But I couldn’t figure out how to remove, say, all of my annoying
la commands (an alias I have).
What you can do is, call
history -w (write) to save out any changes to the .bash_history file right then and there. Then you can edit the file to your hearts content using one of the many great text editing commands on UNIX (
sed, shell script, etc.), and then read the file back into the history in working memory. Use option
-r to read in the current .bash_history file (and any file you want really) which will be appended to your current history. So if you want to replace it entirely (and not duplicate your history), use option
-c first to clear your history.
[delrocco@localhost ~]$ history -w [delrocco@localhost ~]$ sed -i '/^la/d' .bash_history [delrocco@localhost ~]$ history -c [delrocco@localhost ~]$ history -r .bash_history [delrocco@localhost ~]$ history 1 cd ../etc/ 2 cd profile.d/ 3 less ldm.sh 4 su - root 5 sudo vi custom.conf 6 less /etc/gdm/custom.conf 7 less Init 8 cd /etc/gdm 9 cd Init/ 10 less Default 11 cd ../PreSession/ 12 less custom.conf 13 cd ldm/ 14 sudo cd ldm 15 history -d 19 16 history -w 17 history
Note: Even though
.bash_history is just a final saved out copy, I was able to delete the file (and not save out current working history) before exiting my session, and when I logged back on, my history was wiped. You would think that if
.bash_history is just a dump of working memory, that it should have rewritten upon exit.