history command

 

 


查看之前行過的 cmd

history [n]    # 顯示最尾 n 項 history (Default: HISTSIZE)

 

File: ~/.history

Default filename for reading and writing saved history
(只保留了 HISTFILESIZE 行 records)

 

Clear History

history -c

 

Save Histroy

Bash saves history when you exit the terminal session(exit bash).

history -a        # append history lines from this session to the history file

history -w       # write the current history(HISTSIZE) to the history file

 


screen session 's history

 

Different shells in screen maintain their own history in memory (HISTSIZE)
    until it is flushed (written) to history file.

Bash saves history when you exit the terminal session.

每句 command 後 save & read history

~/.bashrc

PROMPT_COMMAND='history -a; history -n'

Notes

-n        read all history lines not already read from the history file
                and append them to the history list

 


Configure

 

Size

# Defaut:
HISTSIZE=1000           # maximum number of entries for the `history`
HISTFILESIZE=2000       # maximum number of entries store in the .bash_history file

Checking Settings

echo $HISTSIZE

Ignore

HISTCONTROL=ignoredups:ignorespace

History with timestamp

HISTTIMEFORMAT="FMT1 FMT2 ... "

%F       # YYYY-MM-DD

%T       # HH:MM:SS

%c:      # WWW YYYY-MM-DD HH:MM:SS TZ (Wed 11 Sep 2024 04:20:17 PM HKT)

e.g.

~/.bashrc

HISTTIMEFORMAT="%F %T    "

 


執行第 n 句 cmd ("!")

 

"!" = history expansion character

!123     # 報行第 123 包 cmd

!-n    Refer to the current command minus n

!!      Refer to the previous command. (!-1)

!STRING    # Search start with STRING in history AND RUN it

 


Prevent Recording Commands in History

 

set +o history

 


Search Hotkey

 

ctrl+r        # Search for commands in your bash history

ctrl+o       # Run a command you found using a ctrl+R search

ctrl+g       # Exit a crtl+R search

 


Get arguments from most recent command

 

!:^         # 1st argument

!:N         # Nth argument

!:$          # last argument

!:*          # all the arguments

 

 

Creative Commons license icon Creative Commons license icon