history Linux Command
What is Linux history Command?
Explanation
history COMMAND:history command is used to list out the recently executed commands in the number line order.
SYNTAX :
history [-c] [-d offset ] [ n ]
history -anrw [ filename ]
history -ps arg [ arg... ]
OPTIONS:
Options | Description |
-a | Attaches history lines with the history page from this session. |
-n | Read the unread messages from the history lines. |
-r | Attaches all the content to the history list after reading the history files. |
-d offset | delete the history entry at offset OFFSET. |
-c | Clears the complete history list. |
-w | Attaches the current history from the current file to the history list. |
-p | Expands history in each arg and shows the output without storing it in the history list. |
-s | Attaches the arg with the history list and stores as single list. |
EXAMPLE:
- To list the recently executed commands:
history
Output
121ping google.com
122ping 192.168.0.1
123ping google.com
124ping 192.168.0.1
125cd /home/ganesh/Documents/backup/local/
126mv Make_words/ /opt/lampp/htdocs/android/
127mv firstapp/ /opt/lampp/htdocs/android/
128whereis JDK
129whereis java
130java -version
131whereis java
This command is used to list the history .
- To find specific command in history list:
history | grep cd
Output
33 cd Pictures/
37 cd ..
39 cd Desktop/
61 cd /usr/bin/
68 cd
83 cd /etc/
86 cd resolvconf/
90 cd resolv.conf.d/
This command is used to list only cd commands from history list.
- To copy history list to file:
history -a history.txt
Output
In history.txt file.
3 121ping google.com
122ping 192.168.0.1
123ping google.com
124ping 192.168.0.1
125cd /home/ganesh/Documents/backup/local/
126mv Make_words/ /opt/lampp/htdocs/android/
127mv firstapp/ /opt/lampp/htdocs/android/
128whereis JDK
129whereis java
130java -version
131whereis java
This command is used to copy history list to history.txt file .
- To Clear all history:
history -c
This command can be used to clear all history from history list.