项目作者: dotslash

项目描述 :
Swiss army knife to query bash history
高级语言: Python
项目地址: git://github.com/dotslash/recent2.git
创建时间: 2019-09-29T21:55:57Z
项目社区:https://github.com/dotslash/recent2

开源协议:MIT License

下载


Build Status
codecov
PyPI version

Recent

Recent is a more structured way to log your bash history.

The standard ~/.bash_history file is inadequate in many ways, its
worst fault is to by default log only 500 history entries, with no timestamp.
You can alter your bash HISTFILESIZE and HISTTIMEFORMAT variables but it
is still a unstructured format with limited querying ability.

Recent does the following.

  1. Logs current localtime, command text, current pid, command return value,
    working directory to an sqlite database in ~/.recent.db.
  2. Logs history immediately, rather than at the close of the session.
  3. Provides a command called recent for searching bash history.

NOTE about trengrj/recent

recent2 started off as a fork of trengrj/recent.
I used trengrj‘s util for about a month and I really
liked it. However I saw some short comings in the tool. I made a clone because the original
project seems to be unmaintained.

Most of the core logic is written by trengrj. I added
a lot of incremental patches for the things that interested me. I intend to actively
maintain this as I see more interesting use cases.

Installation instructions

Install the recent2 pip package via pip (python 3)

pip3 install recent2

Add the following to your .bashrc or .bash_profile.

export PROMPT_COMMAND='log-recent -r $? -c "$(HISTTIMEFORMAT= history 1)" -p $$'

And start a new shell.

Usage

See example usage at https://asciinema.org/a/271533

Help Text

  1. > recent -h
  2. usage: recent [-h] [-n 20] [--status_num 0] [--successes_only]
  3. [--failures_only] [-w /folder] [--cur_session_only]
  4. [-d 2016-10-01] [--return_self] [--char_limit 200]
  5. [--env key[:val]] [--hide_time] [--debug] [--detail]
  6. [--columns COLUMNS] [-re] [-sql] [--nocase]
  7. [pattern]
  8. recent is a convenient way to query bash history. Visit
  9. https://github.com/dotslash/recent2 for more examples or to ask
  10. questions or to report issues
  11. positional arguments:
  12. pattern optional pattern to search
  13. optional arguments:
  14. -h, --help show this help message and exit
  15. -n 20 max results to return
  16. --status_num 0, -stn 0
  17. int exit status of the commands to return. -1 =>
  18. return all.
  19. --successes_only, -so
  20. only return commands that exited with success
  21. --failures_only, -fo only return commands that exited with failure
  22. -w /folder working directory
  23. --cur_session_only, -cs
  24. Returns commands only from current session
  25. -d 2016-10-01 date in YYYY-MM-DD, YYYY-MM, or YYYY format
  26. --return_self Return `recent` commands also in the output
  27. --char_limit 200, -cl 200
  28. Ignore commands longer than this.
  29. --env key[:val], -e key[:val]
  30. Filter by shell env vars. Env vars set in
  31. RECENT_ENV_VARS as comma separated list will be
  32. captured.
  33. --hide_time, -ht dont display time in command output
  34. --debug Debug mode
  35. --detail Return detailed output
  36. --columns COLUMNS Comma separated columns to print if --detail is
  37. passed. Valid columns are command_dt,command,pid,retur
  38. n_val,pwd,session,json_data
  39. -re enable regex search pattern
  40. -sql enable sqlite search pattern
  41. --nocase, -nc Ignore case when searching for patterns
  42. To import bash history into recent db run recent-import-bash-history

Look at your current history using recent. Here are some examples on how to use recent.

Basic examples

  1. # Look for all git commands
  2. recent git
  3. # Look for git commit commands. Query via regexp mode.
  4. recent -re git.*commit

Less basic usage

  • Filter commands by exit status
    1. recent git --successes_only or recent git -so
    2. recent git --failures_only or recent git -fo
    3. recent git --status_num 1 or recent git -stn 1 returns only the git commands that have exit status 1.
  • recent git --return_self. By default recent commands are not displayed in the output. Pass the return_self to change that.
  • recent git -w ~/code. This returns only the commands that were executed with ~/code as current working directory.
  • Filter the commands by execution date by doing recent git -d 2019 or recent git -d 2019-10 or recent git -d 2019-10-04
  • By default recent prints command timestamp and the command in the output. Use recent git --hide_time or recent git -ht to hide the command timestamp. This is useful when copy-pasting commands from output.
  • Copy paste errors into the shell can result in random junk coming up
    in the bash history. While -so option mostly takes care of this,
    another way to tackle this is to not show commands that are longer
    than a given limit. The default is 200. If you want longer commands,
    then do recent git --char_limit 10000 or recent git -cl 10000

    Usage via sqlite

It is possible directly interact with sqlite if all the above options have failed you. See the table schema below.

  1. CREATE TABLE commands(
  2. command_dt timestamp,
  3. command text,
  4. pid int,
  5. return_val int,
  6. pwd text,
  7. session text);
  8. CREATE INDEX command_dt_ind on commands (command_dt);
  • option1: recent -sql 'command like "%git%" and command not like "%commit%"'
  • option2: You can directly play around with sqlite sqlite3 ~/.recent.db "select * from commands limit 10"

FAQs

Q: Can I have a custom location to store my history sqlite file?
A: Yes. Point RECENT_DB environment variable to your sqlite file.

Q: I want to have a custom PROMPT_COMMAND that calls log-recent using my own logic. How do I do that?
A: This is basically https://github.com/dotslash/recent2/issues/32. Set RECENT_CUSTOM_PROMPT environment variable
to a non empty value.

Dev installation instructions

  1. git clone https://github.com/dotslash/recent2 && cd recent2
  2. pip install -e .