Setting Up Aliases - How to Shorten Linux Commands

Setting Up Aliases - How to Shorten Linux Commands

What Is an Alias?

Lina: Linny-senpai, I'm getting tired of typing ls -la --color=auto every single time. Is there a faster way?
Linny-senpai: Absolutely! You can use an alias — it lets you give a long command a short nickname. Just type alias ll='ls -la --color=auto' and from then on, ll does the job.
Lina: That's it? Sounds super easy!
Linny-senpai: Yep, really simple. The one catch is that aliases made this way disappear when you close the terminal. We'll cover how to make them permanent too.

An alias is a way to give a short custom name to a longer command. It reduces typing, prevents typos, and is a standard productivity technique on Linux.

One-liner summary

alias shortname='long command'

That's the whole thing. Try it right now.

1. Basic Usage

1-1. Create an alias

$ alias ll='ls -la'

Once set, you can use it immediately.

$ ll
total 48
drwxr-xr-x 5 user user 4096 May 31 19:00 .
drwxr-xr-x 3 root root 4096 May 20 10:00 ..
-rw-r--r-- 1 user user  220 May 20 10:00 .bash_logout
-rw-r--r-- 1 user user 3526 May 20 10:00 .bashrc

Aliases created with the alias command are only active in the current terminal session. They disappear when you close the terminal. See Section 4 for how to make them permanent.

1-2. List all current aliases

$ alias

Running alias without arguments shows all currently active aliases.

alias ll='ls -la'
alias grep='grep --color=auto'
alias ls='ls --color=auto'

On Ubuntu, aliases for grep and ls are often pre-configured. You can see them with the alias command.

1-3. Check a specific alias

$ alias ll
alias ll='ls -la'

2. Useful Alias Examples

2-1. ls shortcuts

alias ll='ls -la'
alias la='ls -A'
alias l='ls -CF'
Lina: What's the difference between la and ll?
Linny-senpai: ls -la shows all files including hidden ones with full details. ls -A also shows hidden files, but skips the . and .. entries. Use whichever fits your need.

2-2. Directory navigation

alias ..='cd ..'
alias ...='cd ../..'
alias ~='cd ~'

Just type .. to go up one directory level.

2-3. Safer file operations

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

The -i flag prompts for confirmation before overwriting or deleting.

Watch out in shell scripts

If these aliases are active inside a script, they can cause unexpected interactive prompts. Use \rm or /bin/rm in scripts to bypass aliases and call the command directly.

2-4. Git shortcuts

alias gs='git status'
alias ga='git add'
alias gc='git commit'
alias gp='git push'
alias gl='git log --oneline'
Lina: Just gs for git status? That's a huge time-saver since I use it constantly.
Linny-senpai: Right! Just don't make them so short you forget what they do. You can always run alias to check the full list.

3. Checking and Verifying Aliases

3-1. Check if an alias is defined

$ type ll
ll is aliased to `ls -la'

type shows whether a name is a command, alias, function, or built-in. If ll is not defined, you'll see:

$ type ll
bash: type: ll: not found

3-2. Run the original command, bypassing an alias

If you have alias rm='rm -i' but want to run rm without confirmation for once:

$ \rm file.txt

Prepending \ tells the shell to use the actual command, not the alias.

4. Making Aliases Permanent

To keep aliases across terminal sessions, add them to ~/.bashrc.

4-1. Edit .bashrc

$ nano ~/.bashrc

Add your aliases at the end of the file:

# My aliases
alias ll='ls -la'
alias ..='cd ..'
alias gs='git status'
alias cp='cp -i'
alias mv='mv -i'
alias rm='rm -i'

4-2. Apply the changes immediately

Editing .bashrc alone does not affect the current terminal. Run:

$ source ~/.bashrc

Or use the shorthand (same effect):

$ . ~/.bashrc

What does source do?

It re-reads .bashrc in the current shell session. New terminals automatically load .bashrc on startup, but source applies changes right away without opening a new terminal.

Lina: So after I add aliases to .bashrc and run source, they'll still be there the next time I open a terminal?
Linny-senpai: Exactly. Every new terminal loads .bashrc automatically, so your aliases are always ready.

5. Removing Aliases

5-1. Remove a specific alias

$ unalias ll

5-2. Remove all aliases

$ unalias -a

unalias only removes the alias for the current session. If the alias is in ~/.bashrc, it will come back when you open a new terminal. To permanently remove an alias, delete the line from ~/.bashrc as well.

6. Troubleshooting

Lina: I added the alias to .bashrc but ll still isn't working. What's wrong?
Linny-senpai: Run type ll first and see what it says.
$ type ll
ll is aliased to `ls -la'

If it's not defined, check the following:

Troubleshooting checklist:

  1. Did you save .bashrc? (In nano: Ctrl+OEnterCtrl+X)
  2. Did you run source ~/.bashrc after saving?
  3. Is there a syntax error in your alias definition?

Common mistake: spaces around =

# Wrong: spaces around = cause an error
alias ll = 'ls -la'

# Correct
alias ll='ls -la'

The = sign must have no spaces on either side.

Summary

Task Command
Create an alias alias ll='ls -la'
List all aliases alias
Check a specific alias alias ll
Remove a specific alias unalias ll
Remove all aliases unalias -a
Make aliases permanent Add to ~/.bashrc, then run source ~/.bashrc

3 steps to try right now

  1. Run alias ll='ls -la' to create an alias
  2. Type ll and confirm it works
  3. Add it to ~/.bashrc and run source ~/.bashrc to make it permanent

Next Reading