🖥️history
➡️This is a command-line reference manual for commands and command combinations that you don’t use often enough to remember it. This cheatsheet explains the history command with important options and switches using examples.
12 minute read
▁ ▂ ▃ ▄ ꧁ 🔴☠ COMMANDLINE-KUNGFU WITH CHEATSHEETS ☠🔴꧂▅ ▃ ▂ ▁
# ██╗ ██╗██╗███████╗████████╗ ██████╗ ██████╗ ██╗ ██╗
# ██║ ██║██║██╔════╝╚══██╔══╝██╔═══██╗██╔══██╗╚██╗ ██╔╝
# ███████║██║███████╗ ██║ ██║ ██║██████╔╝ ╚████╔╝
# ██╔══██║██║╚════██║ ██║ ██║ ██║██╔══██╗ ╚██╔╝
# ██║ ██║██║███████║ ██║ ╚██████╔╝██║ ██║ ██║
# ╚═╝ ╚═╝╚═╝╚══════╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝
# To see most used top 10 commands:
history | awk '{CMD[$2]++;count++;}END { for (a in CMD)print CMD[a] " " CMD[a]/count*100 "% " a;}' | grep -v "./" | column -c3 -s " " -t | sort -nr | nl | head -n10
history | sed 's/^[ 0-9]* //'
# Remove numbers from history - Use the following command to give a history listing without the numbers for easier copy and pasting:
history | grep ssh
# !423
# # Search your history for commands matching ssh and then execute command #423 from the output.
history -cw
# To clear history of BASH commands - here c is to clear history and w to write current history
history -dw 420
# To remove a certain line only for example say 420
history -r
# to clear current session history
<space>command
# to run commands without saving its trace - put a space before the command and it wont be saved
unset HISTFILE
# To stop saving history for current session
history | awk '{print $2}' | sort | uniq -c | sort -rn | head
# List your most used commands (sorted)
history | awk -F\ '{ $1=""; print $0; }' | sort | uniq | wc -l
# This command should tell you how many unique shell commands you've fired so far. You should have $HISTFILESIZE set to a large number or not set at all, to store lots of command history.
.---------------------------------------------------------------------------.
| |
| Bash History Cheat Sheet |
| |
'---------------------------------------------------------------------------'
| Peteris Krumins ([email protected]), 2008.02.15 |
| http://www.catonmat.net/download/bash-history-cheat-sheet.txt |
| |
| Released under the GNU Free Document License |
'---------------------------------------------------------------------------'
===================== Emacs Keyboard Shortcut Summary =====================
.--------------.------------------------------------------------------------.
| | |
| Shortcut | Description |
| | |
'--------------+------------------------------------------------------------'
| C-p | Fetch the previous command from the history list. |
'--------------+------------------------------------------------------------'
| C-n | Fetch the next command from the history list. |
'--------------+------------------------------------------------------------'
| M-< | Move to the first line in the history. |
'--------------+------------------------------------------------------------'
| M-> | Move to the end of the input history. |
'--------------+------------------------------------------------------------'
| C-r | Search backward starting at the current line (incremental) |
'--------------+------------------------------------------------------------'
| C-s | Search forward starting at the current line (incremental). |
'--------------+------------------------------------------------------------'
| M-p | Search backward using non-incremental search. |
'--------------+------------------------------------------------------------'
| M-n | Search forward using non-incremental search |
'---------------------------------------------------------------------------'
======================= Vi Keyboard Shortcut Summary ======================
.--------------.------------------------------------------------------------.
| | |
| Shortcut | Description |
| | |
'--------------+------------------------------------------------------------'
| k | Fetch the previous command from the history list. |
'--------------+------------------------------------------------------------'
| j | Fetch the next command from the history list. |
'--------------+------------------------------------------------------------'
| /string or | Search history backward for a command matching string. |
| CTRL-r | |
'--------------+------------------------------------------------------------'
| ?string or | Search history forward for a command matching string. |
| CTRL-s | (Note that on most machines Ctrl-s STOPS the terminal |
| | output, change it with stty (Ctrl-q to resume)). |
'--------------+------------------------------------------------------------'
| n | Repeat search in the same direction as previous. |
'--------------+------------------------------------------------------------'
| N | Repeat search in the opposite direction as previous. |
'--------------+------------------------------------------------------------'
| G | Move to history line N (for example, 15G). |
'---------------------------------------------------------------------------'
======================== History Expansion Summary ========================
Event Designators:
.--------------.------------------------------------------------------------.
| | |
| Designator | Description |
| | |
'--------------+------------------------------------------------------------'
| ! | Start a history substitution. |
'--------------+------------------------------------------------------------'
| !! | Refer to the last command. |
'--------------+------------------------------------------------------------'
| !n | Refer to the n-th command line (try history command). |
'--------------+------------------------------------------------------------'
| !-n | Refer to the current command line minus n. |
'--------------+------------------------------------------------------------'
| !string | Refer to the most recent command starting with 'string'. |
'--------------+------------------------------------------------------------'
| !?string? | Refer to the most recent command containing 'string'. |
'--------------+------------------------------------------------------------'
| ^str1^str2^ | Quick substitution. Repeat the last command, replacing |
| | 'str1' with 'str2'. |
'--------------+------------------------------------------------------------'
| !# | Refer to the entire command line typed so far. |
'---------------------------------------------------------------------------'
Word Designators:
(Word designators follow the event designators, separated by a collon ':')
.--------------.------------------------------------------------------------.
| | |
| Designator | Description |
| | |
'--------------+------------------------------------------------------------'
| 0 | The zeroth (first) word in a line (usually command name). |
'--------------+------------------------------------------------------------'
| n | The n-th word in a line. |
'--------------+------------------------------------------------------------'
| ^ | The first argument (the second word) in a line. |
'--------------+------------------------------------------------------------'
| $ | The last argument in a line. |
'--------------+------------------------------------------------------------'
| % | The word matched by the most recent string search. |
'--------------+------------------------------------------------------------'
| x-y | A range of words from x to y (-y is synonymous with 0-y). |
'--------------+------------------------------------------------------------'
| * | All words but the zeroth (synonymous with 1-$). |
'--------------+------------------------------------------------------------'
| x* | Synonymous with x-$ |
'--------------+------------------------------------------------------------'
| x- | The words from x to the second to last word. |
'---------------------------------------------------------------------------'
Modifiers (modifiers follow word designators, separated by a colon):
.--------------.------------------------------------------------------------.
| | |
| Modifier | Description |
| | |
'--------------+------------------------------------------------------------'
| h | Remove a trailing pathname component, leaving the head. |
'--------------+------------------------------------------------------------'
| t | Remove all leading pathname component, leaving the tail. |
'--------------+------------------------------------------------------------'
| r | Remove a trailing suffix of the form .xxx, leaving the |
| | basename. |
'--------------+------------------------------------------------------------'
| e | Remove all but the trailing suffix. |
'--------------+------------------------------------------------------------'
| p | Print the resulting command but do not execute it. |
'--------------+------------------------------------------------------------'
| q | Quotes the substituted words, escaping further |
| | substitutions. |
'--------------+------------------------------------------------------------'
| x | Quotes the substituted words, breaking them into words at |
| | blanks and newlines. |
'--------------+------------------------------------------------------------'
| s/old/new/ | Substitutes 'new' for 'old'. |
'--------------+------------------------------------------------------------'
| & | Repeats the previous substitution. |
'--------------+------------------------------------------------------------'
| g | Causes s/old/new/ or & to be applied over the entire |
| | event line. |
'---------------------------------------------------------------------------'
============ History Behavior Modification via Shell Variables ============
.----------------.----------------------------------------------------------.
| | |
| Shell Variable | Description |
| | |
'----------------+----------------------------------------------------------'
| HISTFILE | Controls where the history file gets saved. |
| | Set to /dev/null not to save the history. |
| | Default: ~/.bash_history |
'----------------+----------------------------------------------------------'
| HISTFILESIZE | Controls how many history commands to keep in HISTFILE |
| | Default: 500 |
'----------------+----------------------------------------------------------'
| HISTSIZE | Controls how many history commands to keep in the |
| | history list of current session. |
| | Default: 500 |
'----------------+----------------------------------------------------------'
| HISTIGNORE | Controls which commands to ignore and not save to the |
| | history list. The variable takes a list of |
| | colon separated values. Pattern & matches the previous |
| | history command. |
'---------------------------------------------------------------------------'
============ History Behavior Modification via shopt Command ============
.----------------.----------------------------------------------------------.
| | |
| shopt Option | Description |
| | |
'----------------+----------------------------------------------------------'
| histappend | Setting the variable appends current session history to |
| | HISTFILE. Unsetting overwrites the file each time. |
'----------------+----------------------------------------------------------'
| histreedit | If set, puts a failed history substitution back on the |
| | command line for re-editing. |
'----------------+----------------------------------------------------------'
| histverify | If set, puts the command to be executed after a |
| | substitution on command line as if you had typed it. |
'---------------------------------------------------------------------------'
shopt options can be set by a shopt -s option and
can be unset by a shopt -u option.
=============================== Examples ==================================
$ echo a b c d e (executes `echo ab c d e`)
a b c d e
$ echo !!:3-$ (executes `echo c d e`)
c d e
$ echo !-2:*:q (executes `echo 'a b c d e'`)
a b c d e
$ echo !-3:1:2:4:x (executes `echo 'a' 'b' 'd'`)
a b d
$ echo !-4:1-3:s/a/foo/:s/b/bar/:s/c/baz/ (executes `echo foo bar baz`)
foo bar baz
$ tar -xzf package-x.y.z.tgz
...
$ cd !-1:$:r (executes `cd package-x.y.z`)
package-x.y.z $
$ ls -a /tmp
file1 file2 file3 ...
$ ^-a^-l^ (executes `ls -l /tmp`)
-rw------- 1 user user file1
...
=============================== END ==================================
history | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn | head
477 l
444 git
424 cd
402 vim
106 rails
58 rm
53 rake
46 fg
43 rc
42 java
# Ein paar kleine Tricks machen die Nutzung der Bash-History noch effizienter.
# Die meisten Benutzer der Bash-Shell lernen ziemlich schnell die History-Funktion zu schätzen, die die meisten über die Cursor-Tasten abrufen. Alternativ geht das auch mit anderen Tasten wie [Strg]-[p] und [Strg]+[n]. Mit [Strg]+[r] lässt sich die History sogar durchsuchen, wenn man danach Teile des gesuchten Kommandos eintippt.
# Trivialitäten wie "pwd" oder "cd" muss die History nicht unbedingt speichern, denn das verschwendet nur unnötig Platz im begrenzten Speicher (der sich übrigens mit der Umgebungsvariablen "HISTSIZE" einstellen lässt). Auf vielen Systemen verzichtet die Bash auch auf eine Speicherung, wenn man dem Befehl ein Leerzeichen voranstellt. Dies wird durch die Variable "HISTCONTROL" gesteuert, die dann "ignorespace" enthalten muss.
# Bestimmte Befehle ignoriert die History, wenn man die Variable "HISTIGNORE" entsprechend setzt:
export HISTIGNORE="pwd:ls:ls -l:cd"
# Der Befehl muss dabei genau dem aufgeführten entsprechen, damit es klappt. Mit dem Spezialzeichen "&", das für den letzten Befehl steht, verzichtet die Bash darauf, unmittelbar aufeinander folgende doppelte Befehle zu speichern. Zum gleichen Effekt führt "HISTCONTROL=ignoredups". Global verhindert die Einstellung "HISTCONTROL=erasedups" duplizierte Befehle.
# Mit dem Befehl "history" lässt sich die History-Funktion steuern. So löscht "history -c" den aktuellen Speicher, "history -d X" löscht den letzten X-ten Eintrag, und "history -w" schreibt das Protokoll in die Datei (per Default "~/.bash_history"). Mehr Informationen zum History-Befehl sind übrigens in der Bash-Manpage zu finden, während "man history" Informationen zur History-Bibliothek enthält.
# https://robert-wohlfahrt.de/2017/10/loeschen-von-zeilen-aus-der-bash-history.html
# https://www.thegeekstuff.com/2009/09/multitail-to-view-tail-f-output-of-multiple-log-files-in-one-terminal/
# Ihr könnt dazu mit dem Editor eurer Wahl in die Datei ~/.bashrc wechslen und überprüfen, ob die Zeile vorhanden ist. Wenn nicht, dann könnt ihr die Zeile einfach hinzufügen und das Terminal neustarten, damit die .bashrc eingelesen wird. Setzt ihr dann ein Leerzeichen vor ein Kommando, das ihr nicht archiviert haben wollt, wird dieses auch nicht gespeichert. Es ist somit auch nicht mehr über die Pfeiltasten erreichbar.
export HISTCONTROL=ignorespace
HISTIGNORE="clear:bg:fg:cd:cd -:exit:date:w:* --help"
# Colon seperated list of exact commands to ignore for storing in history in BASH.
# Do not save command history of current bash session
HISTFILE=
# Explanation: The command history of the current bash session is saved to $HISTFILE when you exit the shell. If this variable is blank, the command history will not be saved.
history -c
# clear your active shell's history in case you do something stupid like paste a password+newline into your terminal.
# Show top ten most used commands on the shell
history | cut -d' ' -f 1 | uniq -c | sort -r | head -n10
# To view the specific number of top most used commands, run the following command in a Terminal.
history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -nr | head -n x
# To view the most used commands, run the following command in a Terminal:
history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort –nr
#If you want to view the history list in a reverse order that is the least used at the top and the most used commands at the bottom, you can easily do that. Remove the r option for the second sort like shown in the below command.
history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -n | head -n 30
# You can also view the history list of only those commands that occur for once, twice or for any specific number of times. For that, use the following syntax:
history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -n | grep ' x '
# Replace x with any desired number. For instance, to view the list of commands that only occurred once use the below command:
history | awk 'BEGIN {FS="[ \t]+|\\|"} {print $3}' | sort | uniq -c | sort -n | grep ' 1 '
# Run sudo on the last command in your history. Be careful though. <Up Arrow> & [Ctrl-a] sudo can be fast too and perhaps safer. You can also use sudo !!:p first to print what will be run and then !! after to run it.
# Explanation: !! (bang bang!) is replaced with the previous command. You can read more about it and other history expansion commands in man bash in the Event Designators section.
sudo !!
# Recall “N”th command from your BASH history without executing it.
!12:p
# List or edit and re-execute commands from the history list
fc -l
# Explanation: fc is a little known but very useful bash built-in.
# fc -l will list the most recent 16 commands
# fc will open the last command in a text editor defined in the environmental variable FCEDIT or EDITOR or else vi, and re-execute when you exit
# fc 5 9 will open the history entries 5 to 9 in a text editor
# fc -s pat=sub will run the last command after substituting pat with sub in it (does not open editor)
# fc -s pat=sub cc is the same but on the last command starting with cc
# fc -s cc will run the last command starting with cc
# Bash interactive shell tip when using server API tokens; prevent single commands from being logged to `.bash_history` via...
GITHUB_TOKEN=123456 node index.js; history -d $((HISTCMD-1))
# Or clean-up after with. hint, `{}` (curly-braces) between BEGIN and END blocks is an implied loop over each input line.
sed -e '/TOKEN/d' -i .bash_history; history -d $((HISTCMD-1))
#-----------------------------------------------------------------------///
#==============================##==============================#
# CMD history #
#==============================##==============================#
Cheatsheets are an excellent complement to other information sources like Linux man-pages, Linux help, or How-To’s and tutorials, as they provide compact and easily accessible information. While man-pages and detailed tutorials often contain comprehensive explanations and extensive guides, cheatsheets summarize the most important options forthe command history in a clear format. This allows users to quickly access the needed information for history without having to sift through lengthy texts. Especially in stressful situations or for recurring tasks, cheatsheets for history are a valuable resource to work efficiently and purposefully.
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
█║▌│║█║▌★ KALI ★ PARROT ★ DEBIAN 🔴 PENTESTING ★ HACKING ★ █║▌│║█║▌
██╗ ██╗ ██████╗ ██████╗ ██╗ ██╗███████╗██████╗
████████╗██╔══██╗██╔═══██╗╚██╗██╔╝██╔════╝██╔══██╗
╚██╔═██╔╝██║ ██║██║ ██║ ╚███╔╝ █████╗ ██║ ██║
████████╗██║ ██║██║ ██║ ██╔██╗ ██╔══╝ ██║ ██║
╚██╔═██╔╝██████╔╝╚██████╔╝██╔╝ ██╗███████╗██████╔╝
╚═╝ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚══════╝╚═════╝
█║▌│║█║▌ WITH COMMANDLINE-KUNGFU POWER █║▌│║█║▌
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.