Get a free website with any plan

See how
LINUX AND SSH

Reusing commands with history and shortcuts

Last updated

IN SHORT

Linux shell history lets you recall past commands in any SSH session on Flashcloud. Press the up arrow to step backward through recent commands, or press Ctrl+R to search your history using a text fragment. You can edit any recalled command before running it.

Every shell keeps a history of commands you've typed, and you don't have to scroll through it manually to reuse one. Press the up arrow to cycle backward through recent commands, or use Ctrl+R to search your history by typing a fragment of the command you remember. Both work the moment you open a terminal or SSH in.

These shortcuts save the most time on long commands: a multi-flag rsync, a database dump with connection details, or an SSH command to a specific host. Type it once, then recall and tweak it instead of retyping from scratch.

The basics: up arrow and history

Every interactive shell (bash, zsh, the shell you get over SSH) keeps its own history file. Press the up arrow to walk backward one command at a time, and down arrow to walk forward again. Press Enter to run the command as-is, or edit it first with the left/right arrows and backspace.

To see your whole history at once, run:

history

This lists recent commands with numbers. You can rerun a specific one with ! followed by its number, for example !482. This is fragile if the numbers shift, so it's mostly useful for a quick rerun in the same session.

Reverse search with Ctrl+R

The fastest way to find a command you've run before is reverse incremental search. Press Ctrl+R, then start typing any part of the command. The shell jumps to the most recent match as you type. Press Ctrl+R again to step further back through older matches, or press Enter to run the one showing.

If the match isn't quite what you want, press the right or left arrow to drop out of search mode and land on that command line for editing, rather than running it immediately. Press Ctrl+C to cancel the search entirely and get back to an empty prompt.

This is especially useful for commands you run rarely but not never, like a mysqldump against a specific database, a tar command with the right exclude flags, or an rsync to a particular directory. Type a distinctive word from the command and Ctrl+R finds it in seconds.

Editing the recalled line

Once a command is on your prompt, whether from the up arrow or a search, you're not stuck running it verbatim. A few shortcuts make edits fast:

  • Ctrl+A jumps to the start of the line, Ctrl+E jumps to the end.
  • Ctrl+U clears from the cursor to the start of the line, Ctrl+K clears from the cursor to the end.
  • Alt+B and Alt+F move backward and forward one word at a time, faster than holding an arrow key.
  • Ctrl+W deletes the word before the cursor.

Combine these with a recalled command: pull it up with Ctrl+R, jump to the hostname or filename with Alt+B, delete it with Ctrl+W, and type the new value.

Quick substitution shortcuts

Bash and zsh both support a shorthand for fixing a typo in the last command without retyping it:

^oldtext^newtext

Running this replaces the first occurrence of oldtext with newtext in your previous command and re-executes it. It's a one-shot fix, useful right after a command fails because of a small mistake.

!! reruns the last command outright. This is commonly paired with sudo, if you forgot to prefix a command that needed root: sudo !! reruns the previous command with sudo in front.

Where root matters: shared hosting vs VPS

On a VPS, you have root SSH access, so history and these shortcuts apply to any command on the box, including system-level ones like package installs or service restarts. On shared hosting, your SSH session runs as your own account with no root access, so history works the same way, but it's scoped to whatever commands your account can run: file operations, Git, WP-CLI if it's available, and similar user-level tools.

Either way, history is per-user and per-shell. If you SSH in as different users or switch shells, don't expect one history to show commands from the other.

A note on sensitive commands

History saves commands verbatim, including any passwords or tokens you typed directly on the command line, like a database connection string with a password in it. If you've done this, consider removing that line from your history file, and prefer prompts or config files for credentials where the tool supports it, so they never land in your shell history at all.

Common questions

How do I search for a command I ran earlier?

Press Ctrl+R and type a fragment of the command. The shell immediately displays the most recent match. Press Ctrl+R again to move backward through older matches, or press Enter to run it.

How do I fix a typo in my previous command?

Type ^oldtext^newtext and press Enter. The shell replaces the first instance of oldtext with newtext and reruns the command.

Why are my past commands missing after switching users?

Command history is isolated to each user account and shell. Switching to another user or shell opens a separate history file that does not show past commands from the previous one.

How do I rerun my last command with root privileges?

Run sudo !! on your command line. This adds sudo in front of your previous command and runs it. Root access requires a VPS, as shared hosting accounts do not have root privileges.

CAN'T FIND IT?

Real humans answer fast.

Hosting with us? Open a ticket and a real person replies - no scripts, no upsells. Still choosing a host? The same team is included with every plan, from day one.