How to Use Alias in Linux: Simple Command Shortcuts
In Linux, you use
alias to create a shortcut for a longer command by typing alias name='command'. This lets you run the shortcut instead of the full command, saving time and effort.Syntax
The basic syntax to create an alias is:
alias name='command': name is the shortcut you want to use, and command is the full command it runs.- Use single quotes around the command to keep it as one string.
- To see all current aliases, just type
aliaswithout arguments.
bash
alias name='command'Example
This example creates an alias ll that lists files in long format with human-readable sizes. It shows how to create, use, and remove an alias.
bash
alias ll='ls -lh'
ll
unalias ll
alias llOutput
total 4.0K
-rw-r--r-- 1 user user 1.2K Jun 10 12:00 example.txt
bash: alias: ll: not found
Common Pitfalls
Common mistakes when using aliases include:
- Not using quotes around the command, which can cause errors.
- Defining aliases in a terminal session only, so they disappear after closing the terminal.
- Trying to alias commands that require arguments without proper handling.
To make aliases permanent, add them to your shell configuration file like ~/.bashrc or ~/.bash_profile.
bash
alias ll=ls -lh # Wrong: no quotes, will cause error
alias ll='ls -lh' # Correct: quotes includedQuick Reference
| Command | Description |
|---|---|
| alias name='command' | Create a new alias |
| alias | List all current aliases |
| unalias name | Remove an alias |
| source ~/.bashrc | Reload aliases from config file |
Key Takeaways
Use
alias name='command' to create shortcuts for long commands.Always put the command in single quotes to avoid errors.
Aliases created in a terminal session last only until you close it.
Add aliases to your shell config file to keep them permanent.
Use
unalias name to remove an alias when no longer needed.