Linux Comm History
π
2026-06-22 | π Linux
Linux history Command | Rookie Tutorial
[ Linux Command Manual](#)
The history command is an extremely useful built-in command in Linux systems that records all command history executed by the user in the terminal. This feature is particularly valuable for the following scenarios:
1. **Tracing Operations**: View previously executed commands
2. **Quick Reuse**: No need to retype long commands
3. **Troubleshooting**: Check system operation records
4. **Efficiency Improvement**: Quickly complete repetitive tasks using command history
When you enter commands in the terminal, Bash saves these commands in memory and writes them to the `~/.bash_history` file upon exit (under default configuration).
* * *
## Basic Syntax and Usage
The basic syntax of the history command is very simple:
history
### Common Forms
1. View complete history:
history
2. View the last N entries:
history 10 # Display the last 10 commands
3. Clear all history:
history -c
4. Delete a specific history entry (e.g., delete entry 1010):
history -d 1010
* * *
## Detailed Explanation of Common Options and Parameters
The history command supports several practical options:
| Option | Description | Example |
| --- | --- | --- |
| `-c` | Clear all history records | `history -c` |
| `-d` | Delete the history record at the specified position | `history -d 1005` |
| `-a` | Immediately write the history from memory to the history file | `history -a` |
| `-n` | Read unread history records from the history file | `history -n` |
| `-r` | Read the contents of the history file into the current session | `history -r` |
| `-w` | Write the current history to the history file | `history -w` |
* * *
## Practical Tips and Advanced Usage
### 1. Quickly Execute History Commands
## Examples
!n # Execute the nth command in the history record
!!# Execute the previous command
!string # Execute the most recent command starting with 'string'
Example:
## Examples
!1024# Execute command number 1024 from the history
!!# Re-execute the previous command
!vim# Execute the most recent command starting with 'vim'
### 2. Search History Commands
Use `Ctrl+R` to perform a reverse search through history commands; entering part of a keyword will find matching commands.
### 3. History Command Substitution
## Examples
^old^new # Execute the previous command after replacing 'old' with 'new'
Example:
## Examples
$ cat file1.txt
$ ^file1^file2 # Equivalent to executing cat file2.txt
### 4. Display Command Timestamp
Add the following configuration to `~/.bashrc` to display command execution time:
## Examples
export HISTTIMEFORMAT="%F %T "
Then execute:
## Examples
source ~/.bashrc
After this, the `history` command will show the execution time for each command.
* * *
## Environment Variable Configuration
You can customize the behavior of the history command through environment variables:
| Variable | Description | Recommended Value |
| --- | --- | --- |
| `HISTSIZE` | Number of history commands saved in memory | `5000` |
| `HISTFILESIZE` | Number of commands saved in the history file | `10000` |
| `HISTCONTROL` | Controls how history is recorded | `ignoredups:erasedups` |
| `HISTIGNORE` | Specifies commands not to be recorded | `"ls:cd:pwd:exit"` |
Configuration example (add to `~/.bashrc`):
## Examples
export HISTSIZE=5000
export HISTFILESIZE=10000
export HISTCONTROL=ignoredups:erasedups
export HISTIGNORE="ls:cd:pwd:exit"
export HISTTIMEFORMAT="%F %T "
* * *
## Practical Application Scenarios
### Scenario 1: Retrieve a Forgotten Command
## Examples
history|grep"apt install"
### Scenario 2: Count Most Frequently Used Commands
## Examples
history|awk'{CMD[$2]++;count++;} END {for (a in CMD)print CMD " " CMD/count*100 "% " a;}'|grep-v"./"| column -c3-s" "-t|sort-nr|nl|head-n10
### Scenario 3: Backup History
## Examples
history-a# Ensure the latest commands are written to the file
cp ~/.bash_history ~/command_history_backup_$(date +%F).txt
* * *
## Important Notes
1. **Privacy and Security**: History may contain sensitive information (like passwords), handle with care
2. **Multiple Terminal Issue**: Different terminal sessions do not share history in real-time by default
3. **History Loss**: Abnormal exit may cause commands not to be saved
4. **Large File Handling**: Excessively large history files may impact performance
* * *
## Summary and Exercises
1. View your command history and find the last 5 git commands used
history | grep git | tail -5
2. Configure your bash environment to include timestamps in history and ignore duplicate commands
3. Create an alias to quickly backup the current history to a specified directory
alias backup_history='cp ~/.bash_history ~/history_backups/history_$(date +%Y%m%d_%H%M%S).txt'
4. Try using `Ctrl+R` to search for a previously used complex command
By mastering the history command, you can significantly improve your efficiency in the Linux terminal, reduce repetitive typing, and better manage your command-line operation history.
[ Linux Command Manual](#)