Using aliases on Linux
Using aliases on Linux systems can save you a lot of trouble and help you work faster and smarter. This post examines the ways and reasons that many Linux users take advantage of aliases, shows how to set them up and use them, and provides a number of examples of how they can help you get your tasks done with less trouble.
What are aliases?
Aliases are simply one-line commands that are assigned names and generally stored in a startup file (e.g., .bashrc) that is run when you log in using a tool like PuTTY or open a terminal window on your desktop. The syntax is easy. It follows this pattern:
$ alias NAME = 'COMMAND'
As a simple example, typing a command like that shown below enables you to clear your screen simply by typing “c”.
$ alias c="clear"
If you add that alias to the end of your startup file with a command like the one shown below, it will be available every time you open a terminal window. You can also edit your ~/.bashrc file and add an alias like c=’clear’ to it.
$ echo “alias c="clear"” >> ~/.bashrc
Anything you can do with an alias can be done with a script, but aliases are easier to organize and simpler to use.
Search paths and aliases
Aliases can be dependent on your search path if you don’t include full path names as part of the aliases. Most Linux users, however, will have file system locations such as /bin, /usr/bin, /usr/local/bin and such in their search paths – mostly set up by default – so few aliases will require more than the command and its arguments.
You can display your search path with a command like this:
$ echo $PATH /usr/bin:/bin:/usr/local/bin:~/bin
Listing aliases
To get a list of all your active aliases, all you need to do is use the command “alias” and it will display a list of your aliases in alphabetical order.
$ alias alias .. = 'cd ..' alias apt-get="sudo apt-get" alias c=clear alias recent="history | tail 10" …
Simplifying complex commands
The alias that lets you clear your terminal window by typing “c” in place of “clear” is commonly used. Some Linux users also like to assign the cd .. (move up one directory) command to “..”.
$ alias ..='cd ..'
To ensure that grep commands use color to highlight the search terms in their output, you an use aliases like these:
alias grep='grep --color=auto' alias egrep='egrep --color=auto' alias fgrep='fgrep --color=auto'
Avoiding typos
Long or complicated commands are a good target for turning into aliases. You save time and avoid typos. The most important issue is giving them names that are easy to remember and relate to the purpose of the command.
alias recent="history | tail -20"
Running privileged commands
If you’re at all likely to forget to insert “sudo” at the beginning of a command that requires it, you can create an alias that does this for you. You can even give the alias the same name as the command as aliases will take precedence.
For example, to reboot a system, you could use an alias like this one:
$ alias reboot="sudo reboot"
Similarly, running updates can be done with an alias like this one:
$ alias updates="sudo dnf update && sudo dnf upgrade -y"
Deactivating aliases
To temporarily deactivate an alias, you can use the unalias command. That will make the alias unusable until you log in again. To activate it permanently, you need to remove it from your startup file or comment it out.
Saving aliases for repeated use
You can create an alias and use it for a single login session without saving it, but it will not be available when you login again unless you save it. To view recent aliases that you have set up, check your command history with a command like this one:
$ history | grep alias
Typical uses for aliases include simplifying commands, running commands without needing full pathnames, making complex commands simple to use, avoiding typos and simply saving yourself some time.
Sourcing an alias file
If you want, you can store aliases in a their own file (i.e., not your startup file) and source it when you need to make them active again with a command like this:
$ . myaliases
Wrap-up
Aliases can save you time and trouble and are very simple to set up and manage. Whether you’re clearing your screen or running a long and complicated command, they can save you quite a bit time and trouble.
Copyright © 2023 IDG Communications, Inc.