Logs bash history to an sqlite database
Recent is a more structured way to log your bash history.
~/.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
HISTTIMEFORMAT variables but it
is still a unstructured format with limited querying ability.
Recent does the following.
- Logs current localtime, command text, current pid, command return value,
working directory to an sqlite database in
- Logs history immediately, rather than at the close of the session.
- Provides a command called
recentfor searching bash history.
NOTE about trengrj/recent
recent2 started off as 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 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.
You need will need sqlite installed.
Install the recent pip package.
pip install recent2
Add the following to your
export PROMPT_COMMAND='log-recent -r $? -c "$(HISTTIMEFORMAT= history 1)" -p $$'
And start a new shell.
See example usage at https://asciinema.org/a/271533
> 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] [-e key[:val]] [--hide_time] [--debug] [--detail] [--columns COLUMNS] [-re] [-sql] [--nocase] [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. -e key[:val], --env key[:val] Filter by shell env vars. Env vars set in RECENT_ENV_VARS as comma separated list will be captured. --hide_time, -ht dont display time in command output --debug Debug mode --detail Return detailed output --columns COLUMNS Comma separated columns to print if --detail is passed. Valid columns are command_dt,command,pid,retur n_val,pwd,session,json_data -re enable regex search pattern -sql enable sqlite search pattern --nocase, -nc Ignore case when searching for patterns 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.
# 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
recent git --successes_onlyor
recent git -so
recent git --failures_onlyor
recent git -fo
recent git --status_num 1or
recent git -stn 1returns only the git commands that have exit status 1.
recent git --return_self. By default
recentcommands are not displayed in the output. Pass the
return_selfto change that.
recent git -w ~/code. This returns only the commands that were executed with
~/codeas current working directory.
- Filter the commands by execution date by doing
recent git -d 2019or
recent git -d 2019-10or
recent git -d 2019-10-04
- By default recent prints command timestamp and the command in the output. Use
recent git --hide_timeor
recent git -htto 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
-sooption 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 10000or
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);
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"
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
git clone https://github.com/dotslash/recent2 && cd recent2 pip install -e .
Release history Release notifications | RSS feed
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size recent2-1.0.1-py3-none-any.whl (11.7 kB)||File type Wheel||Python version py3||Upload date||Hashes View|