The Wayback Machine - https://web.archive.org/web/20241001044133/https://www.geeksforgeeks.org/history-command-in-linux-with-examples/
Open In App

How to Display Command History in Linux | history Command

Last Updated : 19 Jan, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The command-line interface in Linux provides powerful tools for users, and mastering command history is essential for efficient navigation and retrieval of previously executed commands. The history command is a valuable utility that allows users to view and search through their command history. In this comprehensive guide, we will explore the various features of the history command, enabling users to streamline their workflow and save time by efficiently accessing and reusing commands.

Understanding the history Command:

The history command in Linux provides a chronological list of previously executed commands, along with corresponding command numbers. This feature allows users to recall, reuse, and modify commands without having to retype them. The command history is stored in a file, typically ~/.bash_history for the Bash shell.

How to Display Command history in Linux

To view the command history, simply type:

history 

Here, the number(termed as event number) preceded before each command depends on the system. You may get different numbers while executing on your own system.

Display a Limited Number of Commands History

To show the limited number of commands that executed previously as follows:

history 5 

This command displays the last 5 commands from the command history.

Execute a Command by Event Number

Execute a command using its event number with the ! symbol. For instance:

!1997 

This will rerun the command with event number 1997.

To print a command before executing it to avoid errors, use the :p option after the event number. Example:

!1997:p

This will display the command associated with event number 1997 without executing it.

Search Command History with grep

Combine the history command with grep for efficient searching. Example:

Example:

history | grep chpasswd 

This command filters and displays only the commands containing the term “chpasswd.”

View the Most Recent Command History

To quickly view and rerun the most recent command, use:

!!

This will rerun the last executed command.

Execute a Command without Storing History

If a command needs to be executed without being stored in history, unset the HISTFILE variable. Example:

This ensures that the command won’t be stored in the history file.

Execute a Command using a Part of the Command History

Execute a command using a part of its string. Example

!command_starting_string

This will execute the latest command starting with “command_starting_string.”

Remove a Specific Command from History

Remove a specific command from history using the history -d option. Example:

history -d 1996 

This command removes the command with event number 1996 from history.

Clear Entire Command History

Clear the entire command history using the history -c option. Example

history -c

Be cautious, as this action irreversibly removes all commands from the history.

View the Last 10 Commands History

Use history | tail to view the last 10 commands from the history:

history | tail

This command efficiently displays the most recent commands in the terminal.

Conclusion

In this article we discussed the history command which is crucial for efficient command-line navigation in Linux. This guide has explored various features, allowing users to view, search, and manipulate command history. From displaying a limited number of commands, executing by event number, and searching with grep, to advanced functionalities like executing without storing, removing specific commands, and clearing the entire history, users can enhance their command-line efficiency. This skill empowers users to save time, avoid errors, and confidently navigate the Linux command line.



Previous Article
Next Article

Similar Reads

