Live data from Hacker News

How and Why to Log Your Bash History

spin.atomicobject.com

111–120 of 139 posts

Re: How and Why to Log Your Bash History

#111
post #51
post #29

https://news.ycombinator.com/item?id=10695305 ^ Here is my comment on a previous post for how I am logging all of my bash history in a logical manner (keeping track of terminals and timestamps, etc.) to an sqlite database.

I've been tempted to do something similar. The context information (e.g. environment, working directory) would be especially helpful, since that's what I'm currently missing. Have you tried https://github.com/umang1912/advanced-shell-history ? That was where I planned to start. Here are a few other projects: https://github.com/thenewwazoo/bash-history-sqlite # simple shell script to store Bash history into SQLite3 da…

Most of these are extremely complex for almost no good reason. Storing you bash history "in the cloud" seems like a dumb idea to me, requiring a Python script for this seems gratuitous... and essentially all of these are using PROMPT_COMMAND (except bash-history-sqlite, which seems more legit), which doesn't have the correct behavior. I remember all sorts of issues with the timing of what is logged, how pipes are handled, and interaction with expected history behavior. I'm going to counter-ask: have you tried my script? ;P I recommend starting with my script, but obviously I'm going to be biased. Almost all of my script is the actual SQL parts: you will note that I have almost no shell shenanigans. At least from the perspective of asking me why I haven't tried something else, I feel like you would have to provide some rather extreme motivation at this point to work with one of these much more complex scripts.

Re: How and Why to Log Your Bash History

#112
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?)

Yes, the stars are missing, but not where you think. The --files option has a star on the inside of each quote. This makes the search only show history for the host you are on (I sync my Dotfiles between machines).

sift searches a path/folder so the star you have is not needed.

But yeah, HOSTNAME_SHORT is something I derive from HOSTNAME.

Re: How and Why to Log Your Bash History

#113
post #76

Earlier quoted context omitted.

In zsh, you can complete a glob.

bash too

But if there are multiple candidate completions, you don't get a list of them, the way you do from with no glob - it just completes unconditionally to the first candidate.

Re: How and Why to Log Your Bash History

#114
post #34

I go one big step further than this and log everything that comes across the screen. One time it saved me from a crontab -r that wiped out a 100+ line crontab. I had viewed it recently so I just copied it out of my history. On a day to day basis it's more about looking up old queries I typed out, the results of those queries at that time, bash commands and their results, the state of a file I edited at a certain time…

This could be done using script as well.

script gives you the whole console, including output and control characters. It's a useful tool but might be a little overkill for keeping track of what commands you have run.

Re: How and Why to Log Your Bash History

#115
post #112

Earlier quoted context omitted.

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?)

Yes, the stars are missing, but not where you think. The --files option has a star on the inside of each quote. This makes the search only show history for the host you are on (I sync my Dotfiles between machines). sift searches a path/folder so the star you have is not needed. But yeah, HOSTNAME_SHORT is something I derive from HOSTNAME.

Thanks for clarifying! Now I'm curious to hear more about how you sync your dotfiles ;-)

Re: How and Why to Log Your Bash History

#116

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.

The default bash setting is to not write history until the session terminates. In which case the whole session will be forgotten, not just the stuff after overwriting HISTFILE (I always set it to /dev/null FWIW, didn't know blank was sufficient).

Re: How and Why to Log Your Bash History

#117
post #99

Earlier quoted context omitted.

Yours is a good additional idea, but it is in no way a substitute. You might want to look at some history you forgot to log. Having intentional logging on top of comprehensive logging is like markup metadata, very handy too. it makes me sad when everybody doesn't see the same things the same way. I'm just waiting now for systemd to disable bash history in favor of logging in binary form all mouse rolls intermixed wit…

6 of one, half dozen of another. It really depends on how you see things. For security reasons we've disabled logging. And users don't access servers at the shell anyway so no need for it there. We log app usage in app. I find screen logging a bit more useful than shell logging. Shell only records the commands sent and not the result. Screen records everything including interactive like nano.

the most precious thing to a computer should be the human input, that's the thing that your brain worked on and that's what primarily should be recorded. (that's what source code is) Computers are deterministic, replaying your input should yield the same output again.

That is why it is soo important to save .bash_history (seriously, that's not a dot file you should be searching hard for)

saving other things are "nice to haves"

Re: How and Why to Log Your Bash History

#118
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…

I having trouble seeing what's going on here. Is the formatting ok? Maybe throw it in a gist?

Re: How and Why to Log Your Bash History

#119
post #112

Earlier quoted context omitted.

Yes, the stars are missing, but not where you think. The --files option has a star on the inside of each quote. This makes the search only show history for the host you are on (I sync my Dotfiles between machines). sift searches a path/folder so the star you have is not needed. But yeah, HOSTNAME_SHORT is something I derive from HOSTNAME.

Thanks for clarifying! Now I'm curious to hear more about how you sync your dotfiles ;-)

I sync my "$HOME/Sync" folder to pretty much every device I have. Android phone/tablet, Windows Laptop, FreeBSD Desktop, Linux VPS....

syncthing[0]/SyncTrazor[1]

[0] https://syncthing.net/

[1] https://github.com/canton7/SyncTrayzor#installation

Post reply on HN