How to Use the alias Command (with Examples)
The alias
command in Linux and Unix systems is used to create aliases, which are words that are replaced by a command string. Aliases can be used to create shortcuts for longer or frequently used commands. In this article, we will explore 8 different use cases of the alias
command along with code examples to illustrate each case.
List all aliases
To list all aliases in your current shell session, simply run the alias
command without any arguments:
alias
Motivation: This use case is helpful when you want to view all the aliases defined in your shell and check if any conflicts or duplicates exist.
Example Output:
alias la='ls -a'
alias ll='ls -l'
alias ..='cd ..'
alias c='clear'
Create a generic alias
To create a generic alias, use the following syntax: alias word="command"
, where word
is the alias you want to create and command
is the command string you want the alias to represent.
alias myalias="ls -l"
Motivation: This use case allows you to create custom aliases for frequently used commands, making it easier and faster to execute them.
Example Output: No output is produced when creating an alias.
View the command associated with a given alias
To view the command associated with a specific alias, use the alias
command followed by the alias name:
alias myalias
Motivation: This use case helps you verify the command string associated with a particular alias in case you forgot or want to double-check.
Example Output:
alias myalias='ls -l'
Remove an aliased command
To remove an aliased command, use the unalias
command followed by the alias name:
unalias myalias
Motivation: This use case is useful when you no longer need a particular alias and want to remove it to avoid any potential conflicts or confusion.
Example Output: No output is produced when removing an alias.
Turn rm
into an interactive command
To turn the rm
command into an interactive command, create an alias with the --interactive
(or -i
) option:
alias rm="rm --interactive"
Motivation: This use case helps protect you from accidentally deleting files by adding an interactive prompt before each deletion.
Example Output: No output is produced when creating an alias.
Create la
as a shortcut for ls --all
To create a shortcut alias la
for the ls --all
command, use the following syntax:
alias la="ls --all"
Motivation: This use case allows you to quickly list all files and directories, including hidden ones, by simply typing la
instead of the longer ls --all
command.
Example Output: No output is produced when creating an alias.
These are just a few examples of how the alias
command can be used to simplify and customize your command-line workflow in Linux and Unix systems. By creating aliases, you can save time and reduce the need to remember lengthy commands. Experiment with different aliases to find the ones that best suit your needs.
Remember that aliases expire with the current shell session unless defined in the shell’s configuration file, such as ~/.bashrc
. If you want your aliases to persist across sessions, consider adding them to your shell’s configuration file.