How to Display the current Username in Linux | whoami Command
Imagine you're working on your computer and forget who you're logged in as. In Linux, there's a special trick called "whoami" that's like asking "Hey computer, who am I right now?" This article explains how this simple command works and helps you remember who's in charge! Don't worry, it won't be full of confusing tech words. We'll keep it fun and
4 min read
How to Display Path of an Executable File in Linux | Which Command
In Linux finding the exact path of an excutable file can be crucial for the system adminstration, scripting and as well for troubleshooting. The `which` command helps with providing a simple and effective way to locate the executable files within the directories that are listed in your system. In this article, we are going to discuss on what is whi
6 min read
How to Display and Set Date and Time in Linux | date Command
date command is used to display the system date and time. date command is also used to set date and time of the system. By default the date command displays the date in the time zone on which unix/linux operating system is configured.You must be the super-user (root) to change the date and time.  Syntax of Date Command in Linux date [OPTION]... [+F
6 min read
How to Display Current Working Directory in Linux | pwd Command
The 'pwd,' which stands for "print working directory." In this article, we will delve into the 'pwd' command, exploring its functionality, usage, and various examples. It prints the path of the working directory, starting from the root. pwd is shell built-in command(pwd) or an actual binary(/bin/pwd). $PWD is an environment variable that stores the
4 min read
Ccat – Colorize Cat Command Output command in Linux with Examples
ccat is a command-line tool for Linux and OSX, which is similar to the cat command in Linux. But the difference between cat and ccat is that the ccat shows the content of the file with the syntax highlighted. Currently, ccat supports the following programming languages. JavaScriptJavaRubyPythonGoCJSONInstallation of Ccat First, we are going to see
2 min read
Shell Script to Display the Exit Status Using Grep Command
Linux provides users a great cool feature of the command-line tool along with a graphical user interface where they can perform tasks via ruining command. All of this command returns a status according to their execution. Its execution value can be used for showing errors or take some other action in a shell script. Exit status is following accordi
4 min read
How to Display and Set Hostname in Linux?
Hostname is a unique name for a computer and computer network node in a network. It is also known as a Nodename, computer name or Sitename where it is a label that is assigned to a device that is connected to the computer network. Hostname has simple names consisting of words and phrases or they may be well-structured. Example: In the URL www.geeks
3 min read
Smem Tool To Display Memory Usage More Accurate in Linux
Multiple tools are available to monitor the memory usage on a Linux system. Memory management by monitoring memory usage is one of the important things to do on our Linux system, Smem is one of the memory usage monitoring tools available in Linux. Smem displays the memory usage of the processes on the command line, smem also displays a Graphical Ou
3 min read
How to Display Line Number in vim editor in Linux
In this article, we will cover how to make the Vim editor display or hide line numbers. First, we look at what Linux and VIM editors and why we use them, are its features and how can we use them Follow the basic VIM editor guide to get familiar with the editor followed by various options to make the vim editor display or hide line numbers and how w
5 min read
How to display nc return value in Linux shell script?
When you need to monitor network connections and understand their status, creating a custom bash script can be an invaluable solution. This article demonstrates how to build a bash script that checks network connections using the nc command and displays the corresponding exit status. By providing a user-friendly menu-driven interface, this script e
6 min read
Display File System Type in Linux
Understanding the file system type on a Linux system is crucial for efficient management and troubleshooting. Different file systems serve various purposes, and identifying them provides insights into storage devices and their capabilities. This article explores various methods to display the file system type in Linux, providing detailed explanatio
4 min read
How to Display Random ASCII Art on Linux Terminal?
Here we are going to see how to print the drawing on the terminal in the form of ASCII characters. ASCII-Art-Splash-Screen is a tool written in python that pulls a random drawing from a repository and shows it every time you open the terminal. This works on UNIX-based systems like Linux and macOS.  Now let's see how to install the ASCII-Art-Splash-
2 min read
Setting Date and Time For Each Command in Bash History
Prerequisites: History command in Linux with Examples In bash, all history of commands executed by the user are stored in /home/user/.bash_history. This file is store by default in 1000 commands, but we can increase or decrease it. By using the history command the system administrator can view the commands executed by the respective user. Then hist
2 min read
Linux from the beginning - History and Evolution
In this article, we will discuss the history and evolution of the Linux Operating System. But before jumping into the history of Linux we must pass through UNIX. History of UNIX : It started in 1964 in New Jersey when some people of Bell Labs tried to create a multiuser operating system (OS), they worked on it till 1969, after facing lots of failur
3 min read
Executing Linux Commands Without Storing Them in History
In Linux, a lot of work is done via the command line or terminal. However, sometimes we might run a command that might contain some sensitive information or we just do not want to clutter our history file with unnecessary commands. In such cases, we want to execute Linux commands without keeping them in memory. There are multiple ways to achieve th
6 min read
How to Clear the Terminal History in Linux
In today's digital era, privacy and security are the main aspects that every user should follow. As on the Linux operating system, we as the user are administrators, perform different types of command execution on the terminal. Once the command is been executed, its history is been saved in the file which is hidden from us. So if this file is been
6 min read
atrm command in Linux with examples
atrm command is used to remove the specified jobs. To remove a job, its job number is passed in the command. A user can only delete jobs that belong to him. Only superuser can delete any job even if that belongs to another user. Syntax: atrm [-V] job [job...] Options: -V : Used to print the version number atrm -V job : Job number of the job which i
1 min read
expand Command in LINUX with examples
Whenever you work with files in LINUX there can be a situation when you are stuck with a file containing many tabs and whatever you need to do with a file requires that file with no tabs but with spaces. In this situation, the task looks quite simple if you are dealing with a small file but what if the file you are dealing with is very big or you n
3 min read
rcp Command in Linux with examples
There comes a time while using LINUX when there is a need to copy some information stored in a file to another computer. This can be done simply using rcp command line utility . Obviously there exists some other methods to complete the above mentioned task which are more secure (like scp or rsync) but this command lets you do this in the simple way
4 min read
fmt command in Linux with examples
fmt command in LINUX actually works as a formatter for simplifying and optimizing text files. Formatting of text files can also be done manually, but it can be really time-consuming when it comes to large text files, this is where fmt comes to rescue. fmt re-formats each paragraph in the file specified, writing to standard output. Here's the syntax
4 min read
unexpand command in Linux with Examples
To convert the leading spaces and tabs into tabs, there exists a command line utility called unexpand command. The unexpand command by default convert each spaces into tabs writing the produced output to the standard output. Here's the syntax of unexpand command : Syntax : $unexpand [OPTION]... [FILE]... where, OPTION refers to the options compatib
2 min read
anacron command in linux with examples
anacron command is used to execute commands periodically with a frequency specified in days. Its main advantage over cron is that it can be used on a machine which is not running continuously. In cron if a machine is not running on time of a scheduled job then it will skip it, but anacron is a bit different as it first checks for timestamp of the j
3 min read
SED command in Linux | Set 2
We have discussed some of the SED command options in Sed Command in Linux/Unix with examples SED is used for finding, filtering, text substitution, replacement and text manipulations like insertion, deletion search, etc. It's a one of the powerful utilities offered by Linux/Unix systems. We can use sed with regular expressions. I hope atleast you h
8 min read
enable and disable command in Linux
Enables and disables are the built-in shell commands. enable command is used to start the printers or classes whereas the disable command is used to stop the printers or classes. Syntax For enable Command: enable [-a] [-dnps] [-f filename][name ...] Syntax For disable Command: disable [-c] [-W] [ -r [ reason ] ] printer Options: -a : List all built
1 min read
autoheader command in Linux with Examples
autoheader command in Linux is used to create a template file of C "#define" or any other template header for configure to use. If the user will give autoheader an argument, it reads the standard input instead of reading configure.ac and also writes the header file to the standard output. This command scans the configure.ac file and figures out whi
1 min read
expect command in Linux with Examples
expect command or scripting language works with scripts that expect user inputs. It automates the task by providing inputs. // We can install expect command using following if not installed // On Ubuntu $sudo apt install expect // On Redhat based systems $ yum install expect First we write a script that will be needing inputs from users and then we
2 min read
function command in Linux with examples
The function is a command in Linux that is used to create functions or methods. It is used to perform a specific task or a set of instructions. It allows users to create shortcuts for lengthy tasks making the command-line experience more efficient and convenient. The function can be created in the user's current shell session or saved in the shell
2 min read
fg command in Linux with examples
fg command in linux used to put a background job in foreground. Syntax: fg [job_spec] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this job if n
1 min read
bg command in Linux with Examples
bg command in linux is used to place foreground jobs in background. Syntax: bg [job_spec ...] job_spec may be: %n : Refer to job number n. %str : Refer to a job which was started by a command beginning with str. %?str : Refer to a job which was started by a command containing str. %% or %+ : Refer to the current job. fg and bg will operate on this
1 min read
Clear Command in Linux with Examples
clear is a standard Unix computer operating system command that is used to clear the terminal screen. This command first looks for a terminal type in the environment and after that, it figures out the terminfo database for how to clear the screen. And this command will ignore any command-line parameters that may be present. Also, the clear command
2 min read
Article Tags :