Linux alias command: How to create and use Linux aliases

Unix/Linux aliases FAQ: How do I create an alias in Linux? Can you share some examples of the Linux alias command?

Using Linux aliases

Aliases in Unix and Linux operating systems are cool. They let you define your own commands — or command shortcuts — so you can customize the command line and make it work the way you want it to work. In this tutorial I'll share several Linux aliases that I use on a daily basis.

Unix alias examples

Aliases make your command-line life easier. For instance, instead of typing ls -al dozens of times a day, I create an alias using the letter l (lowercase L):

alias l="ls -al"

Then I just type that one character to get a directory listing:

$ l
total 24
drwxr-xr-x   8 al  staff    256 Mar 30 16:49 .
drwxr-xr-x+ 93 al  staff   2976 May 15 12:33 ..
-rw-r--r--@  1 al  staff  10244 May 13 12:19 .DS_Store
drwxr-xr-x  19 al  staff    608 Feb  3 14:14 IntroToFP
drwxr-xr-x  23 al  staff    736 Nov 28 15:38 IntroToScala3
drwxr-xr-x   7 al  staff    224 Feb  1 16:36 JSoupTest
drwxr-xr-x  27 al  staff    864 Mar 20 14:36 Misc-Examples
drwxr-xr-x  13 al  staff    416 May 12 12:42 ZIO

Here are a few other aliases I use related to the ls command:

alias l="ls -al"
alias lf="ls -aFG"
alias lsize="ls -Slhr"
alias ldate="ls -ltr"

Aliases are also nice when you’re working with Git, and you don’t want to have to type those long commands all the time:

alias gpom="git push origin main"
alias gs="git status"
alias gb="git branch"
alias gco="git checkout"

Using aliases like this you can create anything from simple shortcuts like this to powerful custom commands.

How to define a Linux alias

Creating a Linux alias is very easy. You can either enter them at the command line as you're working, or more likely, you'll put them in one of your startup files, like your .bash_profile or .bashrc files, so they will be available every time you log in.

I created the l alias above by entering the following command into my .bash_profile file:

alias l="ls -al"

As you can see, the Linux alias syntax is very easy:

  1. Start with the alias command
  2. Then type the name of the alias you want to create
  3. Then an = sign, with no spaces on either side of the =
  4. Then type the command (or commands) you want your alias to execute when it is run. This can be a simple command, or can be a powerful combination of commands.

Unix/Linux alias examples and syntax

To get you going, here is a list of sample Linux aliases I use all the time. I've pretty much just copied them here from my .bash_profile file:

alias dirs="ls -al | grep '^d'"

alias l="ls -al"
alias lf="ls -aFG"
alias lm="ls -al | more"
alias lsize="ls -Slhr"
alias ldate="ls -ltr"

alias h="history"
alias html="cd /web/apache/htdocs/devdaily/html"
alias logs="cd /web/apache/htdocs/devdaily/logs"
alias psm="ps auxwww | more"
alias nu="who | wc -l"

# GIT
alias gpom="git push origin master"
alias gs="git status"
alias gb="git branch"
alias gco="git checkout"

# SCALA
alias repl='scala-cli repl --jvm adopt:14 --classpath=/Users/al/repl'

As you can see, you can get as creative as you want, and pipe commands together to create “command pipelines” to do whatever you want. In the last alias shown I've actually combined three Linux commands in a row into one alias to get the output I want.

I show command pipelines in the dirs, lm, psm, and nu alias commands.

Because the Unix shell is very programmable and because the output of commands is very consistent and reliable, you can create your own aliases to do just about anything.