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`:
Option | Description |
-c | Clears all the entries from the history list. |
-d offset | Deletes a specific command from the history based on its position number in the list. |
-a | Adds new commands from the current session to the history file. |
-n | Loads all unread history lines from the history file into the current session. |
-r | Reads the history file and adds its content to the existing history list. |
-w | Saves the current history list to the history file. |
-p | Expands 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. |
-s | Saves 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:
history
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
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"
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
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.
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
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
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
If you want to quickly repeat the most recent command, simply type:
!!
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
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
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
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
You can fix the typo by replacing `atp` with `apt` like this:
^atp^apt^
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
This action has no output, but you can verify by listing recent history, such as the last 10 entries:
history 10
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.