Skip to content
alias and history Commands in Linux — Save Time with Shortcuts

alias and history Commands in Linux — Save Time with Shortcuts

DodaTech Updated Jun 20, 2026 6 min read

The alias and history commands in Linux save you from typing repetitive commands — alias creates shortcuts, and history recalls everything you’ve typed. Together they eliminate hundreds of keystrokes daily.

What You’ll Learn

You’ll create temporary and permanent aliases, search command history, re-run commands with !, use reverse search (Ctrl+R), and build a productivity-enhancing bash environment.

Why alias and history Matter

A system administrator types hundreds of commands daily. docker ps, systemctl status, ssh user@server — these repeat constantly. Aliases shorten them to one or two characters. History lets you find and re-run yesterday’s complex command without retyping it. DodaZIP developers maintain shared alias files for build commands. Durga Antivirus Pro engineers use history grep extensively to trace previous scan sessions and reproduce bugs.

Learning Path

    flowchart LR
  A[Essential Commands] --> B[Bash Shell Basics]
  B --> C[alias & history<br/>You are here]
  C --> D[Bash Scripting]
  C --> E[Shell Configuration]
  style C fill:#f90,color:#fff
  
Prerequisites: Familiarity with essential Linux commands and Bash shell. These features work in bash, zsh, and most modern shells.

Syntax Overview

alias name='command'
alias                      # list all aliases
unalias name               # remove alias
history                    # show command history
!n                         # re-run command number n
!!                         # re-run last command
!$                         # last argument of last command

Options Table

CommandOptionDescription
alias(none)List all defined aliases
alias -p-pPrint aliases in a reusable format
unalias-aRemove ALL aliases
history(none)Show command history with line numbers
history NNShow last N commands
history -c-cClear history
history -d N-d NDelete specific history entry
history -a-aAppend current session to history file

Examples

Example 1: Create an Alias

$ alias ll='ls -la'
$ alias gs='git status'
$ alias dfh='df -h'

$ ll
total 24
drwxr-xr-x 2 user user 4096 Jun 20 10:00 .
drwxr-xr-x 3 user user 4096 Jun 20 10:00 ..
-rw-r--r-- 1 user user   45 Jun 20 10:00 file.txt

Typing ll is equivalent to ls -la. These aliases last only for the current session.

Example 2: Permanent Aliases (~/.bashrc)

$ echo "alias ll='ls -la'" >> ~/.bashrc
$ echo "alias gs='git status'" >> ~/.bashrc
$ echo "alias dc='docker compose'" >> ~/.bashrc
$ source ~/.bashrc

Adding aliases to ~/.bashrc makes them permanent. After sourcing or opening a new terminal, the aliases are available.

Example 3: List All Aliases

$ alias
alias ll='ls -la'
alias gs='git status'
alias dc='docker compose'
alias dfh='df -h'

Running alias without arguments shows every alias currently defined.

Example 4: Remove an Alias

$ unalias dc
$ alias
alias ll='ls -la'
alias gs='git status'
alias dfh='df -h'

unalias removes a specific alias. Use unalias -a to remove ALL aliases.

Example 5: history — View Command History

$ history | tail -5
 1001  ls -la /var/www/
 1002  sudo systemctl restart nginx
 1003  tail -f /var/log/nginx/error.log
 1004  docker compose up -d
 1005  history | tail -5

Each command is numbered. The history is saved to ~/.bash_history by default.

Example 6: Re-run with !! and !n

$ sudo systemctl restart nginx
[sudo] password for user:

# Oops, forgot sudo? Use !!
$ sudo !!
sudo sudo systemctl restart nginx   # hmm, that doubles sudo

# Better: use !!
$ !!
sudo systemctl restart nginx

# Re-run specific command by number
$ !1003
tail -f /var/log/nginx/error.log

!! re-runs the last command. !1003 re-runs command number 1003.

Example 7: Use Last Argument (!$)

$ mkdir -p /var/www/project
$ cd !$
cd /var/www/project

