Cyberithub

20 Useful Linux History Command Examples | Bash Command History

Advertisements

In this article, I will take you through 20 Useful Linux History Command Examples. As a Linux Professional you must be running multiple commands every day in your system but did you ever thought if these commands gets recorded anywhere or can you fetch the commands which you ran in the past ? The answer to this question is yes your Linux Server does record all the commands in a default hidden file known as .bash_history under the User home directory. You can also change the name and path of the file using HISTFILE variable. You can check and access all the recorded commands by using Linux history command.

20 Useful Linux History Command Examples | Bash Command History 1

Linux History Command Examples

Also Read: 31 Useful grep command examples in Linux/Unix(How to Use grep command)

Example 1: How to Check Command History using Linux history command

If you want to check Linux Command history then you need to use simple history command as shown below. This command will show all the previous commands ran from the logged in user. You might think how many commands does it show from the history ? The answer is it is limited by the HISTFILESIZE and HISTFILE value. By default, you will see 1000 commands saved in the bash_history file.

[root@localhost ~]# history
3 grep -n cyberit *
4 grep -l cyberit *
5 grep -L cyberit *
6 man grep
7 grep "^this" *
8 grep -i "^this" *
9 grep -i "cyberithub$" *
10 grep -e this -e cyberit *
11 grep -e is -e from *
12 grep [0-9][0-9] /var/log/messages
13 grep [0-2][0-1] /var/log/messages
14 grep 17:22:5[2-4] /var/log/messages

NOTE:

Please note that here I am using root user to run all the below commands. Hence all the commands are saved in the root user command history.

Example 2: How to List top 10 Lines of Bash Command History

If you want to check top 10 Lines of bash command history then you need to use head command on history command output as shown below. Please note that when we say top 10 lines of Linux history command output then it necessarily means the top 10 older commands of Linux history command output.

[root@localhost ~]# history | head
12 grep [0-9][0-9] /var/log/messages
13 grep [0-2][0-1] /var/log/messages
14 grep 17:22:5[2-4] /var/log/messages
15 man grep
16 grep cyber[e-j]thub *
17 grep cyber[x-z]thub *
18 grep cyber[e-j][r-t]hub *
19 ifconfig -a | grep -i enp0s3
20 grep -E cyber+thub *
21 grep --binary-file=text which

Example 3: How to run Some Specific Line Command of Bash Command History

If you want to run any of the specific Line command from Linux command history then you need to use Line number with ! mark as shown below. In this example, we are trying to run command of 19th Line by using !19 as specified below.

[root@localhost ~]# !19
ifconfig -a | grep -i enp0s3
enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500

Example 4: How to List bottom 10 Lines of Bash Command History 

If you want to List bottom 10 Lines of bash command history then you need to use tail command on history command output as shown below. It is worth noting here that bottom 10 means recent 10 commands from Linux history command.

[root@localhost ~]# history | tail
1004 history -n 8
1005 echo grep -i "^this" *
1006 grep -i "^this" *
1007 *(8)
1008* echo grep -i "^this" * $
1009 echo grep -i "^this" * $ 8
1010 history
1011 history | head
1012 ifconfig -a | grep -i enp0s3
1013 history | tail

Example 5: How to Clear Bash Command History using Linux History Command

If you want to clear complete bash command history or Linux history then you need to use history -c command as shown below. Please note that running history -c command will clear the history from current logged in session only. If you close the current session and open a new session you will see the same output from Linux history command.

This is because all the history commands are saved in bash_history of User profile which does not get removed by the history -c command. To permanently remove the commands from history you need to empty up the bash_history file.

[root@localhost ~]# history -c

Example 6: How to Add Timestamp in Linux History Commands

If you want to add timestamp in the Linux history commands then you need to set HISTIMEFORMAT variable as shown below. In this example, we are setting the value of HISTTIMEFORMAT to '%F %T'. It is important to note here that modifying the value of HISTTIMEFORMAT from here will be only a temporary change which will not work if you close the current session and open a new session. So to make it permanent you can set the format in .bashrc file under User Profile.

[root@localhost ~]# export HISTTIMEFORMAT='%F %T'
[root@localhost ~]# history | head
17 2020-06-15 01:38:04grep cyber[x-z]thub *
18 2020-06-15 01:38:04grep cyber[e-j][r-t]hub *
19 2020-06-15 01:38:04ifconfig -a | grep -i enp0s3
20 2020-06-15 01:38:04grep -E cyber+thub *
21 2020-06-15 01:38:04grep --binary-file=text which
22 2020-06-15 01:38:04grep --binary-files=text which
23 2020-06-15 01:38:04grep -i cyberithub * --color
24 2020-06-15 01:38:04grep -i cyberithub *
25 2020-06-15 01:38:04grep -i cyberithub * --color=blue
26 2020-06-15 01:38:04grep -i cyberithub * --color=always

