Linux alias Command: Create Terminal Shortcuts
An important shell feature is the ability to create aliases or Linux shortcuts for commands. It can be used to simplify commands with many options or run several processes in sequence. The alias command creates these shortcuts:
$ alias apelido="comandos"
In this example the alias psweb corresponds to the ps -e | grep nginx commands:
$ alias psweb="ps -e |grep nginx"
$ psweb
9113 ? 00:00:00 nginx
9115 ? 00:00:22 nginx
9116 ? 00:00:00 nginx
The alias list can be displayed with the alias command without any parameters:
$ aliasalias ..=’cd ..’alias ...=’cd ../..’alias l=’ls -alF’alias la=’ls -la’alias ll=’ls -l’alias ls=’/bin/ls $LS_OPTIONS’alias psweb="ps -e |grep nginx"
Some distributions use some pre-configured aliases to facilitate the "transition" of users accustomed to the Windows prompt. Others include options in commands like rm and mv, to avoid overwriting files without asking:
$ alias
alias rm=’rm -i’
alias mv=’mv -i’
An interesting alias to check the size of a directory and its subdirectories can be done with:
$ alias dus='du -sh ./*/'
Another "lazy" alias is to open a tar file:
$ alias untar='tar -zxvf '
If you want a little help to create an 8-character password:
$ alias senha='openssl rand -base64 8'
You can also imitate the Windows ping with only 5 pongs:
$ alias ping='ping -c 5'
If you need a web server in any directory to do some quick tests:
$ alias www='python -m SimpleHTTPServer 8000'
Want to know what your external IP is (valid on the Internet) directly in the shell?
$ alias meuip='curl ipinfo.io/ip'
Several commands can be executed with the alias all at once, using the semicolon ";" to separate them:
In this example, the commands w, free and df are executed with the alias "verifica":
$ alias verifica='w|grep load ; free -h; df -h'$ verifica 11:13:51 up 20:54, 1 user, load average: 0,00, 0,00, 0,00 total used free shared buff/cache availableMem: 983M 139M 201M 11M 642M 661MSwap: 0B 0B 0BSist. Arq. Tam. Usado Disp. Uso% Montado em/dev/xvda1 16G 11G 5,3G 68% /tmpfs 99M 0 99M 0% /run/user/1000
It is important to note that an alias created in the shell will only exist permanently if it is saved in a Bash load script or in the profile configuration files. If you don't save your aliases, when you disconnect, you will lose your keyboard shortcuts.
It is suggested to make the shortcuts permanent by saving them in the .bashrc file in the user's HOME directory.
Conclusion
If you use the shell a lot, you need to create your own list of shortcuts with the alias command to gain productivity. Just don't become dependent on them.... :-D
Learn much more about Linux in our online course. You can enroll here. If you already have an account, or want to create one, just log in or create your user here.
Liked it? Share
That was just one piece. The course teaches the whole path — from your first command to exam day.