Create a gist now

Instantly share code, notes, and snippets.

directory based history bash profile
function mycd()
{
#if this directory is writable then write to directory-based history file
#otherwise write history in the usual home-based history file
tmpDir=$PWD
echo "#"`date '+%s'` >> $HISTFILE
echo $USER' has exited '$PWD' for '$@ >> $HISTFILE
builtin cd "$@" # do actual cd
if [ -w $PWD ]; then export HISTFILE="$PWD/.dir_bash_history"; touch $HISTFILE; chmod --silent 777 $HISTFILE;
else export HISTFILE="$HOME/.bash_history";
fi
echo "#"`date '+%s'` >> $HISTFILE
echo $USER' has entered '$PWD' from '$OLDPWD >> $HISTFILE
}
alias cd="mycd"
#initial shell opened
export HISTFILE="$PWD/.dir_bash_history"
#timestamp all history entries
export HISTTIMEFORMAT="%h/%d - %H:%M:%S "
export HISTCONTROL=ignoredups:erasedups
export HISTSIZE=1000000
export HISTFILESIZE=1000000
shopt -s histappend ## append, no clearouts
shopt -s histverify ## edit a recalled history line before executing
shopt -s histreedit ## reedit a history substitution line if it failed
## Save the history after each command finishes
## (and keep any existing PROMPT_COMMAND settings)
export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
@leipzig
Owner
leipzig commented Jan 21, 2012

Directory Based Bash Histories

Rationale

Using a directory-based bash history allows for a record of shell actions on a directory basis, so a group of developers have some record of what was done while in a directory, when, and by whom. This can be helpful when trying to reconstruct history with limited documentation.

This was derived from http://dieter.plaetinck.be/per_directory_bash_history

Installation

cd ~/
git clone git://gist.github.com/1651133.git .mycd
echo '. $HOME/.mycd/mycd.sh' >> ~/.bash_profile
source ~/.bash_profile

Tips

  • add .dir_bash_history to .gitignore files in git repositories
@mmcdaris
mmcdaris commented Feb 7, 2014

This is cool, thanks for writing it and documenting.

@inodb
inodb commented May 8, 2015

Looks interesting! Do you still use this or have you come up with a different way to do things?

@leipzig
Owner
leipzig commented May 23, 2015

I still use this although I recognize the use of both personal and directory-based histories.

@djKianoosh

btw, for a global git ignore:
http://stackoverflow.com/a/22885996

so... in ~/.config/git/ignore have:
.dir_bash_history

@rvelseg
rvelseg commented Jul 29, 2016

Awesome, Thanks :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment