Alias command in Linux (keyboard shortcuts) [Basic Guide]

An important feature of the shell is the possibility of creating nicknames or Linux shortcuts for commands. It can be used to simplify commands with many options or execute several processes in sequence. The alias command creates these shortcuts:

$ alias nickname=”commands”

In this example, the nickname 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 list of nicknames can be displayed with the alias command without any parameters:

$ alias 
alias.. ='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 make use of some pre-configured nicknames to facilitate the “transition” of users who are used to the Windows prompt. Others include options in commands such as rm and mv, to avoid overwriting files without asking:

$ alias
alias rm = 'rm -i'
alias mv='mv -i'

An interesting alias for verifying the size of a directory and its subdirectories can be done with:

$ alias dus='du -sh. /*/'

Another very “lazy” alias is to open a tar file:

$ alias untar='tar -zxvf '

If you want a little strength to create an 8-character password:

$ alias password='openssl rand -base64 8'

You can also mimic the Windows ping with just 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'

Do you want to know your external IP (valid on the Internet) directly in the shell?

$ alias myip='curl ipinfo.io/ip'

Several commands can be executed with the alias at once, using the semicolon “;” to separate them:

In this example, the w, free, and df commands are executed with the nickname “verify “:

$ alias verifies = 'w|grep load; free -h; df -h' 
$ verify
11:13:51 up 20:54, 1 user, load average: 0.00, 0.00, 0.00
total used free shared buff/cache available
Mem: 983M 139M 201M 11M 642M 661M
Swap: 0B 0B
0B System Ark. Tam. Used Disp. Use% Mounted on

/dev/xvda1 16G 11G 5.3G 68%/
tmpfs 99M 0 99M 0% /run/user/1000

It is important to note that a nickname 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 log out, you’ll lose your keyboard shortcuts.

It is suggested to make the shortcuts permanent, save them to 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 get dependent… 😀

Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.

Uirá Endy Ribeiro

Uirá Endy Ribeiro is a Software Developer and Cloud Computing Architect with a 23-year career. He has master's degrees in computer science and fifteen IT certifications and is the author of 11 books recognized in the IT world market. He is also Director at Universidade Salgado de Oliveira and Director of the Linux Professional Institute - LPI Director's Board.

Leave a Reply 0

Your email address will not be published. Required fields are marked *


This site uses Akismet to reduce spam. Learn how your comment data is processed.

Need help?