EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Linux History Command

By Priya PedamkarPriya Pedamkar

Home » Software Development » Software Development Tutorials » Linux Tutorial » Linux History Command

Linux History Command

Introduction to Linux History Command

In the Linux ecosystem, the history command is used to get the list of old command that have triggered by earlier in the same terminal. With the help of the history command, we are able to reuse the older command instead of typing the whole command. The history feature is not available in all the shell environment. It is available in bash, Korn. The history feature is not available in the Bourne shell. Whenever we are triggering any command on the Linux shell. The executed or non-executed command having their own event number with the help of the same event number. We are able to evoke the older command.

Syntax of History Command:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

history [OPTION]

  • history: We can use the history keyword in the syntax or command. It will take the argument as an option. It will provide the end result with the list of older commands or provide the list of specific commands only.
  • OPTION: We can provide the different flags as options that are compatible with the history command.

How Linux History Command works?

In the Linux environment, history is not a command. The end-user is considering this is a command. But with the help of built-in keyword, we are able to fetch out the older command with the help of history shell file. All the history command is saved in the “.bash_history” file. In the same file, we are able to fetch all the historical commands. When any user will log in in the shell terminal. By default, the “.bash_history” file will create and store all the Linux commands in the “.bash_history” file. We can also edit and do the modification in the same file.

While working in the shell terminal, we are triggering the multiple commands. All the

triggered commands (consider correct execution or non-execution command) will store in the “.bash_history” file. If we need to perform some operation on history command. In backed, the “.bash_history” file is only in use.

Below are the lists of options available in the History Command:

Sr. No File Name Description
1 -c It will clear the history list. It will delete all the entries of the “.bash_history” file.
2 -d It will delete the history entry at the position offset.
3 -a It will help to append the new history lines to the “.bash_history” file.
4 -n It will read the history lines but not already read from the “.bash_history” file into the current history list.
5 -r It is helping to read the contents of the “.bash_history” file and use it as the current history of the terminal.
6 -w It will write the current history to the “.bash_history” file and help to overwrite the current history file contents.
7 -s It will store the arguments in the “.bash_history” file as a single entry.

Examples of Linux History Command

Given below are the examples of Linux History Command:

Example #1

History Command

It is very simple and common History command in a Linux environment. It will print all the history command of the working terminal.

Popular Course in this category
Kali Linux Training (3 Courses, 3+ Projects)3 Online Courses | 3 Hands-on Projects | 21+ Hours | Verifiable Certificate of Completion | Lifetime Access
4.5 (5,486 ratings)
Course Price

View Course

Related Courses
Linux Training Program (16 Courses, 3+ Projects)Red Hat Linux Training Program (4 Courses, 1+ Projects)

Code:

history

Explanation:

  • As per the above command, we are able to print all the history command in the terminal window. We are having a total 1002 command in history.

Output:

Linux History Command 1

Example #2

History Command – Print Limited Command

In the history command, we are able to print the number of commands that are executed recently.

Code:

history 10

Explanation:

  • As per the above command, we are able to print the history command but only the last 10 commands executed in the shell window.

Output:

Print Limited

Example #3

History Command – Execute the Command with Event Number

In history command, we are having the functionality to execute the history command with a command event number. We do not need to write the complete command.

Code:

!20

Explanation:

  • In history command, we are getting the information of command as we all as the event id associated with the command. If I need to execute the “ip add sh” command without typing it. I can do that, with the help of event id associated with it i.e. “20”.

Output:

with Event Number

Linux History Command 4

Example #4

History Command – Only Print the Command with Event ID

In the history command, we are only able to print the command before execution. To do that, we need to use the “p” option with the history command event id.

Code:

!20:p

Explanation:

  • If we need to print only the command without execution of it. We need to use the “p” option with it. So it will not execute the command but only print the command.

Output:

with Event ID

Example #5

History Command -With Grep command

In history command, we are having the functionality to use the history command with the combination of different commands like grep, tail, less, more, etc.

Code:

history | grep -i "which"

Explanation:

  • In the history command, we are able to find the specific command from the number of history command. We are finding “which” keyword from the entire history command.

Output:

With Grep

Example #6

History command – get most resent command

In history command, we are able to get and executed the most recent command on the terminal.

Code:

!!

Explanation:

  •  As per the above command, we are able to execute the most recent command.

Output:

get most resent

Example #7

History Command – With “-d” Option

In the History command, we can delete the specific history command with the help of associated event id.

Code:

history -d 1012

Explanation:

  • As per the above command, we are deleting the specific history command record from the history file. We are deleting the “1012” event id command.

Output:

Linux History Command 8

Linux history command

Example #8

History Command – With “-c” Option

We are able to delete all the historical command of the current login terminal.

Code:

history -c

Explanation:

  • We can delete all the history command in a single shot. We need to use the “-c” option with the history command.

Output:

Linux History Command 10

Linux History Command 11

Conclusion

