Tutorial

Tutorial

Is there a way that you can see all past activity / records for the command line, like a 'history' feature in a browser?

Bit of background I'm using OSX terminal to interact with the command line.

Antwort1

Unless your system administrator or yourself have set $HISTFILE to something else, your history is stored in

~/.bash_history

Tutorial

terminal reverse-i-search

With the Terminal window active, press Control+R and the reverse-i-search prompt will appear. Now simply start typing the first few characters of a previously used command – and the entire command will display. This is invaluable for those of us with short memories and long commands.

increase the size of terminal history

If you like the idea of Terminal reminding you of previous commands, then you may wish to increase the size of your Terminal history file – for even more history storage.

If you have a .bash_profile

  1. Open .bash_profile in your text editor of choice

  2. Add this:

    HISTFILESIZE=1000000000 HISTSIZE=1000000

  3. Save and exit

If you do not have a .bash_profile

  1. Start Terminal

  2. Navigate to the Home folder by entering:

    cd ~/

  3. Create .bash_profile by entering:

    touch .bash_profile

  4. Now, either edit .bash_profile in your favorite text editor or type this in the Terminal window to automatically open the file in the default TextEdit:

    open -e .bash_profile

  5. Lastly, add this to the .bash_profile file:

    HISTFILESIZE=1000000000 HISTSIZE=1000000

  6. Save and exit

The credits to this great tutorial belongs to mactoids.

Antwort2

In many shells, you can see your command history simply by typing history.

Antwort3

Bash has quite a few options for dealing with your history. First of all, your history is saved in a text file. This is normally ~/.bash_history but can be changed by setting the $HISTFILE variable to something else.

The easiest ways to access your history are

  1. The history command. With no options, it will simply print the contents of your $HISTFILE. However, it has quite a few options to manipulate the history list. You can see all of them with help history. The ones I find particularly useful are

    -a  append history lines from this session to the history file
    -r  read the history file and append the contents to the history list
    

    I have this line in my bash initialization file (this is ~/bash_profile on OSX and ~/.bashrc for most other *nix flavors):

    export PROMPT_COMMAND='history -a;history -r;'
    

    Diese beiden Befehle werden jedes Mal ausgeführt, wenn ich eine neue Eingabeaufforderung erhalte (jedes Mal, wenn das Terminal anzeigt $, wenn es zum ersten Mal geöffnet wird und nach jedem ausgeführten Befehl). Das Ergebnis ist, dass mein Verlauf in allen Terminalfenstern gemeinsam genutzt wird, da nach jedem ausgeführten Befehl dieser Befehl auf die Festplatte geschrieben $HISTFILEund $HISTFILEdann von der Festplatte gelesen wird.

  2. Die Schaltflächen Upund Down. Wenn Sie Upeinmal darauf klicken, wird der zuvor ausgeführte Befehl angezeigt, wenn Sie erneut darauf klicken, wird der davor ausgeführte Befehl angezeigt und so weiter. Mit der DownSchaltfläche gelangen Sie in die andere Richtung.

    Dies ist in den meisten modernen Systemen (wie etwa OSX) standardmäßig aktiviert. Falls nicht, können Sie dieses Verhalten aktivieren, indem Sie die folgenden Zeilen zu einer Datei mit dem Namen hinzufügen ~/.inputrc:

     "\e[A": history-search-backward
     "\e[B": history-search-forward
    

Bash bietet außerdem zahlreiche Optionen und Variablen, die das Verlaufsverhalten steuern (viele davon werden in der sehr guten Antwort von @Chris erwähnt, ich werde einige erwähnen, die er nicht erwähnt):

  • histappend

    If  set,  the history list is appended to the file named by the 
    value of the HISTFILE variable when the shell exits, rather than
    overwriting the file.
    

    Auch dies sollte standardmäßig eingestellt sein. Wenn Sie feststellen, dass der Verlauf nicht über Terminalsitzungen hinweg erhalten bleibt, fügen Sie diese Zeile zu Ihrer hinzu ~/.bash_profile:

    shopt -s histappend
    
  • Die HISTCONTROLVariable:

    Eine durch Doppelpunkte getrennte Liste von Werten, die steuern, wie Befehle in der Verlaufsliste gespeichert werden. Wenn die Werteliste ignorespace enthält, werden Zeilen, die mit einem Leerzeichen beginnen, nicht in der Verlaufsliste gespeichert. Der Wert ignoreups bewirkt, dass Zeilen, die mit dem vorherigen Verlaufseintrag übereinstimmen, nicht gespeichert werden. Der Wert ignoreboth ist eine Abkürzung für ignorespace und ignoreups. Der Wert erasedups bewirkt, dass alle vorherigen Zeilen, die mit der aktuellen Zeile übereinstimmen, aus der Verlaufsliste entfernt werden, bevor diese Zeile gespeichert wird. Alle Werte, die nicht in der obigen Liste enthalten sind, werden ignoriert. Wenn HISTCONTROL nicht gesetzt ist oder keinen gültigen Wert enthält, werden alle vom Shell-Parser gelesenen Zeilen in der Verlaufsliste gespeichert, abhängig vom Wert von HISTIGNORE. Die zweite und alle nachfolgenden Zeilen eines mehrzeiligen zusammengesetzten Befehls werden nicht getestet und unabhängig vom Wert von HISTCONTROL zum Verlauf hinzugefügt.

  • Die HISTIGNOREVariable:

    Eine durch Doppelpunkte getrennte Liste von Mustern, die verwendet wird, um zu entscheiden, welche Befehlszeilen in der Verlaufsliste gespeichert werden sollen. Jedes Muster ist am Anfang der Zeile verankert und muss mit der vollständigen Zeile übereinstimmen (kein implizites „ *' is appended). Each pattern is tested against the line after the checks specified by HISTCONTROL are applied. In addition to the normal shell pattern match‐ ing characters,&“ entspricht der vorherigen Verlaufszeile). „&“ kann mit einem Backslash maskiert werden; der Backslash wird entfernt, bevor ein Abgleich versucht wird. Die zweite und die folgenden Zeilen eines mehrzeiligen zusammengesetzten Befehls werden nicht getestet und unabhängig vom Wert von HISTIGNORE zum Verlauf hinzugefügt.

Wenn ich das alles zusammenfasse, habe ich die folgenden Zeilen in meiner Shell-Initialisierungsdatei ( ~/.bash_profilefür OSX ~/.bashrcund die meisten anderen):

export HISTCONTROL=ignoredups
export HISTSIZE=10000
export HISTIGNORE="pwd:df:du:ls"

## Make Bash append rather than overwrite the history on disk:
shopt -s histappend

## history -a causes the last command to be written to the
## history file automatically and history -r imports the history
export PROMPT_COMMAND='history -a;history -r;'

verwandte Informationen