How to use history command effectively In Linux

Most of the Linux server admins spends a lot of time on the command line. Hence history command is once of the essential command for them.

BASH stands for (Bourne Again SHell) is the default command line shell that is used in most of the major Linux distributions to interact with operating system.

What is History Command

The history command is used to list Bash’s log of the commands previously you have typed.

This log is called the “history” which allows us to quickly see what has been executed previously on a system.

BASH writes all the commands which you have ran when you close the Terminal session to your ~/.bash_history file.

When you run two session concurrently and the history command will show the current session history alone and doesn’t shows another session history.

How to read History Command

Run history command alone without any options to view the commands which you have previously ran. If you had huge history logs, you can’t see those properly. In this case you have to use the file view commands such as less, more, etc,.

# history
or
# history | more
or
# history | less
or
# history | head -20
or
# history | tail -15
  .
  .
  759  ls -lh
  760  touch magi.txt
  761  echo "Magi-Thanu" >>magi.txt
  762  cat magi.txt
  763  grep -r "Magi-Thanu" /
  764  grep -r "Magi-Thanu" / 2> /dev/null
  765  grep -r /* | grep "Magi-Thanu" / 2> /dev/null
  766  grep -r /* | grep "Magi-Thanu"
  767  grep -r /* | grep "Magi-Thanu" 2> /dev/null
  768  find / -xdev -type f -print0 | xargs -0 grep -H "Magi-Thanu" 2> /dev/null
  769  grep -r /* | grep "Magi-Thanu" 2> /dev/null
  770  exit
  771  histor
  772  history
  773  hostname
  774  date
  775  uname -a
  776  history

How to view last “n” Commands

By default, history command shows the last 1000 commands executed by us. If you want to list specific number of commands , use the following command history n. For example, to display last 10 history, run the following command.

# history 10
  764  grep -r "Magi-Thanu" / 2> /dev/null
  765  grep -r /* | grep "Magi-Thanu" / 2> /dev/null
  766  grep -r /* | grep "Magi-Thanu"
  767  grep -r /* | grep "Magi-Thanu" 2> /dev/null
  768  find / -xdev -type f -print0 | xargs -0 grep -H "Magi-Thanu" 2> /dev/null
  769  grep -r /* | grep "Magi-Thanu" 2> /dev/null
  770  exit
  771  # history -10
  772  history -10
  773  history 10

How to run previously executed Command

  • Use the UP and DOWN Arrow key’s to scroll through previously typed commands.
  • Press Enter to execute them or use the Left and Right arrow keys to edit the command.

Repeat Last Command

To repeat the last command which you executed. Run the following command !! - Double exclamation point.

# uname -a
Linux vps138235 4.4.0-108-generic #131-Ubuntu SMP Sun Jan 7 14:34:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

# !!
uname -a
Linux vps138235 4.4.0-108-generic #131-Ubuntu SMP Sun Jan 7 14:34:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Repeat Last Command Using Its order

Repeat previously executed command using it’s number.
This will quickly run previously executed command easily instead of typing a big command.

# !774
uname -a
Linux vps138235 4.4.0-108-generic #131-Ubuntu SMP Sun Jan 7 14:34:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

Search and run Previously Executed Commands

History command offering the variety of search options. This will allows us to search and run previously executed commands.

  • Run the command that matches the string at the beginning of the command
  • Run the command that matches given string

To perform this activity, I took last 30 history logs.

# history | tail -30
  751  du
  752  top -c
  753  cat /etc/*-release
  754  docker ps
  755  history
  756  docker run -ti fedora /bin/bash
  757  ls -lh
  758  cd /opt
  759  ls -lh
  760  touch magi.txt
  761  echo "Magi-Thanu" >>magi.txt
  762  cat magi.txt
  763  grep -r "Magi-Thanu" /
  764  grep -r "Magi-Thanu" / 2> /dev/null
  765  grep -r /* | grep "Magi-Thanu" / 2> /dev/null
  766  grep -r /* | grep "Magi-Thanu"
  767  grep -r /* | grep "Magi-Thanu" 2> /dev/null
  768  find / -xdev -type f -print0 | xargs -0 grep -H "Magi-Thanu" 2> /dev/null
  769  grep -r /* | grep "Magi-Thanu" 2> /dev/null
  770  exit
  771  # history -10
  772  history -10
  773  history 10
  774  uname -a
  775  history
  776  uname -a
  777  grep -r /* | grep "Magi-Thanu" 2> /dev/null
  778  history
  779  history | tail -25
  780  history | tail -30

1) If you want to run the command that matches the string at the beginning of the command. Use the following format. It will run the most recent command that begins with doc string.

In my case, it will run the docker run command which is located in the 756 row.

# !doc
docker run -ti fedora /bin/bash
[root@54bc0e2c1323 /]# cat /etc/fedora-release
Fedora release 27 (Twenty Seven)

2) If you want to run the command that matches given string. Use the following format.

In my case, it will run the docker ps command which is located in the 754 row.

# !? ps
or
# !? ps?
docker ps
CONTAINER ID        IMAGE                             COMMAND                  CREATED             STATUS              PORTS                              NAMES
25431e3d61fc        rancher/scheduler:v0.8.3          "/.r/r /rancher-entr…"   5 days ago          Up About an hour                                       r-scheduler-scheduler-1-297af68f
5420cdb768bc        centos                            "/bin/bash"              5 days ago          Up 5 days                                              festive_colden
f453843a43f4        centos:latest                     "/bin/bash"              7 days ago          Up 7 days                                              gifted_kepler
6a385828575a        fedora                            "/bin/bash"              7 days ago          Up 7 days                                              dreamy_einstein
ca5148e8d4f6        centos                            "/bin/bash"              7 days ago          Up 7 days                                              nifty_joliot
6f49f5fd6e49        fedora                            "/bin/bash"              10 days ago         Up 7 days                                              tender_bhabha
73878066ab70        centos:latest                     "/.r/r /bin/bash"        2 weeks ago         Up 2 weeks                                             r-CentOS-2307d873
a8f8bda21205        rancher/healthcheck:v0.3.3        "/.r/r /rancher-entr…"   2 weeks ago         Up 2 weeks                                             r-healthcheck-healthcheck-2-9d8b4357
507817dbf425        rancher/network-manager:v0.7.19   "/rancher-entrypoint…"   2 weeks ago         Up 2 weeks                                             r-network-services-network-manager-2-b5a2e972
009034ba6536        rancher/net:v0.13.7               "/rancher-entrypoint…"   2 weeks ago         Up 2 weeks                                             r-ipsec-ipsec-connectivity-check-2-c5addb83
6fb051f8d176        rancher/net:v0.13.7               "/rancher-entrypoint…"   2 weeks ago         Up 2 weeks                                             r-ipsec-ipsec-router-2-ab035109
fdd5cd881fc1        rancher/dns:v0.15.3               "/rancher-entrypoint…"   2 weeks ago         Up 2 weeks                                             r-network-services-metadata-dns-2-44490e8d
26cf808ef887        rancher/net:holder                "/.r/r /rancher-entr…"   2 weeks ago         Up 2 weeks                                             r-ipsec-ipsec-2-ff75d784
8a82a7ae9192        rancher/metadata:v0.9.5           "/rancher-entrypoint…"   2 weeks ago         Up 2 weeks                                             r-network-services-metadata-2-d034268c
c2cc6d345430        rancher/net:v0.13.7               "/rancher-entrypoint…"   2 weeks ago         Up 2 weeks                                             r-ipsec-cni-driver-1-8c666e6b
3415a1fd101f        rancher/agent:v1.2.9              "/run.sh run"            2 weeks ago         Up 2 weeks                                             rancher-agent
008e8d292f46        rancher/server:stable             "/usr/bin/entry /usr…"   3 weeks ago         Up 5 days           3306/tcp, 0.0.0.0:8080->8080/tcp   sharp_hypatia

How to replace the string and run previously executed commands

If you want to repeat the last command with new string. Just replace the string1 with string2 (Equivalent to “!!:s/string1/string2/”).

Use the following format. In the below example first I have ran Fedora docker image then ran the Centos docker image by replacing the string with CentOS instead of Fedora.

# !794
docker run -ti fedora /bin/bash
[root@029af8069eaa /]# uname -a
Linux 029af8069eaa 4.4.0-108-generic #131-Ubuntu SMP Sun Jan 7 14:34:49 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
[root@029af8069eaa /]# exit
exit

# ^fedora^centos
docker run -ti centos /bin/bash
[root@710ea49f548b /]# cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core)
[root@710ea49f548b /]# exit
exit

If you want to run any specific command which was executed previously with new string, use the following any one of the command format.

In my case, 821 line history command was fedora docker container. To run centos container, just replace the word fedora to centos.

# !821:s|fedora|centos
docker run -ti centos /bin/bash
[root@e37a89af2959 /]# exit
exit

or

# !821:s/fedora/centos
docker run -ti centos /bin/bash
[root@050610f3198b /]#

List the matching string using grep command

If you have ran a lot of commands against docker. It could be very easy to display list of commands matching with docker command using grep command.

# history | grep docker
   23  docker ps
   24  docker images
   25  docker pull centos
   26  docker ps
  503  docker --version
  504  docker ps
  505  docker pull fedora
  506  docker pull debian
  507  docker pull archlinux
  508  docker pull arch
  509  docker pull arch linux
  510  docker pull opensuse
  516  docker run -ti fedora /bin/bash
  517  docker run -ti debian /bin/bash
  518  docker run -ti centos /bin/bash

How to display Date and Time in History Command

By default history command shows only the commands executed by users and it doesn’t print the date and time but it logs the time when you ran a command.

If you want to add date and time in history command output, just navigate to the following link.

Suggested Read : How To Display Date And Time In History Command

How to use YUM History Command to rollback an updates

Yum (Yellowdog Update Modified) is RPM Package Management utility for CentOS and Red Hat systems, Yum history command allows administrator to rollback the system to a previous state but due to some limitations, rollbacks do not work in all situations, or The yum command may simply do nothing, or it may remove packages you do not expect.

Suggested Read : How To Use YUM History Command To Rollback An Updates

How to view other users History

Use the following format to view other users history.

# more /home/$USER/.bash_history

How to search through bash History

We can search previously executed command using a keyword by hitting CTRL-r (This searching backwards in history and display most recent results first) key. Press CTRL-r again to see the next result.

# 
(reverse-i-search)`dock': docker run -ti centos /bin/bash

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *