Skip to content

Terminal: who are you? The big world of alias

An alias is just what you think it should be. You are giving a command a different name. You could, for example rename ls to execute when you type list, or delete instead of rm. Typically you are trying to shorten a command or combine several often used ones.

Before we get started we need to understand the difference between a temporary and a permanent alias. A temporary alias is only available for the current terminal session and will be gone once you restart the program. To make it permanent we have to save it to a text file. There are several different files you can write them to, but the .bash_profile in your home directory works just fine (remember that the dot means it’s a hidden file). So let’s go find the file:

ls -a

There’s a chance the file doesn’t exist yet. If so you have to create it by using nano, touch, or your favorite text editor. Just make sure it’s set to plain text and that smart quotes are off and that format is set to Plain text:textEdit_smartquotes

Save it as .bash_profile and you are set. The syntax for creating an alias is as follows (notice the lack of spaces!!):

alias myName=existingCommand

For your first alias I’m suggesting the following:

alias rb="source ~/.bash_profile"

The rb here stands for reload bash and is used to tell bash that something was changed in the .bash_profile file and that its content needs to be reloaded. But before this alias will work we have to load it the long (non-aliased) way:

source ~/.bash_profile

From now on, whenever you make changes to .bash_profile you can just write your alias rb and you can test your addition.

One very common use for an alias is to make certain options to commands the default. For example:

alias ls="ls -p"

This adds the -p option anytime you type ls, showing trailing slashes for folders (very handy). Type rb and try it out.

Here are another couple of common aliases you will find:

alias c=clear

This will delete or clear the terminal window.

alias ..="cd .."

If you recall from last time, cd .. is a shortcut for moving back one level in your file hierarchy. Wrapping it into an alias like this simply shortens it a bit and makes moving around your system a bit easier and convenient.

Obviously there are a ton of other things you could put in here to customize your workflow. Here are some of my favorite and most often used ones:


###########
# Aliases #
###########

alias x=exit      #exit current terminal session
alias c=clear      #clear the terminal screen
alias ..="cd .. && ls"      #move back one directory and display contents of new directory
alias topS="top -o cpu"      #show current processes sorted by CPU
alias duS="du -sh"      #show size of PWD
alias findbig="find . -type f -exec ls -s {} \; | sort -n -r | head -5"      #show 5 biggest files in PWD
alias lss="ls |rev|sort|rev|more"      #sort by filetype
alias openbash="open ~/.bash_profile"      #open the .bash_profile file from anywhere
alias rb="source ~/.bash_profile && c"      #source the .bash_profile changes then clear the terminal window
alias ls="c && ls -p"      #clear the terminal window then list content
alias battery="pmset -g batt"      #show battery stats
alias vol="osascript -e 'set volume output volume 70'"      #set system volume using apple script
alias MRscript="cdl /Users/martinritter/Documents/Development/Scripts/bash/"      #go directly to my script locations
alias lc="ls; echo ''; command ls -1 | wc -l; echo ' items'"      #list content and file count
alias rsync="rsync -vazhPm"      #set a bunch of default options for rsync

 

Next time we are going to look at some simple scripting to tap into even more powerful ways to use the terminal.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.