If you've spent any time on the Unix command line, chances are you've come across the history
command. It's that nifty tool that remembers all the commands you've typed before. But, like many things in Unix, there's more beneath the surface than meets the eye. While you might know the basics, I'm betting there's more for you to discover. This guide is designed to walk you through the nuances of the history
command. By the end, not only will you understand it better, but you'll also pick up practical tips to streamline your tasks. So, even if you've used history
a hundred times, stick around — you might just learn something new.
Why Use the history
Command?
The history
command in Unix serves as a recorder of your command-line activities. It's not just about listing old commands, but optimizing repetitive tasks, error corrections, and more. Let's deep dive into its utility.
Getting Started: Basic Commands
Initiating the history
command is simple:
- View Commands: Input
history
to display a list of previously executed commands. - Navigate Backward:Use the Up Arrow or
CTRL + P
to navigate to previous (older) commands - Navigate forward: Use the Down Arrow or
CTRL + N
to navigate to next (newer) commands. - Rerun Last Command: The
!!
(double exclamation mark) command lets you effortlessly redo your last action. - Use Specific Command Number: If you see a command in your history list numbered
123
, you can rerun it by typing!123
. - Clearing History: Keep your history concise by periodically using
history -c
.
Tips for Enhanced Usage
Here below some of my favorite history
features:
- Reverse-Search for commands: Press
CTRL + R
and start typing a portion of your command to search in previous commands. If you hitCTRL + R
again, it will show you older commands. - Forward-Search: After you enter the Reverse-Search, you can search for newer commands by pressing
CTRL + S
. So by usingCTRL + R
andCTRL + S
you will search back and forth in your history. - Preview Commands:
!123:p
displays the 123rd command without immediately executing it. - Tagging: Use comments starting with
#
to tag specific commands. This is a handy technique to annotate certain commands. It can be invaluable, especially when you revisit your command history and wonder why a particular command was executed, or to search for commands that are hard to remember.
For example here we are tagging our tar
command with a # BACKUP
tag:
tar -czvf backup.tar.gz /path/to/directory # BACKUP
You can then search for this command with:
history | grep "# BACKUP"
Or with the reverse-search as seen before by pressing CTRL + R
and start typing your tag # BACKUP
:
(reverse-i-search)`# BACKUP': tar -czvf backup.tar.gz /path/to/directory # BACKUP
Tweaks and Settings for Your Command History
Here are some of the settings that you can apply to personalize your history
behavior.
History Size: To control the number of stored commands, add these two lines to your, .bashrc
or .zshrc
file:
export HISTFILESIZE=5000
export HISTSIZE=5000
If you want to better understand the difference between the two modifiers, I suggest to read the following Stack Overflow answer:
Excluding Specific Commands: HISTIGNORE
for Bash or HISTORY_IGNORE
for Zsh, are useful for preventing frequent or sensitive commands from being stored in the history. Just specify a list of patterns to be excluded from the command history.
For Bash: add the following line to your .bashrc
file:
export HISTIGNORE="ls:clear:exit"
For Zsh: add this line to your .zshrc
file:
export HISTORY_IGNORE="ls|clear|exit"
Please note the different separators: a colon:
for theHISTIGNORE
and a pipe|
for theHISTORY_IGNORE
.
Excluding Commands that start with a space: As an alternative to the HISTIGNORE
/ HISTORY_IGNORE
above, you can use the HISTCONTROL
for Bash or HIST_IGNORE_SPACE
for Zsh, so that every time you add a space at the beginning of a command, it will not go to the history.
For Bash: add this line to your .bashrc
:
export HISTCONTROL=ignorespace
For Zsh: add this line to your .zshrc
file:
setopt HIST_IGNORE_SPACE
Exclude Duplicate commands: You can avoid saving a command to the history if it's the same as the last command you entered. In simpler terms, it prevents duplicate entries from appearing one after the other in your command history.
For Bash: add this line to your .bashrc
file:
export HISTCONTROL=ignoredups
For Zsh: add this line to your .zshrc
file:
setopt HIST_IGNORE_DUPS
Note that in Bash, ignoreboth
is used when we want to use both ignorespace
and ignoredups
so, the following line
export HISTCONTROL=ignoreboth
is the same as:
export HISTCONTROL=ignorespace:ignoredups
Add Timestamps to your history: If you want to store timestamps with commands use the HISTTIMEFORMAT
and HIST_STAMPS
modifiers for Bash and Zsh, respectively.
For Bash: add this line in your .bashrc
file:
export HISTTIMEFORMAT="%F %T: "
For Zsh: add this line to your .zshrc
file:
HIST_STAMPS="%F %T: "
Change history file location: The default history file location is:
.bash_history
for Bash shells.zsh_history
for Zsh shells- if it's not either of these above, you can check yours with
echo $HISTFILE
If you want to change the defaults, just add the following line to your .bashrc
or .zshrc
file so that your history will be written to a different file. In our example here below we are changing it to a file named .my_custom_history
inside your home directory:
export HISTFILE=~/.my_custom_history
Apply your settings immediately: After making changes to your .bashrc
or .zshrc
, you'll need to source
the file for the changes to take effect in your current shell session. This allows you to immediately see and use any changes you've made without having to close and reopen your terminal or start a new shell session.
Here's how you can do it:
For .bashrc
:
source ~/.bashrc
or simply:
. ~/.bashrc
For .zshrc
:
source ~/.zshrc
or:
. ~/.zshrc
Conclusion
The history
command in Unix is more than just a recap of what you've done—it's a handy feature that can boost your productivity on the command line. With the tips from this guide, you're better equipped to use history
to its fullest. Give it a try, and see how it simplifies your Unix tasks.