How to Create Command Aliases in Linux and macOS
- Published on
- • 20 minutes read
Aliases in Unix-based systems like Linux and macOS allow you to create shortcuts for frequently used or complex commands. This tutorial will guide you through creating aliases that execute multiple commands in sequence.
Imagine you frequently need to clear the terminal screen and then list all files in the current directory in detail. Instead of running these commands separately:
clear
ls -la
You can create an alias that combines both commands:
alias ll='clear && ls -la'
An alias assigns a custom shortcut to a longer command or sequence of commands. This helps simplify command execution and improves workflow efficiency.
To create a temporary alias (valid only for the current terminal session), use this syntax:
alias alias_name='command1 && command2 && command3'
For example, to create an alias that clears the screen and lists files:
alias cls='clear && ls -la'
Now, typing cls in the terminal will execute both commands sequentially.
To make an alias persist across terminal sessions, add it to the shell configuration file. The file depends on your shell and operating system.
Edit the .bashrc
(Linux) or .bash_profile
(macOS) file:
Open the configuration file with your preferred text editor:
nano ~/.bashrc # For Linux
nano ~/.bash_profile # For macOS
Add the alias to the file:
Append this line to the end of the file:
alias cls='clear && ls -la'
Save and exit the editor.
Apply the changes by sourcing the file:
To apply changes without restarting the terminal, run:
source ~/.bashrc # For Linux
source ~/.bash_profile # For macOS
If you use Zsh, the configuration file is .zshrc
.
Edit the .zshrc
file:
Open the configuration file with your preferred text editor:
nano ~/.zshrc
Add the alias to the file:
alias cls='clear && ls -la'
Save and close the file.
Apply the changes by sourcing the file:
source ~/.zshrc
Creating aliases in Linux and macOS helps streamline command execution and improves productivity. By following these steps, you can customize your terminal environment and optimize your workflow.