2011-11-17 8 views
5

Vorrei poter esaminare la cronologia dei miei comandi (fino all'inizio dell'utente)..bash_history: registra sempre ogni comando che io abbia mai emesso?

C'è qualche garanzia che continui ad essere aggiunto a .bash_history?

Se esiste un limite in cui il file inizierà a essere troncato (si spera dall'inizio) c'è un modo per rimuovere tale limite?

+5

Questa domanda è più adatto per [unix.stackexchange.com] (http://unix.stackexchange.com/) –

risposta

11

C'è un numero di variabili di ambiente che controllano come funziona la cronologia in bash. estratto rilevante da bash manpage segue:

HISTCONTROL 
      A colon-separated list of values controlling how commands are saved on the history list. If the list of values includes ignorespace, lines which 
      begin with a space character are not saved in the history list. A value of ignoredups causes lines matching the previous history entry to not be 
      saved. A value of ignoreboth is shorthand for ignorespace and ignoredups. A value of erasedups causes all previous lines matching the current 
      line to be removed from the history list before that line is saved. Any value not in the above list is ignored. If HISTCONTROL is unset, or does 
      not include a valid value, all lines read by the shell parser are saved on the history list, subject to the value of HISTIGNORE. The second and 
      subsequent lines of a multi-line compound command are not tested, and are added to the history regardless of the value of HISTCONTROL. 
    HISTFILE 
      The name of the file in which command history is saved (see HISTORY below). The default value is ~/.bash_history. If unset, the command history 
      is not saved when an interactive shell exits. 
    HISTFILESIZE 
      The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, by 
      removing the oldest entries, to contain no more than that number of lines. The default value is 500. The history file is also truncated to this 
      size after writing it when an interactive shell exits. 
    HISTIGNORE 
      A colon-separated list of patterns used to decide which command lines should be saved on the history list. Each pattern is anchored at the begin- 
      ning of the line and must match the complete line (no implicit `*' is appended). Each pattern is tested against the line after the checks speci- 
      fied by HISTCONTROL are applied. In addition to the normal shell pattern matching characters, `&' matches the previous history line. `&' may be 
      escaped using a backslash; the backslash is removed before attempting a match. The second and subsequent lines of a multi-line compound command 
      are not tested, and are added to the history regardless of the value of HISTIGNORE. 
    HISTSIZE 
      The number of commands to remember in the command history (see HISTORY below). The default value is 500. 

per rispondere alle vostre domande direttamente:

No, non c'è una garanzia, poiché la storia può essere disattivato, alcuni comandi non possono essere conservati (ad esempio, a partire da uno spazio bianco) e potrebbe esserci un limite imposto alle dimensioni della cronologia.

Per quanto riguarda la limitazione di dimensione storia: Se viene disattivata HISTSIZE e HISTFILESIZE:

unset HISTSIZE 
unset HISTFILESIZE 

si evitare che la shell troncare il file di storia. Tuttavia, se si ha un'istanza di una shell in esecuzione con queste due variabili impostate, troncherà la cronologia mentre si esce, quindi la soluzione è piuttosto fragile. Nel caso in cui si debba assolutamente conservare la cronologia della shell a lungo termine, non si deve fare affidamento sulla shell e copiare i file regolarmente (ad esempio utilizzando un cron job) in una posizione sicura.

Il troncamento cronologico rimuove sempre prima le voci meno recenti, come indicato nell'estratto di manpage sopra.

2

man bash è tuo amico: Il HISTFILESIZE variabile:

Il numero massimo di linee contenute nel file di storia. Quando a questa variabile viene assegnato un valore, il file di cronologia viene troncato, se necessario, rimuovendo le voci più vecchie , per non contenere più di quel numero di righe. Il valore predefinito è 500. Il file di cronologia viene inoltre troncato a queste dimensioni dopo averlo scritto quando viene chiusa una shell interattiva .

+0

Is c'è un modo per impostare questo così non c'è limite (in modo che riempia il mio disco rigido se dovessi inserire tanti comandi)? –

+0

Pensandoci ancora, penso che sarebbe sufficiente per i miei scopi impostare il valore a un valore esorbitante. –

0

Nota anche. Per impostazione predefinita, se si lavora con più sessioni di accesso (di solito 2 o 3 finestre di mastice registrate nello stesso server), NON visualizzano la cronologia di ciascuno di essi.

Inoltre sembra che quando esco da tutte le shell, l'ultimo a uscire sia quello che viene salvato di nuovo nel file (cioè è visibile quando inizio a lavorare il giorno successivo). Quindi presumo che bash stia mantenendo la cronologia in memoria e poi scarichi il file all'uscita, almeno questa è la mia ipotesi basata su ciò che vedo.

+0

sì, ho notato che le sessioni salvano separatamente. A volte trovo che una sessione si blocca e tutti i comandi da esso non sono stati salvati! Mi piacerebbe evitare questo. È l'unico modo per configurarlo in modo che si scarichi su ogni comando? Vorrei che il file bash_history contenga una copia sincrona (cronologica) dei miei comandi. Penso che il flushing su ogni comando inserito dovrebbe fare il trucco. –

+0

Non che io sappia di – Sodved

+0

come in, non conosci altri metodi? Ho letto qualcosa che può essere impostato in modo che si irrigidisca su ogni comando inserito. –

0

Adam ha suggerito che i limiti di dimensione disinserimento non è tagliato, ma si può evitare altre istanze di shell per cambiare HISTSIZE, HISTFILESIZE, ... impostando bandiera sola lettura su di essi in/etc/profile.

unset HISTSIZE 
unset HISTFILESIZE 
readonly HISTFILE HISTFILESIZE HISTSIZE HISTCONTROL HISTIGNORE HISTTIMEFORMAT 
1

Se si desidera che tutti i comandi di tutte le sessioni allegati alla storia immediatamente (in modo che è possibile utilizzare i comandi da una sessione immediatamente in un altro), è possibile inserire le seguenti linee nel vostro.bashrc:

unset HISTSIZE 
unset HISTFILESIZE 
HISTCONTROL=erasedups 

# append to the history file, don't overwrite it 
shopt -s histappend 
# multi-line commands should be stored as a single command 
shopt -s cmdhist 

# sharing of history between multiple terminals 
# histfile has to be read and saved after each command execution 
PROMPT_COMMAND="history -n; history -w; history -c; history -r; $PROMPT_COMMAND" 

Maggiori informazioni su questi comandi si possono trovare qui: Preserve bash history in multiple terminal windows

+0

Interessante. Dovrò provare questo a volte. Ho rinunciato a bash essere in grado di registrare la storia nel modo in cui lo voglio (che è in realtà un insieme ragionevole di comportamenti). La soluzione è installare zsh ... –

Problemi correlati