Example 7: How to Search Linux Commands History Using Control+R

If you want to search any command from Linux history then you need to press Control+R and then write initials of that command to show the complete command. Then you can simply press Enter to run that command as shown below.

(reverse-i-search)`C': curl -# -a -T CentOS.ISO -u centos:Harmony@123 ftp://192.168.0.103

[root@localhost ~]# curl -# -a -T CentOS.ISO -u centos:Harmony@123 ftp://192.168.0.103
######################################################################## 100.0%

Example 8: How to show Recent N number of Lines using Linux History Command

If you want to show Recent N number of Line from Linux History command output then you need to mention the number of Lines with history command as shown below. In this example, we are trying to show recent 4 Lines of command from Linux history command output by using history 4 command as specified below.

[root@localhost ~]# history 4
1017 2020-06-15 14:00:30curl -XGET -H "Upgrade-Insecure-Requests": "1" https://google.com
1018 2020-06-15 14:00:47curl -# -a -T CentOS.ISO -u centos:Harmony@123 ftp://192.168.0.103
1019 2020-06-15 14:42:20histor 4
1020 2020-06-15 14:42:23history 4

Example 9: How to Repeat Last Command of Bash Command History

If you want to repeat last command from bash command history then you need to use !! as you can see below. As you can see from below output, previous ran command was history 4 so it ran this command again and shown the output below.

[root@localhost ~]# !!
history 4
1017 2020-06-15 14:00:30curl -XGET -H "Upgrade-Insecure-Requests": "1" https://google.com
1018 2020-06-15 14:00:47curl -# -a -T CentOS.ISO -u centos:Harmony@123 ftp://192.168.0.103
1019 2020-06-15 14:42:20histor 4
1020 2020-06-15 14:42:23history 4

Example 10: How to Run Previous Command starting with some word

If you want to run any previous command which starts with a certain word then you need to use that word with ! to run the complete command. In this example we are trying to use curl word with ! to run the recent curl command that we executed.

[root@localhost ~]# !curl
curl -# -a -T CentOS.ISO -u centos:Harmony@123 ftp://192.168.0.103
######################################################################## 100.0%

Example 11: How to check the total number of Lines using HISTFILESIZE and HISTSIZE

If you want to check the total number of Lines Linux History can save then you need to check the value of HISTFILESIZE and HISTSIZE using echo $HISTFILESIZE and echo $HISTSIZE command as shown below.

[root@localhost ~]# echo $HISTFILESIZE
1000
[root@localhost ~]# echo $HISTSIZE
1000

Example 12: How to Change the total number of Lines using HISTFILESIZE and HISTSIZE

If you want to change the total number of Lines in the command history then you need to modify the value of HISTFILESIZE and HISTSIZE using export command as shown below.

[root@localhost ~]# export HISTFILESIZE=1500
[root@localhost ~]# export HISTSIZE=1500

Now again if you check the value of HISTFILESIZE and HISTSIZE, you can see that value is changed to 1500 from 1000. Please note that modifying the value of HISTFILESIZE and HISTSIZE from here will be only a temporary change which will not work if you close the current session and open a new session. So to make it permanent you can set the variables in .bashrc file under User Profile.

[root@localhost ~]# echo $HISTFILESIZE
1500
[root@localhost ~]# echo $HISTSIZE
1500

Example 13: How to write the changes to bash_history File using Linux History Command

If you want to write all the changes done in the current session to bash_history file then you need to run history -w command as shown below.

[root@localhost ~]# history -w

Example 14: How to delete a Specific Line using Linux History Command

If you want to delete some specific line from Linux history then you can delete it by using -d option with history command as shown below. Before deleting any command we first need to identify that command.

[root@localhost ~]# history | head
30 2020-06-15 01:38:04grep --binary-files=text -i cyberithub * >> testfile.txt
31 2020-06-15 01:38:04cat testfile.txt
32 2020-06-15 01:38:04grep -i cyberithub * >> testfile.txt
33 2020-06-15 01:38:04cat testfile.txt
34 2020-06-15 01:38:04grep -i cyberithub * > testfile.txt
35 2020-06-15 01:38:04cat testfile.txt
36 2020-06-15 01:38:04grep -i cyberithub *
37 2020-06-15 01:38:04grep -i cyberithub file1.txt > testfile.txt
38 2020-06-15 01:38:04cat testfile.txt
39 2020-06-15 01:38:04grep -color cyberithub file1.txt

In this example, we are trying to delete line 34 from Linux Command History using history -d 34 command as shown below.

[root@localhost ~]# history -d 34

Again if you check the Linux command history you can see that Line 34 command is deleted and now Line 35 command becomes Line 34 command as shown below.

[root@localhost ~]# history | head
30 2020-06-15 01:38:04grep --binary-files=text -i cyberithub * >> testfile.txt
31 2020-06-15 01:38:04cat testfile.txt
32 2020-06-15 01:38:04grep -i cyberithub * >> testfile.txt
33 2020-06-15 01:38:04cat testfile.txt
34 2020-06-15 01:38:04cat testfile.txt
35 2020-06-15 01:38:04grep -i cyberithub *
36 2020-06-15 01:38:04grep -i cyberithub file1.txt > testfile.txt
37 2020-06-15 01:38:04cat testfile.txt
38 2020-06-15 01:38:04grep -color cyberithub file1.txt
39 2020-06-15 01:38:04grep -color cyberithub *

Example 15: How to Check History File Location using HISTFILE

If you want to check the location of bash_history file then you need to check the value of HISTFILE using echo $HISTFILE command as shown below.

[root@localhost ~]# echo $HISTFILE
/root/.bash_history

Example 16: How to Change History File location using Linux History Command

If you want to change the location of bash_history file then you need to change this File path in HISTFILE variable as shown below.

[root@localhost ~]# export HISTFILE=/root/example/.bash_history
[root@localhost ~]# echo $HISTFILE
/root/example/.bash_history

Example 17: How to check duplicate commands allowed in history using HISTCONTROL

If you want check that the duplicate commands are allowed or not in Bash Command history then you need to check the value of HISTCONTROL variable. If it is set to ignoredups then duplicate commands are not allowed.

[root@localhost ~]# echo $HISTCONTROL
ignoredups

Example 18: How to Ignore any command using HISTIGNORE

If you want to ignore any command to be saved in Command history then you need to put that command in HISTIGNORE variable as shown below. In this example, we trying to set less and pwd command in HISTIGNORE variable which will prevent these commands to appear in Linux history command output.

[root@localhost ~]# export HISTIGNORE=less:pwd
[root@localhost ~]# echo $HISTIGNORE
less:pwd

Example 19: How to Prevent any command to be recorded in Bash Command History

If you want any command to be recorded in history then you can simply unset the HISTFILE variable using unset HISTFILE command as shown below.

[root@localhost ~]# echo $HISTFILE
/root/.bash_history
[root@localhost ~]# unset HISTFILE
[root@localhost ~]# echo $HISTFILE

Example 20: How to Search a Recent command from Bash Command History

If you want to search a specific command from bash command history then you can simply grep that command from history command output as shown below. After that if you want to check only Recent 10 command then you can feed the output to tail command to shown the recent 10 value.

[root@localhost ~]# history | grep -i curl | tail
991 2020-06-15 01:38:04curl --help
992 2020-06-15 01:38:04man curl
993 2020-06-15 01:38:04man curl
995 2020-06-15 01:38:04man curl
998 2020-06-15 01:38:16man curl
1016 2020-06-15 14:00:30curl -XGET -H "Upgrade-Insecure-Requests": "1" https://google.com
1017 2020-06-15 14:00:47curl -# -a -T CentOS.ISO -u centos:Harmony@123 ftp://192.168.0.103
1020 2020-06-15 15:51:21curl -# -a -T CentOS.ISO -u centos:Harmony@123 ftp://192.168.0.103
1055 2020-06-15 17:24:42history | grep -i curl
1056 2020-06-15 17:24:49history | grep -i curl | tail

 

 

 

 

 

 

Recommended Posts:-

10 Useful iproute2 tools examples to Manage Network Connections in Linux

The Power of Linux History Command in Bash Shell

8 Most Popular mkdir command in Linux with Examples

26 Useful Firewall CMD Examples on RedHat/CentOS 7

How to Use Linux History Command

Linux History Command Tutorial for Beginners

Getting Started: Setup FreeRadius Server

5 Easy Steps to recover LVM2 Partition , PV , VG , LVM metadata in Linux

17 Bash Command History Examples

How to Configure and Setup FreeRadius Server

Leave a Comment