- Alias means other alternative name or nickname.
- We can give our own more convinient nicknames for unix commands. This concept is called command aliasing.
Note: we can use type command, to check whether the command is already available or not.
How to Create Alias Names?
$ alias nickname='original command'
$ alias nickname="original command"
# After aliasname space is not allowed. Hence the following are invalid
alias nickname ='original command'
alias nickname= 'original command'
alias nickname = 'original command'How to List all available Aliases? By using alias command without any arguments
$ aliasHow to Remove Alias Names? By using unalias command.
$ unalias alias_nameWhere we can use aliasing:
- If any lengthy command repeatedly required, then we can create shortcut alias name and we can use that short alias name every time.
alias d20f='mkdir dir1;touch dir1/file{1..20}.txt'-
List Files and Save to Output
- To list all files in the current working directory, save this data to
output.txt, and display the number of lines to the terminal:
alias current='ls -l | tee output.txt | wc -l'
- To list all files in the current working directory, save this data to
-
Using Windows Commands in Linux
- Create aliases for common Windows commands:
alias cls='clear' alias rename='mv'
-
Correct Typing Mistakes
- Create an alias for a common typo:
alias grpe='grep'
-
Handle Language Barriers
- In Germany, "datum" means "date". Create an alias for the date command:
alias datum='date'
You can add these aliases to your .bashrc or .bash_profile file to make them permanent.
Aliases created in a terminal session are temporary and will be lost once the terminal is closed. To make them permanent, you can use one of the following methods:
- Open the
.bashrcfile in your home directory:gedit ~/.bashrc - Add your aliases to the file. For example:
# My own aliases alias cls='clear' alias ddd='date; date; date'
- Note: You need to close and reopen the terminal for the changes to take effect.
- Create a file named
.bash_aliasesin your home directory:gedit ~/.bash_aliases - Add your aliases to this file. For example:
alias ccc='cal; cal; cal' alias ct='cal; date'
- Note: Again, you must close and reopen the terminal for the changes to take effect.
Q1: What is the Purpose of the Alias Command?
A: To list all available aliases and to create new aliases.
Q2: How to Use the Unalias Command?
- To remove a specific alias:
unalias alias_name - To remove all aliases:
unalias -a
Q3: In which file do we define aliases to persist them?
A: .bashrc or .bash_aliases.
Q4: Which of the following is a valid way of creating an alias?
- A
alias rename ="mv" - B
alias rename= "mv" - C
alias rename = "mv" - D
alias rename="mv"
Answer: D
Note: While creating aliases, do not include spaces around the=sign.