将bash历史记录记录到sqlite数据库
recent2的Python项目详细描述
最近
Recent是一种更结构化的记录bash历史的方法。在
标准的~/.bash_history
文件在许多方面都是不够的,它
最糟糕的错误是默认情况下只记录500个历史条目,没有时间戳。
您可以更改bash HISTFILESIZE
和HISTTIMEFORMAT
变量,但是
仍然是非结构化格式,查询能力有限。在
Recent执行以下操作。在
- 记录当前本地时间、命令文本、当前pid、命令返回值,
工作目录到
~/.recent.db
中的sqlite数据库。在 - 立即记录历史记录,而不是在会话结束时。在
- 提供一个名为
recent
的命令来搜索bash历史记录。在
关于trengrj/recent的说明
recent2
从trengrj/recent的克隆开始。
我用了大约一个月的trengrj的util,我真的
喜欢。不过,我看到这个工具有一些不足之处。我克隆了一个因为特伦吉
反应不是很好。在
大部分核心逻辑是由trengrj编写的。我补充道 很多我感兴趣的东西的增量补丁。我打算积极 在我看到更多有趣的用例时维护这个。在
安装说明
通过pip安装recent2pip包(python3)
pip3 install recent2
将以下内容添加到您的.bashrc
或.bash_profile
。在
export PROMPT_COMMAND='log-recent -r $? -c "$(HISTTIMEFORMAT= history 1)" -p $$'
开始一个新的外壳。在
使用
请参见https://asciinema.org/a/271533处的示例用法
帮助文本
> recent -h usage: recent [-h][-n 20][--status_num 0][--successes_only][--failures_only][-w /folder][--cur_session_only][-d 2016-10-01][--return_self][--char_limit 200][--env 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 --cur_session_only, -cs Returns commands only from current session -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. --env key[:val], -e 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
使用“最近”查看您当前的历史记录。下面是一些关于如何使用“最近”的示例。在
基本示例
^{pr2}$基本用法较少
- 按退出状态筛选命令
recent git --successes_only
或{} recent git --failures_only
或{} recent git --status_num 1
或recent git -stn 1
只返回退出状态为1的git命令。在
recent git --return_self
。默认情况下,recent
命令不显示在输出中。传递return_self
来更改它。在recent git -w ~/code
。这只返回以~/code
作为当前工作目录执行的命令。在- 通过执行
recent git -d 2019
或recent git -d 2019-10
或recent git -d 2019-10-04
按执行日期筛选命令 - 默认情况下,recent在输出中打印命令时间戳和命令。使用
recent git --hide_time
或recent git -ht
隐藏命令时间戳。当从输出复制粘贴命令时,这很有用。在 - 复制粘贴错误到shell中会导致随机垃圾出现
在巴什历史上。虽然
-so
选项主要负责这一点, 另一种解决方法是不显示较长的命令 超过一个给定的限度。默认值为200。如果你想要更长的命令, 然后执行recent git --char_limit 10000
或{}
通过sqlite使用
如果以上所有选项都失败了,可以直接与sqlite交互。请参阅下面的表架构。在
CREATETABLEcommands(command_dttimestamp,commandtext,pidint,return_valint,pwdtext,sessiontext);CREATEINDEXcommand_dt_indoncommands(command_dt);
- 选项1:
recent -sql 'command like "%git%" and command not like "%commit%"'
- 选项2:您可以直接使用sqlite
sqlite3 ~/.recent.db "select * from commands limit 10"
常见问题
Q:我可以有一个自定义位置来存储我的历史sqlite文件吗?
A:是。将RECENT-DB环境变量指向sqlite文件。在
Q:我想要一个自定义的PROMPT_命令,它使用我自己的逻辑调用logrecent。我该怎么做?
^{str1}$A:这基本上是https://github.com/dotslash/recent2/issues/32。设置RECENT_CUSTOM_PROMPT环境变量
设置为非空值。在
Dev安装说明
git clone https://github.com/dotslash/recent2 &&cd recent2 pip install -e .
- 项目
标签: