-
Notifications
You must be signed in to change notification settings - Fork 0
/
.bashrc_history
107 lines (98 loc) · 3.93 KB
/
.bashrc_history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#emacs: -*- mode: shell-script; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
#ex: set sts=4 ts=4 sw=4 noet:
#-------------------------- =+- Shell script -+= --------------------------
# @file .bashrc_history.sh
# @date Thu Mar 10 14:02:36 2005
# @brief
#
# Created: Thu Mar 10 14:02:36 2005
#
# Yaroslav Halchenko CS@UNM, CS@NJIT
# web: http://www.onerussian.com & PSYCH@RUTGERS
# e-mail: yoh@onerussian.com ICQ#: 60653192
#
# DESCRIPTION (NOTES):
# A script to be sourced from .bashrc to provide ways to archive all the
# actions in infinitely long history file.
#
# To use, just place
# '[ -r ~/.bashrc_history ] && source ~/.bashrc_history' in ~/.bashrc
#
# '$if Bash
# # to exit through calling exit function which will archive the history
# # next ones are optional: first is left for historical reasons
# "\C-x\C-x": "exit\n"
# "\C-x\C-w": "archive_history\n"
# $endif' in ~/.inputrc
#
# Then whenever you close bash (exit,logout or exit by pressing Ctrl-D
# or Ctrl-X twice you will have a piece of current history added to
# ~/.bash_history.archive
#
# Thanks to Philippe Teuwen for the comments and "patches"
#
# SOURCE (might be outdated):
# http://www.onerussian.com/Linux/.files/.bashrc_history
#
# LICENSE:
# Released under GNU Generic Public License. You should've received it with
# your GNU/Linux system. If not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#-----------------\____________________________________/------------------
# For easy migration under modular bash configuration (after madduck's
# zsh configuration) -- if we find older archive file and now HISTFILE
# points to $HOME/.var, we need to move things around
if [ "$HISTFILE" != "" ] && [ "$PS1" ] \
&& [ -f $HOME/.bash_history ] && [ "$BASH_VARDIR" = "$HOME/.var/bash" ]; then
if [ ! -f "$BASH_VARDIR/history" ] ; then
echo -e "I: Found previousely allocated history files.\n Moving them under $BASH_VARDIR"
for f in $HOME/.bash_history*; do
mv "$f" "${f//.bash_/.var/bash/}"
done
else
echo -e "I: New layout of history files also found.\n Please remove/propagate old history into new"
fi
fi
if [ "$PS1" ] ; then # interactive shell
export STARTTIME="`date '+%Y-%m-%d %H:%M:%S'`" \
HISTORYDUMP=0 \
HISTTIMEFORMAT='%F %T '
shopt -s cmdhist histappend
# Next function really stores the history logs. Besides that if you want to store whatever you have so far
# you can call this function and it will save increment from the last call to it.
archive_history()
{
TFILE=${HISTFILE}.$$.temp
CURTIME=`date '+%Y-%m-%d %H:%M:%S'`
CURTTY=`tty`
#STAMP
HISTORYDUMP=$(($HISTORYDUMP+1))
# Title
echo "#$USER@${HOSTNAME} [ ${STARTTIME} - ${CURTIME} ]:$HISTORYDUMP ($CURTTY) ----" >| $TFILE
# Fresh tasty history
history -a ${TFILE}
# Append it to the archive
#cat ${TFILE} >> ${HISTFILE}.archive
which gawk >/dev/null \
&& awk '/^#[0-9]+$/{print strftime("#%F %T ", gensub(/#/,"","g")) }//' ${TFILE} >> ${HISTFILE}.archive \
|| cat ${TFILE} >> ${HISTFILE}.archive
# Adjust history file itself
# of cause we need to dump to some temp file and then rename... he heh
cat ${HISTFILE} ${TFILE} | tail -${HISTSIZE} >| ${HISTFILE}.$$
mv -f ${HISTFILE}.$$ ${HISTFILE}
# clean up after yourself - to be absessive remove the other tmp file as well -
# - might happen if there were no permission to move it at above point
rm -rf ${TFILE} ${HISTFILE}.$$
}
trap 'archive_history' EXIT HUP
unset IGNOREEOF
fi
# ---------------{ some shortcuts }---------------------
gbha ()
{
grep "$@" ${HISTFILE}.archive
}
gbhaa ()
{
grep -h "$@" ${HISTFILE}.archive{,s/.*}
}