hosting image
Linux-History-Command

Linux history Command

The `history` command in Linux is a useful tool that shows a list of previously executed terminal commands. With this command, users can rerun any command from the list without typing it out again.

In this guide, we’ll explore how the `history` command functions and the various ways to use it.

Prerequisites

– A Linux-based system (this guide uses Ubuntu 22.04).

– A user account with root or sudo access.

– Terminal access.

Syntax of the `History` Command

The general syntax for using the `history` command is:

history [options]

While it can be run without any additional options, these options offer more useful features.

Options for the `history` Command

The `history` command supports multiple options that modify its behaviour. Below is a table of commonly used options with `history`:

OptionDescription
-cClears all the entries from the history list.
-d offsetDeletes a specific command from the history based on its position number in the list.
-aAdds new commands from the current session to the history file.
-nLoads all unread history lines from the history file into the current session.
-rReads the history file and adds its content to the existing history list.
-wSaves the current history list to the history file.
-pExpands the provided arguments and shows the result without saving it to the history list. This lets you see how the command would look after expansion without adding it to history.
-sSaves the provided arguments as a single entry in the history list.

Examples of Using the `History` Command

The `history` command helps users review and handle commands previously run in the terminal, simplifying the process of reusing past commands. Below are some examples of how to utilize `history`.

Display the Command History

Simply running the `history` command on its own will show a list of all commands entered since the beginning of the current terminal session:

See also  What is Debian OS?

history

Linux-History-Command

Limit the Number of Displayed Entries

You can limit the number of commands shown in the history by specifying a number after the `history` command. For example, to see just the last five commands, run:

history 5

Linux-History-Command

Search Through Command History

To search for a specific command within your history, you can combine `history` with the `grep` command. For instance, to search for instances of the `ls` command, use:

history | grep "ls"

Linux-History-Command

Add Date and Time Stamps to History

The `.bashrc` file contains configuration settings for the Bash shell, and by modifying it, you can change how the `history` command displays its output.

To adjust the settings, open the `.bashrc` file using a text editor like Nano:

sudo nano .bashrc

To include dates and timestamps in the history output, add the following line to the file:

export HISTTIMEFORMAT="%c "

Note: The space before the closing quotation mark ensures the timestamp is separated from the command name, improving readability.

You can customize the format of the timestamp by using different options with `HISTTIMEFORMAT`, such as:

- `%d`: Day.

- `%m`: Month.

- `%y`: Year.

- `%H`: Hour.

- `%M`: Minutes.

- `%S`: Seconds.

- `%F`: Full date in Y-M-D format.

- `%T`: Time in H:M:S format.

- `%c`: Full date and time (Day-D-M-Y H:M:S).

After saving the changes to `.bashrc`, restart the terminal and run the `history` command to see the updated format:

history

Linux-History-Command

Check the History Buffer Size

The `.bashrc` file includes two key settings that control the size of the command history buffer:

– `HISTSIZE`: Limits the number of commands stored in the history list.

– `HISTFILESIZE`: Sets the maximum number of entries saved in the `.bash_history` file, where your command history is kept.

By modifying these values, you can adjust how many commands the Bash shell keeps and displays. For example, setting `HISTSIZE` to 10 will limit the visible command history to the 10 most recent entries.

See also  Advanced Docker Commands / Getting to know important Docker commands

Linux-History-Command

To change the buffer size, edit these values in `.bashrc`, save the file, and restart the terminal. Then run the `history` command to verify the changes:

history

Linux-History-Command

Reusing a Previous Command

The `history` command shows a list of commands you’ve run in the past. You can easily re-execute a command by typing an exclamation mark (`!`) followed by the command’s number in the list. For example, to rerun the third command, you would use:

!3

Linux-History-Command

To execute a specific command from the end of the list, place a dash (`-`) before the command number. For instance, to repeat the seventh last command, enter:

!-7

Linux-History-Command

If you want to quickly repeat the most recent command, simply type:

!!

Linux-History-Command

Search for a Command by String

You can rerun the most recent command that begins with a specific string by typing an exclamation mark (`!`) followed by the string. For instance, to repeat the last command starting with `sudo`, you would type:

!sudo

Linux-History-Command

However, this can sometimes run an unintended command, especially with `sudo`. To avoid this, add `:p` after the string to display the command without executing it. This lets you check the command before deciding to run it:

!sudo:p

Linux-History-Command

If you need to find a command that contains a string but doesn’t necessarily start with it, use a question mark (`?`) after the exclamation mark. For example, to find the most recent command that includes `ls`, use:

