◦ Comprehensive security
◦ 24/7 support
VPS → Bash History
Bash history: Linux command guide for VPS management
Running a VPS on Linux gives you flexibility and control, but only if you know how to command it. And when you’re deep into server setup, debugging cron jobs, or configuring your firewall rules, being able to track, reuse, or edit previous Bash commands can save hours.
That’s where Bash history comes in. It’s not just a log—it’s a superpower for Linux users. Want to re-run a perfect command from earlier today without typing it all over again? Or trace back something you did yesterday that just broke your config? History has your back.
This guide walks through the essentials of managing your Bash history on a Linux VPS. We’ll cover how to view, search, customize, and even scrub your history. Perfect if you’ve already been working in Linux, but want to get way more efficient.
Get premium VPS hosting
High-performance VPS hosting that delivers unrivaled power
How to view bash history
Using the history command
To get started, open your terminal and type:
historyYou’ll see a numbered list of your previously run commands. Each line represents one command, with its corresponding history number on the left.
This is the simplest way to review what you’ve been doing recently. It’s especially handy when debugging something or retracing steps in a deployment.
Additional viewing options
Want to get more specific? Here are a few powerful ways to dig into your Bash history:
Show the last n commands
To see only the last 20 commands, for example:
history 20This is useful when you’re only interested in your recent activity, like a deployment or system update you just ran.
Search history with grep
This is great for finding a specific command (or type of command) without scrolling endlessly. For example:
history | grep sshPipe to less for scrollable viewing
history | lessThis lets you page through your command history one screen at a time. Especially helpful when looking through a large session.
Scrolling the bash history
Sometimes the simplest way is still best:
- Up arrow / Down arrow: Scroll back and forth through previous commands.
- Ctrl+P / Ctrl+N: Same as up/down arrows. Handy if your terminal doesn’t recognize the arrow keys.
- Meta+P / Meta+N: Scroll by command prefix. Depending on your terminal, Meta is often mapped to Alt or Esc.
How to delete .bash_history
When would you want to delete history?
- You’ve entered sensitive credentials accidentally.
- You want to reset the environment before handing off a VPS.
- You’re cleaning up a shared development machine.
Options for clearing history
Each method serves a slightly different purpose:
Clear the history for this session
history -cThis is useful if you want to start fresh during a current terminal session.
Delete a specific line from history
history -d 203This replaces command 203 with nothing. Ideal for removing just one risky or incorrect entry.
Clear the saved history file
cat /dev/null > ~/.bash_historyThis wipes the .bash_history file. Use it when you’re ready to permanently delete all saved commands between sessions.
.bashrc and configuring bash settings
.bashrc is a hidden config file in your home directory. Bash runs it every time you open a new terminal session. It’s where you can customize your environment—aliases, prompt behavior, and importantly, how Bash handles history.
Set the number of commands that history recalls
By default, Bash saves the last 500 commands, but you can change that.
HISTSIZE=1000
HISTFILESIZE=2000- HISTSIZE: How many commands are remembered in your current session.
- HISTFILESIZE: How many total lines are stored in .bash_history.
This is useful if you run a lot of terminal commands and want a longer audit trail.
Keep all your bash histories
By default, your last terminal session can overwrite earlier ones. To append history (instead of replacing it), add this to .bashrc:
shopt -s histappendThis is essential when working across multiple tabs or SSH sessions.
Add commands to history immediately
Normally, Bash writes history only when a session ends. To write every command as soon as it’s executed:
PROMPT_COMMAND=’history -a’
This is helpful if your VPS crashes mid-session or if you want to see commands live in other sessions.
Exclude certain commands from bash history
You might want to skip storing things like ls or cd.
HISTIGNORE="ls:cd:pwd:exit"Useful for reducing clutter and focusing on commands that actually matter.
Bash history with timestamp
Normally, Bash writes history only when a session ends. To write every command as soon as it’s executed:
HISTTIMEFORMAT="%F %T "This adds YYYY-MM-DD HH:MM:SS before each command when you run history.
Bash history search tips and tricks
Why search your bash history? Because retyping complex commands is a pain. Searching lets you quickly reuse or tweak something you’ve done before—especially during VPS maintenance, config, or scripting.
Searching bash history
Press and hold CTRL+R, then start typing. Bash will auto-complete the most recent match.
- Press Ctrl+R again to keep cycling backward through older matches.
- Press Enter to execute, or use arrow keys to edit before running.
Execute a command from search (without copying it)
!keyword
For example:
!nginx
… instantly re-runs the most recent command that starts with nginx.
Additional bash history search commands
Once you’re comfortable with search, these shortcuts can really speed things up:
CTRL+S: Searches forward through history (reverse of CTRL+R).
Note: In some terminals, this suspends the session. To fix:
stty -ixon- CTRL+Q: Resume after CTRL+S freezes the terminal.
- CTRL+O: Run the current match from history search, but stay in search mode.
- CTRL+G: Exit history search without running a command.
Bash history search mid-command
Already halfway through typing something? Use reverse history in terminal like this:
- Type the first part of the command (e.g. scp).
- Press CTRL+R. Bash will reverse-search for past commands that started with scp.
- Hit enter to execute or edit before running.
This method is super useful when you’re trying to repeat a multi-flag command like rsync or tar.
Bash history expansion commands
History expansions are shortcuts that let you reuse parts of previous commands without typing them all over again. They’re fast, efficient, and especially handy on VPS where you’re scripting or installing tools.
There are two main types: designators (which pull up past commands) and modifiers (which edit those commands on the fly).
Event designators
Used to recall previous commands.
!!: Repeat the last command
!n: Run command number n from your history—for example:
!205
!-n: Run the command n lines before the current one—for example:
!-3
^old^new: Replace old with new in last command—for example:
^dev^prod
!*: Grab all arguments from the last command—for example:
echo foo bar
cp !*# becomes cp foo bar
Word designators
Use these to pull specific parts (words) from the last command.
- !$: Last word
- !^: First word
- !:2: Second word
For example:
tar -xzvf archive.tar.gz
echo !$# runs: echo archive.tar.gz
Modifiers
Used to tweak command parts:
- :h: Remove the last path element (head)
- :t: Keep only the last path element (tail)
- :r: Remove file extension
- :p: Print the command, don’t execute
- :s/old/new/: Replace first match of old with new
For example:
echo /var/www/html/index.html
!:1:r# returns /var/www/html/index
Getting started with bash history
The best place to begin is by using the history command and getting comfortable with scrolling using the arrow keys or Ctrl+R. Once you’re fluent in reviewing what you’ve done, you’ll naturally get better at reusing and modifying past commands.
Next step: Practice using !, !!, ^old^new, and HISTIGNORE. You’ll be amazed how much faster you can get work done on your Linux VPS.
When you’re ready to upgrade your hosting to a high-quality VPS, Liquid Web can help. We’ve been leading the industry for decades, because our VPS servers are fast, cloud-based for easy scalability, and unbeatably reliable. Choose your favorite OS and the management tier that works best for you.
Click below to explore VPS hosting options or start a chat with one of our experts right now to learn more
Additional resources
VPS: A beginner’s guide →
A complete beginner’s guide to virtual private servers
Protect your VPS from DDoS attack →
9 steps to harden your virtual server
Expert tips for managing your VPS →
A complete guide to help you run your VPS with confidence
Brooke Oates is a Product Manager at Liquid Web, specializing in Cloud VPS and Cloud Metal, with a successful history of IT/hosting and leadership experience. When she’s not perfecting servers, Brooke enjoys gaming and spending time with her kids.