historial infinito en =bash=

· osiux's blog

.org | .md | .gmi | .html

# historial numérico

Por defecto bash mantiene un historial numérico, lo cual presenta de manera ordenada la secuencia de comandos ejecutados:

11786  ls -lht | head
21787  du -hs
31788  rm -f /tmp/*
41789  ls
5

# historial para recordar

Mucho mejor es poder contar con un historial que además registre la fecha y hora de cada comando ejecutado:

 122922 2021-03-17 22:47 c history_aliases | cclip
 222923 2021-03-17 22:47 cd
 322924 2021-03-17 22:47 cd bash/
 422925 2021-03-17 22:47 cdc
 522926 2021-03-17 22:47 gir hist *
 622927 2021-03-17 22:47 hhy
 722928 2021-03-17 22:47 jn @blog
 822929 2021-03-17 22:47 pwd
 922930 2021-03-17 22:48 ff bash
1022931 2021-03-17 22:48 ll
1122932 2021-03-17 22:49 pomo
1222933 2021-03-17 22:49 pomodoro
13

# historial para no olvidar nada

Entonces, por un lado necesitamos registrar con un mejor formato cada comando ejecutado y por otro lado queremos que el comando history recuerde muchas líneas 1 y que además guarde inmediatamente cada comando ejecutado 2, por si nos quedamos sin batería o se cuelga el comando ejecutado :P

 1## Allow a larger history file
 2export HISTSIZE=1000000
 3export HISTFILESIZE=1000000
 4
 5## Record timestamps
 6export HISTTIMEFORMAT='%Y-%m-%d %H:%M '
 7
 8## Don’t store specific lines
 9HISTCONTROL=ignoreboth
10
11## Append history instead of rewriting it
12shopt -s histappend
13shopt -s checkwinsize
14
15## Store history immediately
16export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"
17

# alias de historial

Algunos alias de historial me permiten ver rápidamente que hice hoy o ayer para asi poder registrar mejor las tareas o resolver algún misterio.

1alias today='date +%Y-%m-%d'
2alias yesterday='echo $(date --date="yesterday" +%Y-%m-%d)'
3alias hh='history | grep $(today)  | cut -c 19- | sort -u'
4alias hhy='history | grep $(yesterday)  | cut -c 19- | sort -u'
5

# archivando el historial

Hace años que tengo en el crontab un script archive_bash_history que se ocupa de archivar mi historial de bash a un archivo por cada día, asi luego puedo auditar o divertime generando estadísticas :P

 1#!/bin/bash
 2
 3# This script comes with ABSOLUTELY NO WARRANTY, use at own risk
 4# Copyright (C) 2014 Osiris Alejandro Gomez <osiux@osiux.com>
 5#
 6# This program is free software: you can redistribute it and/or modify
 7# it under the terms of the GNU General Public License as published by
 8# the Free Software Foundation, either version 3 of the License, or
 9# any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14# General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program. If not, see <http://www.gnu.org/licenses/>.
18
19YESTERDAY=$(date --date 'yesterday' +%F)
20
21DIR="$HOME/history"
22mkdir -p "$DIR"
23
24DAY="$YESTERDAY"
25
26[[ ! -z "$1" ]] && DAY="$1"
27
28BAK="$DIR/${DAY}.log"
29TMP="$DIR/${DAY}.tmp"
30
31cat "$HOME/.bash_history" | while read -r LINE
32do
33if [[ "$LINE" =~ ^#[0-9]+ ]]
34then
35EPOC="$(echo "$LINE" | tr -d "#")"
36DATE="$(date -d @$EPOC +%Y-%m-%d)"
37TIME="$(date -d @$EPOC +%H:%M)"
38BAK="$DIR/${DATE}.log"
39TMP="$DIR/${DATE}.tmp"
40else
41[[ "$DAY" = "$DATE" ]] && echo "$DATE $TIME $LINE" >> "$BAK"
42fi
43done
44
45sort -u "$BAK" > "$TMP" && mv -f "$TMP" "$BAK"
46

# ChangeLog