!$ expands to the last argument of the previous command. Also available as Alt+. (press Alt and period).

Example 8: Reverse Search (Ctrl+R)

(reverse-i-search)`sys': sudo systemctl restart nginx

Press Ctrl+R, start typing part of a command, and bash shows the most recent match. Press Ctrl+R again to cycle through older matches. Press Enter to run.

Example 9: history grep

$ history | grep "docker"
 1004  docker compose up -d
 1020  docker compose down
 1045  docker compose logs -f
 1088  docker system prune -a

Search history for all commands containing “docker”. Pipe to grep for fast lookup.

Example 10: Clear History

$ history -c
$ history
    1  history

-c clears the history for the current session. To also clear the history file on disk: > ~/.bash_history.

Example 11: Alias with Arguments (Function)

# Aliases can't take arguments directly — use a function
$ mkcd() { mkdir -p "$1" && cd "$1"; }
$ mkcd /tmp/my-new-project
$ pwd
/tmp/my-new-project

For aliases that need arguments, use a shell function. Add to ~/.bashrc for permanence.

Common Use Cases

Use CaseAlias/Command
List with detailsalias ll='ls -la'
Git status shortcutalias gs='git status'
Docker composealias dc='docker compose'
Re-run last command!!
Use last argument!$ or Alt+.
Find past commandhistory | grep keyword
Quick navalias ..='cd ..' and alias ...='cd ../..'

Common Errors

  • Alias not found after reopening terminal: Add aliases to ~/.bashrc and source it, not to a temporary session.
  • Alias expansion in scripts: Bash does NOT expand aliases in non-interactive shells (scripts). Use functions instead.
  • history not persisting: The shell must exit gracefully for history to save. A crash may lose the session’s history.
  • Ctrl+R not working: Some terminals or screen/tmux configurations may rebind Ctrl+R. Check your ~/.inputrc.
  • history -c doesn’t clear file: history -c only clears the current session. Run cat /dev/null > ~/.bash_history to clear the file.
  • ! in double quotes: echo "!!" prints literal !!, not the last command. Use single quotes or no quotes.

Practice Exercises

  1. Create aliases: Add ll, gs, and dfh aliases to your session.
  2. Permanent: Add the aliases to ~/.bashrc and verify they persist.
  3. History search: Use Ctrl+R to find a command from earlier today.
  4. Function: Create a mkcd function that creates a directory and enters it.
  5. History analysis: Use history | awk '{print $2}' | sort | uniq -c | sort -rn | head -10 to find your 10 most-used commands.

Challenge

Create a productivity toolkit in ~/.bashrc that includes:

# Navigation
alias ..='cd ..'
alias ...='cd ../..'
alias ~='cd ~'

# Listing
alias ll='ls -la'
alias l='ls -l'
alias la='ls -A'

# Safety
alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i'

# System
alias dfh='df -h'
alias duh='du -sh *'
alias free='free -h'
alias psg='ps aux | grep -v grep | grep'

# Git
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline --graph'

# Docker
alias dc='docker compose'
alias dps='docker ps'
alias dlogs='docker compose logs -f'

# Network
alias ports='ss -tulanp'
alias myip='curl -s ifconfig.me'

# Safety confirmation
alias shutdown='systemctl shutdown'
alias reboot='systemctl reboot'

Durga Antivirus Pro developers maintain a similar alias file for their daily workflow — reducing common commands to 2-3 characters.

Bonus: Add alias please='sudo $(fc -ln -1)' — type please to re-run your last command with sudo.

Real-World Task

You need to debug a recurring issue that appears during deployment. Use history | grep deploy to find all deployment commands you ran last week, then identify the exact sequence that caused the failure. Save this as a ~/.bash_aliases file for your team.

What is alias?

The alias command creates shortcuts for longer commands, allowing custom abbreviations like ll for ls -la, either temporarily or permanently via ~/.bashrc.

What is history?

The history command displays the list of previously executed commands, enabling recall and re-execution using !!, !n, and Ctrl+R reverse search.

Related Tutorials

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro