Aliases are shortcuts that can help you in your day-to-day work. How you add aliases depends on the shell you use. In this blog post I will explain to you how to add aliases for the default bash shell. Everything is tested on Debian 10.
List currently defined aliases in Linux
Types of Aliases in Linux
There are two types of aliases: temporary and permanent. The temporary aliases are available just for the current terminal session. You can't use those aliases in another terminal session and they will be deleted when you close the session. Temporary aliases are useful if you need an alias for a short time. But if you need a command daily, is really complex, and/ or need it in multiple terminal sessions, a permanent alias is needed/ recommended.
Create command-line aliases
$ alias yourAlias="your command"keep in mind: case-sensitive
It will be available immediately in this session. You can try it or retype
alias to get the list of defined aliases.
echo 'alias yourAlias="your command"' >> ~/.bash_aliases
~/.bash_aliasesin your favorite text editor (if the file doesn't exist, create it)
- Add a new line with your alias:
alias actualyAlias="your command"
Your new alias will be available for all new sessions. For active sessions you can type
source ~/.bash_aliasesto reload your new alias.
You could add your aliases to
~/.bashrc, but the method above prevents you from destroying your
~/.bashrcfile and keeps your custom aliases separated.
To remove aliases from your current session you can use the
unalias yourAlias - Deletes one specific alias in this session
unalias -a - Deletes all aliases in this session
To delete an alias permanently you have to remove the alias from your
Execute alias as sudo
Before you can use aliases as a sudo user, you have to add
sudo="sudo " to your aliases.
alias sudo="sudo "
Multiple commands in one alias
Executes in serial, even if the previous command failed
Executes in serial, but only continues if the previous command succeeded.
alias yourAlias2="cmd1 && cmd2 && cmd3"
alias yourAlias3="cmd1 & cmd2 & cmd3"
Debian update routine
alias update='sudo apt-get update && sudo apt-get upgrade'
Initiate ssh session
alias ssh_ccr="ssh email@example.com"
Push dev env to a webserver
alias push2prod_ccr="rsync -av --update /devenv/ccr/ firstname.lastname@example.org:/home/web/html --progress
It is really easy to create and manage aliases and they help me with my daily tasks. Give it a try and let me know how you use them.