Setting Up Aliases - How to Shorten Linux Commands

Setting Up Aliases - How to Shorten Linux Commands

What you'll be able to do

  • Give a long command a short name with `alias`
  • Keep it for the next terminal by adding it to `~/.bashrc` and running `source`
  • Remove it with `unalias`, and find the cause with `type` when it does not work

Prerequisites (read these first)

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: Yes. You can use an alias. It lets you give a long command a short name. Type alias ll='ls -la --color=auto', and from then on ll does the job.
Lina: That's it? It sounds very easy.
Linny-senpai: It is very easy. There is one catch: an alias made this way disappears when you close the terminalAn interactive program that reads the commands you type and runs them.. Knowing the fix for that makes it complete.

An alias is a short name you give to a longer command. Some people call it a nickname or a shortcut. This article uses the word "alias" throughout.

An alias saves typing. It also cuts down on typos.

Words used here

  • Terminal: the screen where you type commands and read the results as text.
  • Shell: the program that reads your command and runs it. This article assumes bash.
  • Terminal, shell, and console are often used to mean almost the same thing.

Think of an alias as a table of replacements that the shell remembers.

One-liner summary

alias shortname='long command'

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

Nothing here can break your machine

The alias and unalias commands in this article never delete a file. They never edit one either. A typo will not damage your computer.

Only the ~/.bashrc step touches a settings file. Section 4-1 shows you how to undo it before you make any change.

What You'll Learn

  • Give a long command a short name with alias name='command'
  • List all aliases with alias, or check just one with alias name
  • Check a definition with type, and skip an alias once by adding \
  • Keep an alias for the next terminal by adding it to ~/.bashrc and running source
  • Remove an alias with unalias. It comes back until you delete the line from .bashrc

1. Basic Usage

Conclusion: Set an alias with alias name=command and use it at once, but only this session.

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

An alias created with the alias command is active only in the current terminal. It disappears when you close the terminal.

Section 4 shows how to keep it.

1-2. List all current aliases

$ alias

Run alias with nothing after it. It then shows every alias you can use right now.

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

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

What you see depends on your setup. A different list is fine.

1-3. Check a specific alias

$ alias ll
alias ll='ls -la'

2. Useful Alias Examples

Conclusion: Common aliases cover ls, directoryA container that organizes files. Same idea as a "folder" on Windows or macOS. hops, safe -i flags, and short git commands.

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 every file in full detail, hidden files included. ls -A also shows hidden files, but it leaves out the . and .. lines.
Linny-senpai: A hidden file is simply a file whose name starts with .. The classic example is .bashrc. Pick whichever one fits your need.

2-2. Directory navigation

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

Type .. and you move up one directory. A directory is the same thing Windows and MacA system-wide access control model enforced by fixed rules (Mandatory Access Control). call a folder.

2-3. Safer file operations

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

-i is a switch you add after a command to change how it behaves. Switches like this are called options.

With -i, the command asks "are you sure?" before it acts.

Using rm safely

A file removed with rm does not go to a trash can the way it does in a GUI. It is gone at once. That is exactly why a confirming alias such as alias rm='rm -i' is useful.

The virtual terminal on this site is for practice. Nothing you type there can delete a file on your own computer, so try things freely.

Watch out in shell scripts

If these aliases are active inside a script, the script can behave in a way you did not expect. It may stop and wait for an answer.

Write \rm or /bin/rm in scripts. That skips the alias and calls the command itself.

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 and git status runs? I use it every day, so that helps a lot.
Linny-senpai: Right. Just don't make them so short that you forget what they do. Run alias when you need the full list.

3. Checking an Alias and Skipping It Once

Conclusion: type tells you what a name really is. Adding a backslash skips the alias for that one run.

3-1. Check if an alias is defined

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

type tells you what a name really is. It distinguishes an alias from a command, a function, or a shell built-in.

If the name is not defined, you see this instead:

$ type ll
bash: type: ll: not found

3-2. Skip the alias for one run

With alias rm='rm -i' set, every removal asks for confirmation. To skip that just once, put \ in front of the name.

$ \rm file.txt

The \ tells the shell to use the command itself, not the alias. Your setting stays in place, and the next run asks again.

3-3. Lina gets stuck: rm did not ask

Lina: I put alias rm='rm -i' in my .bashrc. Even so, the rm inside my script deleted the file without asking. Did my setting break?
Linny-senpai: Nothing is broken. Aliases are not expanded inside a script.
Linny-senpai: So the rm in your script runs as the real rm, not as your alias. No question is asked.
Lina: So there are places my setting never reaches? That surprised me.
Linny-senpai: Yes. An alias is a convenience for the interactive screen. To get a confirmation inside a script, write rm -i yourself. That is the reliable way.
Lina: I see. An alias is a shortcut for what I type by hand. That makes sense now.

4. Making Aliases Permanent

Conclusion: Add aliases to ~/.bashrc so they survive a closed terminal, then run source to apply them now.

