In Linux, an alias is essentially a shortcut for a command or a series of commands. It allows you to create a new name (alias) for a command, which can make your work faster by reducing the amount of typing, making commands easier to remember, or setting default options for commonly used commands.
Prerequisites
- A system running a Linux distribution.
- An account with sudo privileges.
- Access to the command line.
How Aliases Work
When you define an alias, the shell will replace the alias name with the full command it represents whenever you type it in. This is especially helpful for repetitive tasks or long commands that you frequently use with specific options.
Basic Usage of Aliases
To create an alias in Linux, you use the following syntax:
alias alias_name="command_to_run"
For example:
alias ll="ls -alF"
Here:
ll
is the alias name.ls -alF
is the actual command the alias will run.
Now, whenever you type ll
in the terminal, it will execute ls -alF
, listing files in a detailed format with extra flags (-a
, -l
, and -F
).
Viewing Defined Aliases
You can see all currently defined aliases by simply typing:
alias
This will list all the active aliases in your session.
To view the command behind a specific alias, type:
alias alias_name
For instance:
alias ll
This will show something like alias ll='ls -alF'
if you previously created that alias.
Removing an Alias
If you want to remove a particular alias, use the unalias
command:
unalias alias_name
For example:
unalias ll
This removes the alias ll
from your session.
Making Aliases Permanent
By default, aliases are temporary and only last for the current session. To make an alias permanent, you can add it to your shell’s configuration file (e.g., .bashrc
for Bash):
- Open the
.bashrc
file in a text editor:vi ~/.bashrc
- Add your alias command at the bottom:
alias ll="ls -alF"
- Save and close the
.bashrc
file. - Reload the
.bashrc
file to apply the changes:source ~/.bashrc
After this, the alias will persist even after restarting the terminal.
Example Aliases
Here are a few useful examples of aliases:
1) Enhanced Listing Commands
alias l="ls -CF" # Compact list of files alias
la="ls # Lists almost all files, excluding . and ..
alias ll="ls -alF" # Detail list of permissions and file types
2) System Updates
alias update="sudo apt update && sudo apt upgrade"
3) Navigating Directories
alias ..="cd .." # Go up one directory
alias …="cd ../.." # Go up two directories
4) Safety Overrides
alias rm="rm -i" # Prompts before deleting files
alias cp="cp -i" # Prompts before overwriting files when copying
alias mv="mv -i" # Prompts before overwriting files when moving
These examples are meant to simplify or add safety to commonly used commands.
alias –help
alias --help alias: alias [-p] [name[=value] ... ] Define or display aliases. Without arguments, `alias' prints the list of aliases in the reusable form `alias NAME=VALUE' on standard output. Otherwise, an alias is defined for each NAME whose VALUE is given. A trailing space in VALUE causes the next word to be checked for alias substitution when the alias is expanded. Options: -p print all defined aliases in a reusable format Exit Status: alias returns true unless a NAME is supplied for which no alias has been defined.
Practical Understanding of Aliases
Think of aliases as shorthand commands, allowing you to:
- Simplify commands you run often.
- Add default flags to commands to suit your workflow.
- Increase efficiency and reduce typing time.
Aliases help tailor the command-line environment to your needs, making them especially useful for both beginners and experienced users.