Live data from Hacker News

How and Why to Log Your Bash History

spin.atomicobject.com

101–110 of 139 posts

Re: How and Why to Log Your Bash History

#101
post #96
post #86

I do the opposite and maybe weird - I disable history, and keep the history file contents hand curated. Whenever I come up/across/use a command I'd like to keep for future or use frequently, it gets appended to the history file.

Do you use the fc command (shell builtin in the case of bash) to access the history? Or just up-arrow to scroll through the options?

Ctrl+R (reverse fuzzy lookup) -- In my early days, I quickly found it very annoying to have to scroll through garbage to get to the command I want.

Re: How and Why to Log Your Bash History

#102

I used to do the same thing to figure out which commands I should create short aliases for. Sounded like a good idea at the time but then I realized that I'm creating a file with an awful lot of interesting information in it and I not getting a lot in return. So I set my HISTSIZE to 1000 which is more than enough for interactive shell use and I don't have to worry about having stuff like "youtube-dl fuckmesilly.com/$…

you can also update your HISTIGNORE to do things like not log 'youtube-dl' commands, or not log 'mysql -u root -p', etc.

Re: How and Why to Log Your Bash History

#103

A warning: do NOT do what one engineers I used to work with did, and upload his "dot files" onto GitHub for portability. A lot of secrets can be stored in a few months' worth of bash history.

Make sure you upload your AWS_SECRET_ACCESS_KEY and AWS_ACCESS_KEY_ID.

Re: How and Why to Log Your Bash History

#104
post #97
post #86

I do the opposite and maybe weird - I disable history, and keep the history file contents hand curated. Whenever I come up/across/use a command I'd like to keep for future or use frequently, it gets appended to the history file.

Do you have an alias or something else that facilitates that, or do you just do it manually?

Never bothered to fully automate it, tbh.

I use `unset HISTFILE` to make sure when I exit the history file doesn't get all the cruft in. Every now and then I might do some housecleaning, including going through the history file then `cp ~/.bash_history{,.2}` to keep a clean copy around.

As far as saving, heres a handy function: `getlast() { fc -ln "$1" "$1" | sed '1s/^[[:space:]]*//' }`

Re: How and Why to Log Your Bash History

#105
post #20

I do something very similar, but without the prompt settings. I have settings in .bashrc[0] to have the history file based on date. I then use fzf[1] (fzf-tmux is great) and a grep-like tool(sift[2]) to use for ctrl-r that fuzzy-searches history and orders by usage frequency[3]. This way I can easily search for the command I'm thinking of fairly quickly. Particularly useful for those times I want to run a command aga…

Thanks for the awesome tips and simple recipe to tie things together.

Had to tweak mine slightly though:

  ${HOSTNAME_SHORT} -> ${HOSTNAME}

  $HOME/Sync/Dotfiles/history -> $HOME/Sync/Dotfiles/history/*
(maybe the missing star was due to HN formatting?)

Re: How and Why to Log Your Bash History

#106
The logger in me appended hostname and the tty to the file name and the log message. You could also add a user name to this as well[0].

I love this idea because you can easily analyse your commands then modify or automate your workflow where it makes sense. I've done this in the past to create a two letter aliases to frequently used bash commands.

[0] export PROMPT_COMMAND='if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(hostname) $(tty) $(pwd) $(history 1)" >> ~/.logs/bash-history-$(date "+%Y-%m-%d").log.`hostname`; fi'

Re: How and Why to Log Your Bash History

#107

Just for those out there using zsh or fish I used to following in my .zshrc to get this working: precmd() { eval 'if [ "$(id -u)" -ne 0 ]; then echo "$(date "+%Y-%m-%d.%H:%M:%S") $(pwd) $(history | tail -n 1)" >>! ~/Dropbox/Logs/Bash/Macbook/bash-history-$(date "+%Y-%m-%d").log; fi' } I Store my logs in dropbox but you can put them wherever

Thanks so much!

Re: How and Why to Log Your Bash History

#108
post #88

At work unfortunately the sys admins disable customisation. Especially HIST_SIZE etc :( At regular intervals, especially when I have important commands to save I do a: cp ~/.bash_history ~/bash_history_bak_2016-05-31.txt That way I can do a: cat ~/bash_history* | grep to find it

Can't you work around the restriction using something like ProxyCommand /bin/bash --login --rcfile .mybashrc on ~/.ssh/config?

Re: How and Why to Log Your Bash History

#109

I used to do the same thing to figure out which commands I should create short aliases for. Sounded like a good idea at the time but then I realized that I'm creating a file with an awful lot of interesting information in it and I not getting a lot in return. So I set my HISTSIZE to 1000 which is more than enough for interactive shell use and I don't have to worry about having stuff like "youtube-dl fuckmesilly.com/$…

FYI, if you want your bash history to go dark temporarily run:

  export HISTFILE=
The rest of your session will not be logged.

Re: How and Why to Log Your Bash History

#110
post #20

I do something very similar, but without the prompt settings. I have settings in .bashrc[0] to have the history file based on date. I then use fzf[1] (fzf-tmux is great) and a grep-like tool(sift[2]) to use for ctrl-r that fuzzy-searches history and orders by usage frequency[3]. This way I can easily search for the command I'm thinking of fairly quickly. Particularly useful for those times I want to run a command aga…

For those who want a well thought out solution, I have a co-worker that created a very useful history shell script with lots of integrated tools for search. Just source in your bash profile.

https://github.com/autochthe/history

Post reply on HN