Americas

  • United States
sandra_henrystocker
Unix Dweeb

How to best set up command aliases on Linux

How-To
May 05, 20217 mins
Linux

Setting up the most useful aliases on Linux is a mix of what's otherwise annoying to enter, hard to remember or typed way too often.

shortcut through a maze
Credit: Thinkstock

Used frequently, bash aliases can make working on the Linux command line a lot smoother and easier, but they can also be complicated and hard to remember. This post examines how you might make your aliases work for you rather than vice versa.

In general, aliases are especially good for:

  • simplifying commands that are long and overly complex
  • remembering commands with odd or complicated names
  • saving time using commands that you use very often

What you need to keep in mind is that:

  • aliases can themselves be hard to remember
  • giving an alias the same name as a regular command can be a good thing or a bad thing (more on this shortly)

How to create an alias

Use the alias command and remember to add it to your ~/.bashrc file so that it will still be waiting for you whenever you login.

For example, to set up an alias that allows you to view text files in a side-by-side fashion, you would type something like this:

alias side-by-side='pr -mt'

If “side-by-side” is too much to type, maybe “SxS” would be better.

alias SxS='pr -mt'

How to list aliases

Use the alias command to list all of the aliases that have been established in your account. Notice that they will be listed alphanumerically.

$ alias | head -5
alias ?='apropos'
alias ByCPU='ps aux --sort -%cpu'
alias ByCPUusage='ps aux | sort -nk 3'
alias ByMEMusage='ps aux | sort -nk 4'
alias ByMem='ps aux --sort -%mem'

How to check if a command is an alias

To check if a command is an alias or not, use the which command (e.g., “which ll”). If you get a response like the one below that shows the definition of the alias and the command that it uses, it’s an alias.

$ which ll
alias ll='ls -alF'
        /usr/bin/ls

If it were a command, you’d just see the full path to the file:

$ which date
/usr/bin/date

How to turn an alias off

If you need to deactivate an alias, you can use the unalias command. To make the change permanent, you will also need to comment it out or remove it from your .bashrc file.

$ unalias ll

Using aliases for frequently used commands

The more you use certain commands, the more time you’ll save by shortening them with an alias. In the aliases shown below, we clear the screen with a single letter, display a long file listing with two, and look at only the most recently created or updated five files–maybe those we’re still working on.

alias c='clear'
alias ll='ls -l'
alias new='ls -ltr | tail -5'

Using aliases to alter a command’s behavior

Sometimes you need to be careful not to assign an alias the same name as a command or you basically block the command or have to type it using it’s full path. On the other hand, you might use an alias to enforce a particular command behavior by including some of its options. For example, if you sometimes forget that ping on Linux will continue running until you stop it by typing control-c, you can make sure it sends out only four ping requests by using this alias:

alias ping='ping -c 4'

There’s one thing to consider though. If you have an alias like this set up and then type “ping -c 4”, you’re going to run into a problem because you’ll be telling the system to run “ping -c 4 -c 4”. The shell will complain with a usage error.

Using aliases to avoid a long string of options

Aliases are very useful for remembering long strings of command options. For example, to extract the contents of a compressed tar file (omit the v if you don’t want to watch the files being extracted), you might use an alias like this:

alias untar='tar -zxvf'

Using aliases for listing files in many ways

To list files with their file types (e.g., directory, executable, symbolic link etc.), try an alias like this:

alias lf='ls -F'

To list files by size:

alias lsz='du -sh * | sort -h'

To list recently created/updated files–details or names only:

alias new='ls -ltr | tail -10'
alias new10='ls -tr -1 | tail -10'

To count how many files are in your current file system location:

alias numfiles='find . -type f | wc -l'

Using aliases to help review command history

To view history using a search term:

alias rec='history | grep'

This allows you to view all the instances of using a particular command that are still in your history buffer. For example:

$ rec date
  136  01/04/21 12:00:03 grep updates *
  371  06/04/21 16:36:40 help date
  372  06/04/21 16:36:44 help -d date
  614  16/04/21 13:33:20 ssh firefly date
  623  16/04/21 13:47:22 ssh shs@firefly date
  922  27/04/21 12:14:49 updates
 1016  04/05/21 12:11:55 rec date

Using aliases to identify commands

To find commands that are related to a search term (without having to spell “apropos”), you can use this:

alias ?="apropos"

You would use it like this:

$ ? account
acct (2)             - switch process accounting on or off
acct (5)             - process accounting file
accton (8)           - turns process accounting on or off
...

Using aliases to work with trash

To put files into your desktop trash can from the command line (from which they can be recovered), use an alias like this one:

alias trash='mv --force -t ~/.local/share/Trash'

This alias puts the file(s) into your trash folder that will stay around until you empty it from the desktop by right-clicking on your trash folder and selecting “Empty Trash”. This leaves you with an easy option to recover files if you need and of them back.

To dump your trash can from the command line, you can use an alias like this one:

alias dumpTrash='find ~/.local/share/Trash -type f -exec rm {} ;'

Keep in mind that the only files to be deleted will be the files you deleted on your desktop or through using the trash alias shown above. Files deleted using the rm command will not end up in your .local/share/Trash folder.

Aliases for looking at processes in different ways

There are many ways to list files and focus on different details. Aliases can make this a lot easier.

By CPU usage:

alias ByCPUusage='ps aux | sort -nk 3'

By memory usage:

alias ByMEMusage='ps aux | sort -nk 4'
alias ByMem='ps aux --sort -%mem'

By process ID:

alias ByPID='ps aux | sort -nk 2'

By user:

alias ByUser='ps aux | sort -k 1'

Checking your OS release:

alias rel='lsb_release -r'      

Checking the status of your printer:

alias prstat='lpstat -p -d'

Displaying you IP address

alias myip='hostname -I | awk '''{print }''''

Converting octal numbers to decimal:

alias oct2dec='f(){ echo "obase=10; ibase=8; $1" | bc; unset -f f; }; f'

Wrap-Up

One of the nice things about aliases is that they remain available as you move around in your file system. They don't depend on your location or what's in your PATH variable. If you end up with 65 aliases, you might need to check them from time to time just to remember what they do. However, if you have to check very often, they might not be serving you as well as they should.

$ alias | wc -l
65
sandra_henrystocker
Unix Dweeb

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.