To keep aliases after you close the terminal, add them to ~/.bashrc.

~/.bashrc is a settings file that the shell reads automatically every time you open a new terminal. The ~ stands for your home directory.

4-1. Make a backup before you edit

Prepare the undo first

~/.bashrc is a settings file. A mistake in it can print an error every time you open a terminal.

So copy it before you edit.

$ cp ~/.bashrc ~/.bashrc.bak

If something goes wrong, restore the copy.

$ cp ~/.bashrc.bak ~/.bashrc
$ source ~/.bashrc

If the terminal still works and only prints an error, those two lines put it back.

If the terminal opens and closes immediately, you can start a shell that skips the settings files. From another terminal, type this:

$ bash --norc --noprofile

From there, the cp above restores your file.

4-2. Edit .bashrc

$ nano ~/.bashrc

nano is a text editor that is easy for beginners. 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-3. Apply the changes right away

Editing .bashrc does not reach the terminal you already have open. Re-read the file with this command:

$ source ~/.bashrc

The shorthand means the same thing:

$ . ~/.bashrc

What does source do?

It reads .bashrc again in the current shell.

A new terminal loads .bashrc on its own. With source, you apply the change now, without opening a new terminal.

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

5. Removing Aliases

Conclusion: unalias removes one, unalias -a removes all, but a line left in .bashrc brings it back.

5-1. Remove a specific alias

$ unalias ll

5-2. Remove all aliases

$ unalias -a

unalias only affects the current terminal. Close it and open a new one, and .bashrc is read again. The alias comes back.

To remove it for good, delete the line from ~/.bashrc as well.

6. Troubleshooting

Conclusion: Check type first. A missing save, a missing source, and spaces around = are the three usual causes.

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

If it is defined, you see the line above. If nothing appears, check these three points.

Troubleshooting checklist

  1. Did you save .bashrc? (In nano: Ctrl+OEnterCtrl+X)
  2. Did you run source ~/.bashrc after saving?
  3. Is the alias written correctly?

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. With spaces, the shell reads ll as a command name instead.

7. Mini Exercises: Try It Yourself

Conclusion: Create it, check it, keep it. Three exercises cover the basics by hand.

Lina: I follow the explanation. Now I want to try it myself.
Linny-senpai: Good. I have three exercises for you. Run them in your terminal.

Exercise 1: Create an alias named la that lists files including hidden ones.

Show Hint 1 (Direction)

You join a short name and the longer command it stands for with an equals sign. The command goes inside quotes.

Show Hint 2 (Command name)

Use alias. The command that also shows hidden files is ls -a.

Show Answer
$ alias la='ls -a'
$ la
.  ..  .bashrc  .profile  Documents  Downloads

The exact list depends on your setup. If entries starting with . appear, it worked.

Exercise 2: Confirm that la really is an alias. Then run it once while skipping the alias.

Show Hint 1 (Direction)

One command tells you what a name really is. To skip an alias, put a single character in front of the name.

Show Hint 2 (Command name)

Check it with type. The character that skips an alias once is \.

Show Answer
$ type la
$ \la
la is aliased to `ls -a'
bash: la: command not found

\la skips the alias. There is no real command called la, so command not found is the correct result here.

Exercise 3: Make la available in the next terminal you open. Back the file up before you edit it.

Show Hint 1 (Direction)

One settings file is read automatically whenever a terminal starts. Copy it first, then add your line to it.

Show Hint 2 (Command name)

Copy with cp, edit with nano, and apply with source. The file is ~/.bashrc. In nano, save and exit with Ctrl+OEnterCtrl+X.

Show Answer
$ cp ~/.bashrc ~/.bashrc.bak
$ nano ~/.bashrc

Add this one line at the end of the file and save.

alias la='ls -a'
$ source ~/.bashrc
$ type la
la is aliased to `ls -a'

For a single line, you can also skip nano and type this instead. >> adds one line to the end of a file; a single > would erase the whole file, so it is not used here.

$ echo "alias la='ls -a'" >> ~/.bashrc

8. Review

Lina: So alias applies to the current screen only, and writing it in .bashrc makes it available next time.
Linny-senpai: Exactly. Think of source as the small extra step that says "read it again now".
Lina: And when something fails, type tells me what the name really is. Aliases do not apply inside scripts. I have that.
Linny-senpai: That's it. Now pick two or three commands you type often and turn them into aliases.

Command Reference

Task Command
Create an alias alias ll='ls -la'
List all aliases alias
Check a specific alias alias ll
See what a name really is type ll
Skip an alias once \ll
Remove a specific alias unalias ll
Remove all aliases unalias -a
Make aliases permanent Add to ~/.bashrc, then run source ~/.bashrc

Today's 3-Line Summary

  1. alias name='command' creates a short name, but only for the current terminal
  2. Add it to ~/.bashrc and run source, and it works in the next terminal too
  3. When it fails, run type name. Never put spaces around the =

Next Reading

Share this article

Next steps