
In everyday Linux use, it's common to need to type complex or repetitive commands in the terminal. While this can save a lot of time, it can also become cumbersome to have to remember or type long sequences every time. This is precisely where aliases in Linux come in , little lifesavers that turn tedious commands into simple shortcuts that you can customize to your liking.
If you're not yet familiar with the magic of aliases, or if you've heard of them but aren't quite sure how to use them effectively, here's a detailed explanation of everything you need to know to master their use , from the basics to helpful examples and advanced tips for a much more efficient and comfortable work environment. Get ready to save time at the keyboard and avoid unnecessary errors in your daily tasks.
What is an alias in Linux?
An alias in Linux is a abbreviation or short name which represents a command, or even a sequence of commands, that is more complete or difficult to remember. Its main function is to facilitate the use of the terminal, allowing you to save time and avoid writing errors. For example, you can convert a command as long as sudo apt update && sudo apt upgrade in simply actualizar, making your day-to-day life much simpler.
By defining an alias, you're essentially creating a custom shortcut within your shell (whether it's Bash, Zsh, Fish, or any other). This shortcut reacts when you type the assigned word, automatically executing the command you've configured.
Key advantages of aliases:
- They reduce the amount of text to be written, speeding up your workflow.
- They help avoid typographical errors, since you use words that are comfortable for you.
- They allow you to customize your environment just as you prefer.
- They facilitate the replacement or improvement of standard tools for more modern or visual ones without changing your habits.
What are aliases used for? Utilities and advantages
Aliases not only save time; they also increase productivity and improve the user experience in Linux. Some of their most practical uses are:
- Abbreviate common commands: For example,
llto executels -laand thus quickly obtain a detailed list of files. - Add default options: If you always add certain flags to your commands (
grep --color=auto,ls -lah), you can make it your standard alias and forget about typing them. - Replace tools easily: If you prefer alternatives like
exainstead oflsobatagainstcat, just redefine the alias for that command. - Avoid accidents: Adding default confirmation flags (
alias rm='rm -i'), you can protect yourself from accidentally deleting files. - Automate repetitive actionsFrom updating your system to navigating directories or launching custom scripts with a single word.
How to Create an Alias ​​in Linux
Defining an alias is very simple, but it is important to distinguish between temporary aliases (which only work in the current terminal session) and permanent aliases (which remain available even after restarting the computer or closing the console).
Temporary aliases
To create an alias that only lasts until you close the terminal:
alias alias_name='command_or_commands'
For example, to abbreviate ls -la:
alias ll='ls -la'
After running it, every time you type ll you will see the result of ls -la.
Permanent aliases
If you want your favorite aliases to be available whenever you open the terminal, you need to add them to your shell's configuration file. This varies depending on the shell you use:
- Bash: ~/.bashrc (the most common in most distributions).
- zsh: ~/.zshr.
- Fish: ~ / .config / fish / config.fish.
The steps to create a permanent alias in Bash would be as follows:
- Open your shell's configuration file with a text editor. For example:
nano ~ / .bashrc
- Add your alias to the end of the file. Example:
alias update='sudo apt update && sudo apt upgrade'
- Save the changes and close the editor (
Ctrl+Oto save in nano,Ctrl+Xto go out). - Reload the file to apply the new aliases without closing the terminal:
source ~/.bashrc
Now, every time you open a new terminal, your alias will be ready to use.
Special file: .bash_aliases
Some distributions and users prefer to centralize aliases in a dedicated file (~/.bash_aliases), including it later from the ~/.bashrc with a couple of lines:
if [ -f ~/.bash_aliases ]; then . ~/.bash_aliases fi
This way, you can keep all your aliases organized and separate from the rest of your settings.
Reviewing and managing your aliases in the terminal
Once you have several aliases defined, you may want to easily view or manage them:
- To view all active aliases in the session:
alias
- To view a specific alias:
alias alias_name
These commands give you a quick overview of the shortcuts available in your current environment.
Delete aliases: how to do it temporarily or permanently
At some point, an alias may become unusable or even cause conflicts (for example, if you use a name that matches an actual system command). There are several ways to remove them:
- Temporarily delete an alias (current session only):
unalias alias_name
- Delete all temporary aliases from the session:
unalias -a
- Delete a permanent alias: Opens the corresponding configuration file (
~/.bashrc,~/.zshrco~/.bash_aliases), find and delete the alias line you want to remove, save and reload the file withsource.
Syntax and important considerations when creating aliases
The syntax is simple, but it's worth keeping in mind some details to avoid any surprises:
- Do not use existing system command names to avoid losing access to key functionality. For example, redefining
sudomay leave you temporarily without administrator permissions. - If the command contains quotes, variables, or complex sequences, enclose the entire command in single quotes (
' '), or doubles if you need to evaluate variables. - If you need to pass variable arguments, standard aliases don't directly support them. In that case, it's best to define a function within your configuration file:
search() { grep -rnw . -e "$1"; }
This allows you to create shortcuts that accept dynamic values, increasing the flexibility of your environment.
Additional options of the alias command
Although the basic functionality is sufficient for most, the alias command supports some options that may be useful in advanced cases:
- -p: Displays all aliases defined in the current session in detail.
- -help: Displays built-in help on how to use aliases.
Examples of useful and practical aliases
Here's a collection of really useful and popular aliases among Linux users, which you can adapt or copy to your environment:
- Detailed listings and quick navigation:
alias ls='ls --color=auto' alias ll='ls -la' alias ..='cd ..' alias ...='cd ../..' alias ....='cd ../../..'
- Quick system update (for Debian/Ubuntu distributions):
alias update='sudo apt update && sudo apt dist-upgrade -y'
- Greater security in delicate operations:
alias rm='rm -i' alias cp='cp -i' alias mv='mv -i'
- alternative tools:
alias ls='exa -lah' # Use exa instead of ls (requires installation) alias cat='bat' # Use bat instead of cat (requires installation)
- Quick operations on directories:
alias mkdir='mkdir -pv' alias mkdircd='function _mkdircd(){ mkdir -p "$1"; cd "$1"; }; _mkdircd'
- Get information or launch scripts:
alias ip='curl icanhazip.com' alias syslog='sudo tail -n 10 /var/log/syslog'
- Connection speed test:
speed alias='curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python3 -'
These are just a few examples; the list is practically endless, and you can adapt it to your needs by changing the commands to what you use most in your daily life.
Aliases and functions: when to use each one?
An alias is perfect for abbreviating simple commands or those that don't require variable arguments. However, if you're looking for complete customization or need to pass parameters, shell functions are the best option. You can define functions in the same configuration files to achieve much more powerful results. For example:
search() { grep -rnw . -e "$1"; }
This way, you will be able to run buscar "texto" and pass any word instantly, increasing the flexibility of your environment.
Assign aliases to custom commands
You don't have to limit yourself to standard commands. You can also associate aliases with scripts, command sequences, or even combinations of several tools. For example:
alias ports='netstat -tulanp'to see the open ports.alias count='find . -type f | wc -l'to count the number of files in a directory.alias gh='history | grep'to quickly search through the command history.
Creativity knows no bounds here; it's all about adapting the terminal to your own working style.
Aliases in different shells: Bash, Zsh, Fish and more
While Bash is the most common example, aliases work in virtually all popular Linux shells . You just need to modify the configuration file and adapt some syntax if your shell requires it. For example, in Fish , alias definition is handled differently, but the logic is exactly the same. Consult your shell's documentation for specific details.
Useful aliases for your daily life: table of suggestions
| Alias | Associated Command | Utility |
|---|---|---|
| ll | ls-la | Detailed file listing |
| .. | Cd .. | Upload a directory |
| to update | sudo apt update && sudo apt upgrade | Upgrade the system |
| rm | rm -i | Delete files with confirmation |
| grep | grep –color=auto | Highlighted results in searches |
| mkdircd | function _mkdircd(){ mkdir -p «$1»; cd «$1»; }; _mkdircd | Create and enter the directory |
| speed | curl -s https://raw.githubusercontent.com/sivel/speedtest-cli/master/speedtest.py | python3 – | Connection speed test |
How to choose and organize your aliases
The best strategy is to start with a few must-have aliases and see if they actually improve your workflow. If you find yourself repeating a command constantly, that's a good candidate for an alias. Also, saving your aliases in the .bash_aliases or in commented sections within the .bashrc will help you keep everything well organized.
Understanding and mastering aliases in Linux will allow you to optimize your terminal productivity, personalizing your environment and simplifying your daily tasks. Experiment and adjust your shortcuts until you achieve a workflow that works perfectly for you.