We have seen the uncut concept of “Linux History command” with the proper example, explanation and command with different outputs. The history command is useful to find all the historical command was executed in the terminal window. The old or history command is store in the “.bash_history” file. We are having the functionality to use the different options with the history command.

Recommended Articles

This is a guide to Linux History Command. Here we discuss how linux history command works along with examples respectively. You may also have a look at the following articles to learn more –

  1. Linux Users
  2. Linux comm
  3. Linux mdadm
  4. Linux rm Command

All in One Software Development Bundle (600+ Courses, 50+ projects)

600+ Online Courses

50+ projects

3000+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
Linux Tutorial
  • Linux Commands
    • CAT Command in Linux
    • PS command in Linux
    • Mkdir Command in Linux
    • rmdir command in Linux
    • mv command in Linux
    • Linux rm Command
    • CHMOD Command in Linux
    • Zip Command in Linux
    • Linux Route Command
    • SCP Command in Linux
    • du Command in Linux
    • Less Command in Linux
    • Copy Command in Linux
    • Head Command in Linux
    • Help Command in Linux
    • Linux History Command
    • hostname Command in Linux
    • GREP Command in Linux
    • Linux Curl Command
    • Linux List Users
    • Linux List Directories
    • Linux Directory Permissions
    • Find Directory Linux
    • Linux User Permission
    • Linux Add User
    • Linux Delete User
    • Linux uniq
    • Linux Filter Commands
    • Linux sort Command
    • Linux Sort by Size
    • Remove dir Linux
    • Linux tr Command
    • Linux Touch Command
    • Linux More Command
    • Linux Cal Command
    • Linux OD
    • Linux Zcat
    • Linux tracepath
    • Linux Sleep
    • Linux bg Command
    • Linux WC
    • Linux Read
    • Linux Write
    • Linux Rename Command
    • Linux Free Command
    • Linux tac
    • Linux Shred
    • Linux wget
    • Linux Date
    • Linux Untar
    • Linux JQ
    • Linux Xargs
    • Linux Mount Command
    • Linux mdadm
    • Linux comm
    • Linux tee Command
    • Tail Command in Linux
    • Linux gzip
    • Linux Directory Structure
    • Linux Alias Command
    • Linux Inode
    • Linux Ping
    • Linux Watch Command
    • Linux Split Command
    • Linux Size
    • Linux Network Stack
    • Linux Diff Command
    • Vim Command in Linux
    • Linux nslookup
  • Basic
    • Introduction to Linux
    • What is Linux
    • Careers in Linux
    • How to Install Linux
    • How to Install Ubuntu
    • How to Install Ubuntu Server
    • How To Install Debian
    • Advantage Of Linux
    • What is Raid in Linux
    • What is Raid 5?
    • Raid 5 vs Raid 6
    • Careers in Linux Administration
    • What is Shell in Linux?
    • Bash Shell in Linux
    • Linux Emulator
    • Linux Operators
    • Linux Apps
    • Linux Keyboard Shortcuts
    • Linux Crontab
    • What is Raid Storage
    • What is Vmware
    • What is RedHat
    • What is Debian
    • Uses of Ubuntu
    • Career In RedHat
    • Linux Distributions
    • Linux From Scratch
    • Linux Format
    • Cheat Sheet Linux
  • Advanced
    • Install VMware
    • Install GRUB
    • Chown Command in Linux
    • Linux Network Command
    • Configuring Linux Network Environment
    • DNS Configuration in Linux
    • Linux Users
    • Create User in Linux
    • DHCP Server in Linux
    • FTP Server in Linux
    • Linux Regular Expression
    • IP Command in Linux
    • Set IP Address in Linux
    • Tar Command in Linux
    • Dynamic Host Configuration Protocol
    • Linux Kill Signals
    • Linux File Permissions
    • Linux File System
    • Linux Create File
    • Linux Concatenate Files
    • Linux Join
    • Linux Process Management
    • Linux Proxy Server
    • Linux System Commands
    • Linux System Logging
    • NFS in Linux
    • Linux LVM
    • Linux Repository
    • Samba Server in Linux
    • Top Commands in Linux
    • Types of Shells in Linux
    • Shell Script Types For Linux
    • GCC Command in Linux
    • Monolithic Kernel
    • YARN Package Manager
    • Linux Sed Replace
    • Linux List Groups
    • Linux Container
    • Linux Find File by Name
    • Create Folder in Linux
    • Linux Move Folder
    • Linux Error Codes
    • Linux Automount
    • Linux HugePages
  • Kali Linux
    • Install Kali Linux
    • Kali Linux Uses
    • Kali Linux Commands
    • Kali Linux Repository
    • Kali Linux Packages
    • Kali Linux root password
    • Kali Linux Alternative
  • Interview Questions
    • Linux Interview Questions
    • Linux System Administration Interview
    • Red Hat Interview Questions

Related Courses

Kali Linux Training

Linux Training Course

Red Hat Training Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - Kali Linux Training Learn More