If you spend a lot of time in the Linux terminal, you likely type the same long, complex commands repeatedly. Whether it is an intricate find command with multiple flags, a 15-character SSH login string, or a specific package manager update sequence, retyping these strings is both tedious and error-prone. To solve this, the Linux shell provides the alias command, allowing you to create custom, personalized keyboard shortcuts that execute long command strings instantly.
Why Use the alias Command?
The alias command acts as a text-replacement tool for the shell. When you type an alias and press enter, the shell intercepts your custom word, replaces it with the underlying long command, and executes it. This drastically speeds up your workflow. It is incredibly useful for shortening commands you use dozens of times a day, or for creating easily memorable names for obscure, syntax-heavy commands you only use occasionally.
Step 1: Create a Temporary Alias
You can create an alias instantly in your current terminal session. Note that an alias created this way will disappear as soon as you close the terminal window.
- Open your Linux terminal.
- Use the syntax
alias name='command'. For example, to create a shortcut namedupdatethat runs the full Debian package update process:
alias update='sudo apt update && sudo apt upgrade -y'
- Press Enter.
- Now, type
updateand press Enter. The shell will execute the full apt update sequence.
Step 2: View Active Aliases
If you forget what aliases are currently active, you can list them.
- Simply type the command without any arguments:
alias
This will print a list of every active shortcut, including default aliases created by your Linux distribution (such as alias ls='ls --color=auto').
Step 3: Remove an Alias
If you made a mistake or want to delete a temporary shortcut, use the unalias command.
- Type the following command:
unalias update
Step 4: Create Permanent Custom Shortcuts
To ensure your custom shortcuts survive a reboot and are available every time you open a terminal, you must save them to your user’s shell configuration file (usually .bashrc or .zshrc).
- Open your bash configuration file in a text editor like nano:
nano ~/.bashrc
- Scroll to the very bottom of the file.
- Add your custom alias on a new line. For example:
alias logs='tail -f /var/log/syslog'
- Save the file (
Ctrl+O,Enter) and exit nano (Ctrl+X). - To apply the changes immediately without restarting the terminal, run the
sourcecommand:
source ~/.bashrc
By defining a robust list of permanent aliases in your .bashrc file, you can heavily customize your Linux environment to match your personal workflow.