Live data from Hacker News

How and Why to Log Your Bash History

spin.atomicobject.com

61–70 of 139 posts

Re: How and Why to Log Your Bash History

#61
post #55

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/$…

Servers should have zero history saved on the disk. It gives any intruder an easy place to look for passwords, private keys, etc that may have been accidentally recorded and gives clues about related systems. If you have administrative stuff you need to do more than once, write a little script or alias for it. Depending on history for this is just lazy.

Excellent point if anyone can get access to your home directory files.

I work around the security issues by not backing up history and having encrypted file systems on all of my Linux laptops. I don't save history on my servers.

Re: How and Why to Log Your Bash History

#62
post #32

I'm a big fan of saving history - trying to remember all the shell based commands we use is a nightmare! If you're using zsh, a tip I picked up from [0] was to alias all your common commands like 'cd', 'ls' 'fg'... to: ' cd' ' ls' ' fg' ... Then add the following line to your zshrc to ignore lines prepended with a space: setopt HIST_IGNORE_SPACE This keeps your history cleaner from any ls, cd, fg inputs you use. [0]…

Dropping the ls, ok. But cd conveys relevant context. It would be really clever if a series of cd/ls rolled into a final absolute path cd so history shows context for the subsequent batch of commands.

If you find yourself doing a series of `cd` and `ls` commands frequently, I feel like you might be using `cd` and tab completion ineffectively.

Instead of doing:

    ls
    cd foo/
    ls
    cd bar/
    ls
    cd baz/
You can do:

    ls
    cd foo/bar/baz/
This isn't what it will look like, it's just the key sequence (if this doesn't make sense let me know and I'll try to explain better). will list all the possible completions, which is equivalent to an ls, without having to type in an ls and then another cd.

You can frequently avoid even more `cd`/`ls` shenanigans by judiciously using `pushd`.

Re: How and Why to Log Your Bash History

#64

Possibly a good opportunity to ask: On Mac OS X and iTerm2 I've noticed a thing where pasting in commands means they don't get added to the history and using up arrow or search to find them doesn't work. Anyone seen that? It's one of those things that annoys me every time it happens, but I'm always in the middle of a task when it occurs so I never get a chance to figure it out.

Could it be that you're accidentally adding/copying a space at the beginning of the command? I know Linux BASH will ignore the command from history if that's the case, and I think that OSX is similar.

This is probably it, thanks, I didn't know this could occur. It only happens when I copy the output of a particular command, not always, so I'm guessing it has a space.

Re: How and Why to Log Your Bash History

#65
post #24

This looks like quite a dirty hack, but I'll try it. I wish bash history wasn't so broken. I suppose once upon a time it was ok to assume people only had one shell session at a time.

I'm curious as to why you consider it so broken. As far as I can tell, bash deals with multiple shell sessions exactly how I'd expect (and want) it to: an individual terminal doesn't have access to the live session from other terminals, but when you close it it appends it's session to the shared history file, so each session ends up a continuous block in that file.

I don't know what it does exactly, but it always seems to lose history after a reboot. Either the last shell session to close overwrites the others, or they fail to save the history if not closed cleanly enough.

Re: How and Why to Log Your Bash History

#66
I've written a bash history server in go and it has saved me more time than it took me to write it.

It stores commands in a sqlite database. For each command it also saves the time, the user and the host. It permits global search with some useful features (grep A/B/C switches, regexp, etc).

I wrote it to scratch an itch, thus the documentation isn't user friendly. If anyone is interested, shameless plug: https://github.com/andmarios/bashistdb

Re: How and Why to Log Your Bash History

#67
post #32

I'm a big fan of saving history - trying to remember all the shell based commands we use is a nightmare! If you're using zsh, a tip I picked up from [0] was to alias all your common commands like 'cd', 'ls' 'fg'... to: ' cd' ' ls' ' fg' ... Then add the following line to your zshrc to ignore lines prepended with a space: setopt HIST_IGNORE_SPACE This keeps your history cleaner from any ls, cd, fg inputs you use. [0]…

There's a direct parameter to do that in one go.

HISTIGNORE="history:ls:[bf]g:exit:htop:ncdu:pwd:reset:clear:mount:umount:&:^[ \t]*"

Mine might be flawed though.

Re: How and Why to Log Your Bash History

#69

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

[deleted]

Re: How and Why to Log Your Bash History

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

Could you share a gist of that? Would love similar thing in my toolset!

I don't do it with code, it's a feature of this product: https://www.vandyke.com/products/securecrt/
Post reply on HN