Skip to content
Log In Sign Up info@muvcloud.com

Show Your History

history
history 20
history | grep nginx

Each line starts with a number. history is built into Bash, the default shell on Ubuntu, Debian, AlmaLinux, and Rocky Linux. Bash keeps the list in memory and writes it to ~/.bash_history when the shell exits. Every user has their own file, and root's is /root/.bash_history. Commands you run with sudo are saved in your own history. Other shells keep history differently; see types of shells in Linux.

Rerun Commands

TypeRuns
!!The last command
sudo !!The last command with sudo
!42Command number 42
!-2The second-to-last command
!systemctlThe last one starting with that word
!$The last argument of the previous command

These expansions run at once. To see what a pattern would run first, add :p, as in !systemctl:p, which only prints it. With shopt -s histverify in your .bashrc, Bash puts every expanded line back at the prompt for you to check before you press Enter, which is a sensible setting on a production server.

Search with Ctrl+R

Press Ctrl+R and type part of a command. Bash shows the most recent match; press Ctrl+R again to step to older ones. Enter runs the match, an arrow key places it on the command line for editing, and Ctrl+G cancels. The Up and Down arrow keys step through the history one line at a time.

Size: HISTSIZE and HISTFILESIZE

HISTSIZE is the number of commands the shell keeps in memory; HISTFILESIZE is the number of lines kept in the file. The default .bashrc on Ubuntu and Debian sets them to 1000 and 2000. To keep more, add these lines to ~/.bashrc:

HISTSIZE=10000
HISTFILESIZE=20000
shopt -s histappend
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"

histappend makes several sessions add to the file instead of overwriting each other, and history -a in PROMPT_COMMAND writes each command at once, so a dropped SSH connection loses nothing. Load the file with source ~/.bashrc; the file itself is explained in what is .bashrc.

Add Timestamps

HISTTIMEFORMAT="%F %T  "

With this line in ~/.bashrc, history prints the date and time in front of each command. From then on Bash also saves the time in the history file, as comment lines that start with #. Older entries without a saved time show the time they were loaded.

Skip Duplicates and Lines with a Leading Space

HISTCONTROL=ignoreboth
HISTIGNORE="ls:ll:history:exit"

ignorespace leaves out commands that start with a space, ignoredups leaves out a command repeated straight after itself, and ignoreboth means both; erasedups also removes older copies. The default .bashrc on Ubuntu and Debian already sets ignoreboth. HISTIGNORE is a colon-separated list of commands never to save.

Secrets and the History File

Anything you type on a command line is saved in plain text: a password after a database option, a token in a curl header, an exported API key. While the command runs, it is also visible to other users in the process list. Keep secrets off the command line:

  • Let the program ask: mysql -u root -p with nothing after -p prompts for the password.
  • Read it without echo: read -rs API_TOKEN stores what you type in a variable without showing it.
  • Use a protected file: keep the value in a file with mode 600 that the program reads; see environment variables in Linux.
  • A leading space: with ignorespace the line is not saved, but it still appears in the process list.

Delete One Entry or Clear the History

history | tail -5
history -d 1042
history -w

history -d removes entry 1042 from memory, and history -w writes the current list to the file, replacing the old one. To clear everything, run history -c and then history -w. If a password or token ended up in the history, deleting the line is not enough: other open sessions write their own copy when they close, and backups may already hold the file. Change the password or replace the token.

Updated:

Linux VPS with Full Root Access

MUVCloud cloud servers run on KVM with Enterprise NVMe SSD storage and full root access, in Turkey, the Netherlands, and the USA.

Cloud VPS Plans Starting at2.99 $/mo Cheap Linux VPS

No Limits on Service or Support

You are not on your own as your projects grow. Our support engineers are ready 24/7, on several channels, whenever you need them.

Support specialist
  • 100% customer satisfaction goal
  • Our own hardware
  • 24/7 ticket and email support
  • 4 countries with locations