• Posts
  • RSS
  • ◂◂RSS
  • Contact

  • You Should Be Logging Shell History

    February 13th, 2012
    logging, shell, tech  [html]
    Do you work on the command line? If so, you should be logging your shell history. Don't shells already do this? Not enough. They log only the most recent commands [1] and they don't include when or where you ran them. Not only that, but they don't save until the shell exits, which means you lose the data in a crash. It's a mess, but you can fix it: tell the shell to log this information immediately before it shows the next command prompt. [2] For bash, put this in the init file [3]:
      promptFunc() {
        # right before prompting for the next command, save the previous
        # command in a file.
        echo "$(date +%Y-%m-%d--%H-%M-%S) $(hostname) $PWD $(history 1)" \
          >> ~/.full_history
      }
      PROMPT_COMMAND=promptFunc
    
    You'd think running these extra commands would slow your system down, and I'm sure they do relative to doing nothing or having this built into the shell, but the prompt still feels like it appears instantly.

    Your shell history is an invaluable resource in understanding what you were doing earlier, and keeping a permanent log is well worth it.

    For quick history searching, add:

    function histgrep {
      local n_lines=10
      if [[ "$1" =~ ^[0-9]*$ ]]; then
        n_lines="$1"
        shift
      fi
      grep "$@" ~/.full_history | tail -n "$n_lines"
    }
    
    Usage:
    $ histgrep foo
    ... ten lines of history ...
    $ histgrep 100 foo
    ... a hundred lines of history ...
    

    (Yes, I've written about this before, but I'm continually surprised how many people don't have something like this. It saves me gobs of time.)


    [1] Bash defaults to 500 lines. At my previous job I had ~100K lines logged while at my current one I have 73K.

    [2] This does mean that if you have a crash while a command is running that command will not be written back to the history file. This is bad, but I don't think there's a 'post command entry' hook in bash.

    [3] On my Mac this goes in ~/.bash_profile; on Linux I'd put it in ~/.bashrc. For other shells you'll need something similar.

    Comment via: google plus, facebook, r/commandline

    Recent posts on blogs I like:

    Collections: Clothing, How Did They Make It? Part I: High Fiber

    This week we are starting the first of a four (?) part look at pre-modern textile production. As with our series on farming and iron, we are going to follow the sequence of production from the growing of fibers all the way to the finished object, with a f…

    via A Collection of Unmitigated Pedantry March 5, 2021

    Austerity is Inefficient

    Working on an emergency timetable for regional rail has made it clear how an environment of austerity requires tradeoffs that reduce efficiency. I already talked about how the Swiss electronics before concrete slogan is not about not spending money but ab…

    via Pedestrian Observations February 27, 2021

    The Troubling Ethics of Writing (A Speech from Ancient Sumer)

    (Translated from a transcript of an ancient Sumerian speech by Uruk's most well-respected Scriptological Ethicist) Writing is a profoundly dangerous technology: Access to writing was initially, and still remains, uneven. What's worse, the rich are m…

    via BLOG - Cullen O'Keefe February 15, 2021

    more     (via openring)


  • Posts
  • RSS
  • ◂◂RSS
  • Contact