!?ls

Linux-History-Command

In this case, the shell will reuse the last command that has `ls` anywhere in it, even if the command starts with something else like `sudo`.

Correct a Previously Executed Command

If you’ve made a mistake in a previous command, you can quickly correct it using this syntax:

^[incorrect text]^[correct text]^

For example, if you accidentally typed `sudo atp update` instead of `apt`:

sudo atp update

Linux-History-Command

You can fix the typo by replacing `atp` with `apt` like this:

See also  What Is A Virtual Private Server? (VPS)

^atp^apt^

Linux-History-Command

Disable Command History Recording

If you want to temporarily stop commands from being saved to the history, you can turn off history recording by using the following command:

set +o history

To start recording commands again, use:

set -o history

These commands will not produce any output.

Deleting Commands from History

To remove a specific command from the history, use the `-d` option with the `history` command. For example, to delete the command at position 34 (which you can identify using `history`), you would run:

history -d 34

Linux-History-Command

This action has no output, but you can verify by listing recent history, such as the last 10 entries:

history 10

Linux-History-Command

If you want to clear the entire history list, use the `-c` option:

history -c

Manually Update the History File

In Bash, the command history is automatically saved when you close the terminal. However, you can manually save the history while still in a session.

To append the current session’s commands to the `.bash_history` file, use the `-a` option:

history -a

This command runs without displaying any output. Alternatively, you can save the entire history list to the `.bash_history` file by using the `-w` option:

history -w

Like the previous command, this one also does not produce any output.

Conclusion

the `history` command in Linux is a highly versatile tool that allows users to efficiently manage and reuse previously executed commands, saving both time and effort in terminal operations. By understanding the various options and features it provides, users can customize their command history experience, from searching and re-executing commands to managing the size of the history buffer or even disabling it when necessary. Whether used for basic history viewing or advanced command manipulation, the `history` command is essential for effective terminal workflow in Linux.

5/5 - (1 vote)
Wilivm: VPS Hosting Cheap Company

Choose your service according to your needs

OUR BEST PRICING

RDP Admin

Starting at

$19.99

Linux VPS

Starting at

$13.99

Windows VPS

Show Plans

$15.99

Mikrotik VPS

Starting at

$13.99

RDP Admin

Starting at

$239

Linux VPS

Starting at

$167

Windows VPS

Starting at

$191

Mikrotik VPS

Starting at

$167

Advantages Of Wilivm Services

99.9% Uptime

We guarantee 99% uptime for your website and apps. Our expert technical team monitors our servers 24/7 for stability, security, and performance. We promise to provide the materials you need when you need them to keep your company functioning successfully.

Unlimited Bandwidth

Wilivm VPS clients may transmit data without worrying about exceeding their monthly bandwidth restriction. Regardless of traffic, our dependable and efficient servers keep your website operating smoothly. Wilivm VPS ensures that your website is constantly accessible to visitors worldwide.

Multiple Locations

We are proud to offer our customers a wide range of Wilivm VPS options, all ready to use from multiple locations in datacenters around the world. Our servers provide high-performance and reliable hosting solutions designed for businesses of any size.

Instant Activation

Our Instant Delivery VPS services are ideal for fast, dependable VPS hosting. Your apps will operate quicker than ever on our high-performance, low-latency servers. They're pre-configured, so you can start using them without technical knowledge or setup time. Your company may start immediately with our Instant Delivery VPS services!

Full Support

Full Support We provide 24/7 VPS support. During your service, our knowledgeable staff will assist you with technical concerns. Our customer care staff and extensive knowledge base are available to answer any service questions. With us, your hosting requirements will always be met efficiently!

Admin Access

Admin access to a fully managed VPS lets you manage a server without fuss. Our server management professionals let you concentrate on operating your company. We handle setup, configuration, maintenance, and security updates. Our completely managed service keeps your website or application up-to-date, safe, trustworthy, and quick, so you can focus on developing your company!

Latest Wilivm articles

How Remove A Directory In Linux ?
Yaseen Quinn

How Remove A Directory In Linux ?

Linux is a robust and adaptable operating system that is used by developers, system administrators, and enthusiasts all over the world. Understanding how to manage

Setup Environment Variables in Linux
Yaseen Quinn

Setup Environment Variables in Linux

Setting up environment variables in Linux is a skill that every developer and system administrator should have. Environment variables are dynamic values that can influence

Setup Your Server
Setup Your Server