Monday, 2 September 2019

Shell history in Linux

Shell history stored in memory and on disk. 

In files:

  • ~/.bash_history
  • ~/.histfile
  • ~/.history

history Command

history                              - displays the  shell history
history 5                            - print last 5 commands
history |grep sysctl       
history -d line_number     - delete line line_number from the history
history -c                            - clear all history
history -w                            - write to history file (Usually the history file is                                                         written to upon logout)
Ctrl-r                                    - reverse history search (Ctrl-r again - next search)
Enter                                    - execute command
Arrows                                - change the command
Gtrl-g                                  - cancel the search

echo "secret command";history -d $(history 1)     - execute single command                                                                                            without being logged to                                                                                             history file

Run all command without logging:
[user1@rifle ~]$ unset HISTFILE
[user1@rifle ~]$ echo $HISTFILE

[user1@rifle ~]$

History control

!To make changes permanent you have to put the export commands below to your ~/bash_rc file. See more here...

Controls how many commands store in memory

esport HISTSIZE=1000

Ignore duplicate commands in history

export HISTCONTROL=ignoredups

Filter some commands from history

export HISTIGNORE='ls -l:pwd:date:' 

List all commands with date and time

export HISTTIMEFORMAT='%F %T  '

Using the history

Show last commands:


!N                             - repeat command line number N
!239                           - in above example execute command pwd 
!!                               - repeat previous command
!systemctl:p              - Show last command that begins from systemctl
!string  or !? string   - repeat the most recent command starting with "string"
!groupi                     - in example above run command yum groupinstall                                                  "Development Tools"
vi !239:1                  - edit file one.txt (open vi editor with first parameter of                                              history line 239)
!N^                             - first argument of the command number N. For the                                                  previous example will be            vi !239^
!$                                - last argument
ls  !239$                     - resulting command will be           ls examples/




No comments: