~/.bashrc 에 등록
_loghistory() {# Detailed history log of shell activities, including time stamps, working directory etc.## Based on 'hcmnt' by Dennis Williamson - 2009-06-05 - updated 2009-06-19# (http://stackoverflow.com/questions/945288/saving-current-directory-to-bash-history)## Add this function to your '~/.bashrc':## Set the bash variable PROMPT_COMMAND to the name of this function and include# these options:## e - add the output of an extra command contained in the histentrycmdextra variable# h - add the hostname# y - add the terminal device (tty)# n - don't add the directory# t - add the from and to directories for cd commands# l - path to the log file (default = $HOME/.bash_log)# ext or a variable## See bottom of this function for examples.## make sure this is not changed elsewhere in '.bashrc';# if it is, you have to update the reg-ex's belowexport HISTTIMEFORMAT="[%F %T] ~~~ "local script=$FUNCNAMElocal histentrycmd=local cwd=local extra=local text=local logfile="$HOME/.bash_log"local hostname=local histentry=local histleader=local datetimestamp=local histlinenum=local options=":hyntel:"local option=OPTIND=1local usage="Usage: $script [-h] [-y] [-n|-t] [-e] [text] [-l logfile]"local ExtraOpt=local NoneOpt=local ToOpt=local tty=local ip=# *** process options to set flags ***while getopts $options optiondocase $option inh ) hostname=$HOSTNAME;;y ) tty=$(tty);;n ) if [[ $ToOpt ]]thenecho "$script: can't include both -n and -t."echo $usagereturn 1elseNoneOpt=1 # don't include pathfi;;t ) if [[ $NoneOpt ]]thenecho "$script: can't include both -n and -t."echo $usagereturn 1elseToOpt=1 # cd shows "from -> to"fi;;e ) ExtraOpt=1;; # include histentrycmdextral ) logfile=$OPTARG;;: ) echo "$script: missing filename: -$OPTARG."echo $usagereturn 1;;* ) echo "$script: invalid option: -$OPTARG."echo $usagereturn 1;;esacdonetext=($@) # arguments after the options are saved to add to the commenttext="${text[*]:$OPTIND - 1:${#text[*]}}"# add the previous command(s) to the history file immediately# so that the history file is in sync across multiple shell sessionshistory -a# grab the most recent command from the command historyhistentry=$(history 1)# parse it outhistleader=`expr "$histentry" : ' *\([0-9]* \[[0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*\]\)'`histlinenum=`expr "$histleader" : ' *\([0-9]* \)'`datetimestamp=`expr "$histleader" : '.*\(\[[0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*\]\)'`histentrycmd=${histentry#*~~~ }# protect against relogging previous command# if all that was actually entered by the user# was a (no-op) blank lineif [[ -z $__PREV_HISTLINE || -z $__PREV_HISTCMD ]]then# new shell; initialize variables for next commandexport __PREV_HISTLINE=$histlinenumexport __PREV_HISTCMD=$histentrycmdreturnelif [[ $histlinenum == $__PREV_HISTLINE && $histentrycmd == $__PREV_HISTCMD ]]then# no new command was actually enteredreturnelse# new command entered; store for next comparisonexport __PREV_HISTLINE=$histlinenumexport __PREV_HISTCMD=$histentrycmdfiif [[ -z $NoneOpt ]] # are we adding the directory?thenif [[ ${histentrycmd%% *} == "cd" || ${histentrycmd%% *} == "jd" ]] # if it's a cd command, we want the old directorythen # so the comment matches other commands "where *were* you when this was done?"if [[ -z $OLDPWD ]]thenOLDPWD="$HOME"fiif [[ $ToOpt ]]thencwd="$OLDPWD -> $PWD" # show "from -> to" for cdelsecwd=$OLDPWD # just show "from"fielsecwd=$PWD # it's not a cd, so just show where we arefifiif [[ $ExtraOpt && $histentrycmdextra ]] # do we want a little something extra?thenextra=$(eval "$histentrycmdextra")fi# strip off the old ### comment if there was one so they don't accumulate# then build the string (if text or extra aren't empty, add them with some decoration)histentrycmd="${datetimestamp} ${text:+[$text] }${tty:+[$tty] }${ip:+[$ip] }${extra:+[$extra] }~~~ ${hostname:+$hostname:}$cwd ~~~ ${histentrycmd# * ~~~ }"# save the entry in a logfileecho "$histentrycmd" >> $logfile || echo "$script: file error." ; return 1} # END FUNCTION _loghistory
.bashrc 가 있는 폴더에서 작업
Activating the Logger
export PROMPT_COMMAND='_loghistory'
Add Some Useful Aliases
# dump regular history log
alias h='history'
# dump enhanced history log
alias hh="cat $HOME/.bash_log"
# dump history of directories visited
alias histdirs="cat $HOME/.bash_log | awk -F ' ~~~ ' '{print $2}' | uniq"
추가로
histleader=`expr "$histentry" : ' *\([0-9]* \[[0-9]*-[0-9]*-[0-9]* [0-9]*:[0-9]*:[0-9]*\]\)'`
histentrycmd=${histentry# *[0-9]* \[[0-9\-\: ]*\] }