Unix/Linux aliases FAQ: 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
Using aliases makes your command-line life easier. For instance, a lot of people don't like the name of the grep
command, and wish they could change it to search
. With the alias
command you can do exactly that:
alias search=grep
Now you can just type search
instead of grep
at your Linux command line:
search 'Flintstone' StoryOfBedrock.txt
In another simple alias
example, instead of always typing this ls
command to get a directory listing:
ls -al
I've created an alias so I only have to type the lowercase letter "L" like this:
l
Whenever I use this alias, it's exactly the same as if I had typed out the longer ls -al
Linux command.
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:
- Start with the
alias
command - Then type the name of the alias you want to create
- Then an
=
sign, with no spaces on either side of the=
- 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
, andnu
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.