sandra_henrystocker
Unix Dweeb

Making bash aliases easy to manage

How-To
21 Mar 20244 mins
Linux

Aliases provide an easy way to reuse complicated or often-used commands.

Typing at a computer
Credit: Shutterstock

I’ve undoubtedly said this before, but the most effective aliases on Linux are those that save you a lot of time or help you avoid typing errors – especially those errors that might cause problems on your system. Aliases allow you to run both complicated and frequently used commands with minimal effort. If you type a command like alias rec=ls -ltr | tail -10‘ in your terminal session, you will have created an alias that will display the ten most recently created or updated files in your current directory. This makes it easier to remember what you’ve most recently been working on and to make necessary updates.

To preserve your aliases for future use, you can add them to your .bashrc file. If you do this, it’s a good idea to group them at the end of the file so that they’re easier to find, review and modify as needed. On the other hand, some of my techie friends prefer to store their aliases in a separate file and ensure that their shell will source that file whenever they log in by using a command like source aliases or simply . aliases. Note that the word “source” and the single character “.” do the same thing.

The list below shows a group of aliases. Some are extremely simple, like the one that allows you to type just a “c” instead of typing the word “clear”. I actually use that one very frequently and appreciate that I only have to type a single letter to clear my screen. One shows the largest files in the current directory, one shows the most recently updated files, and another installs system updates. There are many good reasons to use aliases to simplify commands without losing track of what those commands do.

alias big5='du -h | sort -h | tail -5'
alias c='clear'
alias install='sudo dnf install'
alias myprocs='ps -ef | grep `whoami`’V
alias myps='ps -ef | grep `whoami` | awk '{print \$2}'
alias recent='history | tail -10'
alias rec='ls -ltr | tail -5'
alias update='sudo dnf upgrade –refresh'

You can list aliases with the alias command and, when it’s helpful, sort them or use a sort or grep command to list only those containing certain strings.

$ alias | sort | head -2
alias big5='du -h | sort -h | tail -5'
alias c='clear'
$ alias | grep rec
alias recent='history | tail -10'
alias rec='ls -ltr | tail -5'

If you want an alias to go away temporarily for some reason, you can use the unalias command. As long as your alias is included in your .bashrc file, or a file that you source to make your aliases available to you, they’ll all be easily ready when you need to use them again.

$ unalias big5

The aliases below can save you a little time when you need to back up a directory or two. Just remember that aliases will not be available on your next login unless you save them in your .bashrc or separate aliases file.

alias up='cd ..'
alias up2='cd ../..'

When an alias won’t cut it

Aliases are extremely useful, but they have their limitations. When a task that you need to perform periodically is too complex for an alias because of various options that will change from time to time, consider writing a script instead.

Wrap-up

Aliases provide an easy way to reuse complicated commands and those that you use often without much effort.

sandra_henrystocker

Sandra Henry-Stocker has been administering Unix systems for more than 30 years. She describes herself as "USL" (Unix as a second language) but remembers enough English to write books and buy groceries. She lives in the mountains in Virginia where, when not working with or writing about Unix, she's chasing the bears away from her bird feeders.

The opinions expressed in this blog are those of Sandra Henry-Stocker and do not necessarily represent those of IDG Communications, Inc., its parent, subsidiary or affiliated companies.

More from this author

Exit mobile version