logo
  • Blog
  • The Cloud
  • DevOps
  • Trending
    • Blockchain
    • Bitcoin
  • How To
  • Kubernetes

How To Use Linux Bash History Commands

Posted on November 16, 2013
Published on: Nov 13, 2013 | Last updated on: Nov 14, 2013 |
Author: Sushil Verma Sushil Verma (Linkedin, Twitter, Git)

 2,779 total views,  2 views today

How To Use Linux Bash History Commands was last modified: July 17th, 2020 by Sushil Verma
Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email

How To Use Linux Bash History Commands

While working on Linux, you spend most of the time on command line terminal. Mostly you use bash shell, which is default in most of the Linux distribution.

Also while working on terminal, you repeat most of the commands. Typing command on terminal is a good practice for the beginners.

Linux bash shell provides awesome history command. Which captures the history of the command run on terminal.
In this article, we will see how to use history feature provided by bash shell.

Setting History Defaults

Before using the history, its a good practice to adjust some bash settings to make it more useful.

HISTFILESIZE: Parameter configures how many commands are kept in the history file.

HISTSIZE: Controls the number stored in memory for the current session.

You can set reasonable cap for the size of history in memory for the current session, and have an even larger history saved to disk that you can examine at a later time.

Open your ~/.bashrc file with your favirout editor to change these settings:

vim ~/.bashrc

If we want to have bash immediately add commands to our history instead of waiting for the end of each session (to enable commands in one terminal to be instantly be available in another), we can also set or append the history -a command to the PROMPT_COMMAND parameter, which contains commands that are executed before each new command prompt.

To do this correctly, we need to do a bit of a hack. We need to append to the history file immediately with history -a, clear the current history in our session with history -c, and then read the history file that we’ve appended to, back into our session history with history -r.

You can add below line of code in ~/.bashrc file.

export PROMPT_COMMAND="history -a; history -c; history -r; $PROMPT_COMMAND"

To implement your changes immediatly, either re-loging to the system or source the file as given below.

source ~/.bashrc

Review your previous bash history

We use hostory command to review the bash command history. This command will print out our recent commands (one command per line).

history

Output:

. . .
43  man bash
44  man fc
45  man bash
46  fc -l -10
47  history
48  ls -a
49  vim .bash_history
50  history
51  man vim
52  history 10
53  history

It also prints the history number for each command. Each command is associated with a number for easy reference. You will see why this is useful in a moment.

See only last 5 commands

You can use following command to print only last 5 commands:

history 5

Find a certain string in history command

Use grep to filter command history. Lets take an example of finding command contains cd command, as below:

history | grep cd

Output:

. . .
37  cd ..
39  cd Desktop/
61  cd /usr/bin/
68  cd
83  cd /etc/

Executing Commands from your Bash History

We can re-execute any of our history command by its number preceded by an exclamation poit (!).

For example: from my history command, if I want to re-execute man vim command then I will simpley use below command:

!51

Above command will recall and execute the command associated with the history number 51.

 2,778 total views,  1 views today

How To Use Linux Bash History Commands was last modified: July 17th, 2020 by Sushil Verma
Share on Facebook
Facebook
0Tweet about this on Twitter
Twitter
Share on LinkedIn
Linkedin
Email this to someone
email
Posted in Bash shell, Linux, Ubuntu | Tags: bash_shell, Linux, ubuntu |
« Ceilometer an efficient metering/billing for OpenStack
IBM Cloud Bluemix – Platform as a Service (PaaS) for beginners! »

Leave a comment Cancel reply

Your email address will not be published.

Latest Blogs

  • Linux: Viewing Log Messages
  • AWS CodeBuild: Getting Started
  • AWS CodeCommit: Set up Notifications
  • AWS CodeCommit: Securing The Repository and Branches
  • Managing Systemd units in Linux

Tags

Amazon EC2 AWS bash_shell bitcoin blockchain Cloud computing CodeCommit DevOps digital currency Kubernetes Linux trending ubuntu

For Improving Education

Categories

  • Amazon EC2
  • Amazon Web Services
  • AWS
  • AWS CodeCommit
  • Bash shell
  • Best Practices
  • Bitcoin
  • Blockchain
  • Chaincode
  • CLI
  • Cloud Computing
  • Cloud Security
  • CodeBuild
  • CodeCommit
  • CryptoCurrency
  • Cryptography
  • DevOps
  • Digital Currency
  • EC2 Lambda
  • Hyperledger
  • IBM Bluemix
  • IBM Garage
  • Kubernetes
  • Linux
  • Monitoring
  • OpenStack
  • Platform as a Service
  • TDD
  • Trending
  • Ubuntu
  • Virtual Server
Home | Site Map | Privacy Policy | Site Terms | Terms of use @2013, Times of Cloud.
The content is copyrighted to 'Times of Cloud' and may not be reproduced on other websites.