History Command with Examples

History Command is used to view the previously executed command and it can also list it.

This feature was not available in the Bourne shell.

Bash and Korn support this feature in which every command executed is treated as the event and is associated with an event number using which they can also be recalled and changed if required.These commands are saved in a history file. in Bash shell history command shows the whole list of the command.

Syntax:

 history 

Important Points

  1. show the limited number of commands that executed as follows:
 history 5 

Note:

 The command can also be executed using event number .

Example:

 !231

2. you can also print command before executing it by using :p after event number of command.

Example:

!231:p

3. to execute the 6th command from the History list Bottom :

 !-6

4.This Command can also be used along with grep:

Example:

history | grep yum

5. The most recent command can be viewed using !!

Example:

 !!

6. So suppose if the command has to be executed without being stored variable HISTFILE has to be unset.

So the command will be:

Example:

7. The command can also be executed using a part of the command.

Example:

!command_starting_string

8. History can also be removed using history -d event_number.

Example:

history -d 566

NOTE:

The “.bash_history” hidden file stores every command you wrote, every user has a bash history even the super user ‘root’.

9. the whole history can be removed using history -c option but it can’t be removed from the “.bash_history“.

Example:

history -c

10. So to remove the whole history from the “.bash_history” file you can use this :

 echo " " > .bash_history

11. You can also know your history list size :

 echo $HISTSIZE
Qadry
unixcop Admin

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest articles

You might also likeRELATED

How to Count Number of Files and Sub-directories inside a Directory

Hi guys, In this article we will show you how to count the number of files and sub-directories in a directory using a tree command. Count number...

Find Top 10 IP Addresses Accessing Your Apache Web Server

In this post, you will learn how to Find Top 10 IP Addresses Accessing Your Apache Web Server- The monitoring access to your web server...

How to Install MongoDB in CentOS 8

Introduction MongoDB is a source-available cross-platform document-oriented database program. Classified as a NoSQL database program, MongoDB uses JSON-like documents with optional schemas. MongoDB is developed by MongoDB Inc. and licensed under the Server Side Public License (SSPL). MongoDB is a...