Skip to main content

Logs bash history to an sqlite database

Project description

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 is a clone 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 trengrj has not been very responsive.

Most of the code is written by trengrj. I only added a few incremental patches; but I intend to actively maintain this as I see more interesting use cases.

Installation instructions

You need will need sqlite installed.

Install the recent pip package.

pip 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

> recent -h
usage: recent [-h] [-n 20] [--status_num 0] [--successes_only]
              [--failures_only] [-w /folder] [-d 2016-10-01] [--return_self]
              [--char_limit 200] [--hide_time] [-re] [-sql]
              [pattern]

recent is a convenient way to query bash history. Visit
https://github.com/dotslash/recent2 for more examples or to ask
questions or to report issues

positional arguments:
  pattern               optional pattern to search

optional arguments:
  -h, --help            show this help message and exit
  -n 20                 max results to return
  --status_num 0, -stn 0
                        int exit status of the commands to return. -1 =>
                        return all.
  --successes_only, -so
                        only return commands that exited with success
  --failures_only, -fo  only return commands that exited with failure
  -w /folder            working directory
  -d 2016-10-01         date in YYYY-MM-DD, YYYY-MM, or YYYY format
  --return_self         Return `recent` commands also in the output
  --char_limit 200, -cl 200
                        Ignore commands longer than this.
  --hide_time, -ht      dont display time in command output
  -re                   enable regex search pattern
  -sql                  enable sqlite search pattern

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

# Look for all git commands
recent git
# Look for git commit commands. Query via regexp mode.
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.

CREATE TABLE commands(
  command_dt timestamp,
  command text,
  pid int,
  return_val int,
  pwd text,
  session text);
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"

Dev installation instructions

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

recent2-0.2.4.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

recent2-0.2.4-py2.py3-none-any.whl (9.1 kB view details)

Uploaded Python 2Python 3

File details

Details for the file recent2-0.2.4.tar.gz.

File metadata

  • Download URL: recent2-0.2.4.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5

File hashes

Hashes for recent2-0.2.4.tar.gz
Algorithm Hash digest
SHA256 c199c3396db44b2b8c2ea9f0a1aa130c547bc0dd1f7160bc729e2591a79a0b24
MD5 34a0124fd0c3814ceaed03038f77ea15
BLAKE2b-256 c30773a6dd5008f3fcff8e6c46a48cf1711d3053d379699f48bb3fcfaa0a9fe4

See more details on using hashes here.

File details

Details for the file recent2-0.2.4-py2.py3-none-any.whl.

File metadata

  • Download URL: recent2-0.2.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.6.0.post20191030 requests-toolbelt/0.9.1 tqdm/4.38.0 CPython/3.7.5

File hashes

Hashes for recent2-0.2.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 25b51e81c92e037d0d5a5de8f75c9627b0ecf8e932e7a022d63b5fab66d168b5
MD5 712dff0a6cb6bb6e6c6309d04e37b0ed
BLAKE2b-256 641b1661473609a7ac361e5dc3c892767033633a400a48d35f9e97b7186efdc3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page