How to Use History Command in Linux
Learn and master Linux's history command to track and manage your terminal activities.
The history
Linux command is a handy tool that displays a list of all the commands that a user has previously executed in the terminal. This list is stored in a history file, and by using the history command, you can view this file, making it easier to recall and reuse past commands.
This command is commonly used to track actions performed in the terminal, especially when troubleshooting or repeating complex command sequences. It can save time and effort by allowing users to quickly recall and rerun previous commands without having to type them out again. Some users also utilize the history to audit or review actions taken in the system, aiding in system administration and security.
General syntax for history
command:
history [OPTIONS...]
1. Show used commands
history [number]
This command displays a limited number of previously executed commands.
Example:
The history 5
command in Linux will display the last 5 commands that were executed in the terminal. Here’s a sample output:
501 git status 502 cd /var/www/html 503 ls -la 504 nano myfile.txt 505 history 5
Each line shows a command from the history, preceded by a unique number that represents the command’s position in the history file. In this example, the last five commands that were run are shown, including the history 5
command itself.
2. Delete commands from history list
history -d [number]
Use the -d
option to delete commands from the history list.
Example:
Before deleting:
1 ls 2 cd Documents/ 3 touch file.txt 4 nano file.txt 5 rm file.txt 6 history
After running history -d 5
:
1 ls 2 cd Documents/ 3 touch file.txt 4 nano file.txt 5 history
As you can see, the 5th command (rm file.txt
) has been removed from the history.
3. Search used command within history
history | grep [text]
The history | grep
command helps you find commands that match a text pattern.
Example:
The history | grep cat
command in Linux will search through your command history and display all the lines that contain the word “cat.” The grep command filters the output of the history command to only show lines with the specified pattern, in this case, “cat.” Here’s a sample output:
42 cat file.txt 73 cat /etc/passwd 100 cat documents/note.txt | more 123 concatenate file1.txt file2.txt > combined.txt 150 cat logs/error.log | grep "ERROR"
In this example, each line begins with a number representing the command’s position in the history, followed by the command itself. It includes every instance where “cat” appears, whether it’s the cat
command itself or just part of another word or command.
4. Clearing the history list
history -c
The -c (clear)
option is used to clear all commands from the history list.
5. Execute the command with event number
!number
This command allows you to execute commands with an event number from the history list. There is no need to write a complete command.
Example:
Suppose the 10th command in your history was ls -l
, which lists files in the current directory in long format. If you run !10
, the output might look something like this:
total 24 drwxr-xr-x 5 user user 4096 Aug 6 10:00 Documents drwxr-xr-x 2 user user 4096 Aug 6 09:45 Downloads drwxr-xr-x 2 user user 4096 Aug 6 09:45 Music drwxr-xr-x 2 user user 4096 Aug 6 09:45 Pictures
This output shows the contents of the current directory, just as if you had run the ls -l
command directly.
6. Write changes to bash_history file
history -w [filename]
Use this option to write all changes made in the current session to the bash_history
file.
Example:
The history -w file.txt
command in Linux doesn’t produce output in the terminal itself. Instead, it writes the current history of commands to a file named file.txt
.
So, if you were to open file.txt
, you might see something like this:
1 cd /home/user/documents 2 ls -la 3 sudo apt-get update 4 nano file.txt 5 history -w file.txt
This file now contains the list of commands that were previously executed in the terminal, and you can view or edit it using any text editor.
More Linux commands:
Directory Operations | rmdir · cd · pwd · exa · ls |
File Operations | cat · cp · dd · less · touch · ln · rename · more · head |
File System Operations | chown · mkfs · locate |
Networking | ping · curl · wget · iptables · mtr |
Search and Text Processing | find · grep · sed · whatis · ripgrep · fd · tldr |
System Information and Management | env · history · top · who · htop · glances · lsof |
User and Session Management | screen · su · sudo · open |