Introduction

Hello! In this article, we’ll learn about the alias command.

alias is a command that lets you shorten long commands or give alternative names to commands you use frequently. Using this makes your work way easier.

What is the alias Command?

alias is a command for giving alternative names (aliases) to commands.

For example, isn’t it annoying to type git status over and over? You can make it so just gs runs it, stuff like that.

Basic Syntax

1
alias shortname='original command'

Usage Examples

Example 1: Basic Usage

1
alias ll='ls -la'

Now typing ll runs ls -la.

Example 2: Using an Alias

1
2
alias ll='ls -la'
ll

Output:

1
2
3
4
5
# Output of ls -la is displayed
drwxr-xr-x  5 user user  4096 Oct 26 12:00 .
drwxr-xr-x 10 user user  4096 Oct 26 11:30 ..
-rw-r--r--  1 user user   220 Oct 26 10:00 .bashrc
...

Example 3: List Current Aliases

1
alias

Output:

1
2
3
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'

Shows all currently set aliases.

Example 4: Check a Specific Alias

1
alias ll

Output:

1
alias ll='ls -la'

Example 5: Remove an Alias

1
unalias ll

This removes the ll alias.

Common Alias Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# ls family
alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'

# cd family
alias ..='cd ..'
alias ...='cd ../..'

# git family
alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'

# Safety improvements
alias rm='rm -i'    # Confirm before deletion
alias cp='cp -i'    # Confirm before overwrite
alias mv='mv -i'    # Confirm before overwrite

# Other useful ones
alias h='history'
alias grep='grep --color=auto'

Tips & Notes

  • Temporary setting: Gone when you close the terminal

    1
    
    alias ll='ls -la'  # Gone when terminal closes
    
  • Make it permanent: Write in .bashrc to persist

    1
    2
    3
    
    # Add to ~/.bashrc
    alias ll='ls -la'
    alias gs='git status'
    
  • Use single quotes: Double quotes expand variables, so use single quotes

    1
    
    alias mydir='cd ~/my/directory'  # Correct
    
  • Run without alias: Prefix with backslash to run the original command

    1
    2
    
    alias rm='rm -i'
    \rm file.txt  # Delete without confirmation
    
  • With spaces: Need to properly quote it

    1
    
    alias update='sudo apt update && sudo apt upgrade'
    

Practical Usage

Shorten Dev Commands

1
alias dev='cd ~/projects/myapp && code .'

Move to project folder and open VS Code - all in one command.

Shorten Long Commands

1
alias dockerps='docker ps --format "table {{.Names}}\t{{.Status}}\t{{.Ports}}"'

Shorten frequently used docker options.

Typo Prevention

1
2
alias gti='git'
alias claer='clear'

Make aliases for commands you often mistype (lol).

.bashrc Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
# ~/.bashrc

# ls family
alias ll='ls -la'
alias la='ls -A'

# cd family
alias ..='cd ..'
alias ...='cd ../..'

# git family
alias gs='git status'
alias ga='git add'
alias gc='git commit'

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

Writing in .bashrc like this is convenient.

Summary

In this article, we learned about the alias command.

Key Points:

  • Give alternative names to commands with alias
  • Can shorten long commands - very convenient
  • Write in .bashrc to make permanent
  • Actively aliasing frequently used commands boosts efficiency

As you get used to it, you’ll think “Let’s alias this too” and keep adding more. Find commands you use often and alias them - makes work so much easier.

Keep learning Linux commands!