How to Create an Alias in Mac or Linux

Tejaksha K
3 min readSep 19, 2024

--

Aliases are a fantastic way to simplify your workflow in the terminal. They allow you to create shortcuts for commonly used commands, making your experience more efficient. Whether you’re on a Mac or a Linux machine, creating aliases is easy and can save you a lot of time.

In this blog, we’ll walk through the steps to create an alias in your terminal on Mac or Linux.

What is an Alias?

An alias is a custom shortcut or name for a command (or series of commands) in your shell. Instead of typing long or complex commands every time, you can simply use your alias.

For example, instead of typing:

ps aux | grep something

You could create an alias, like:

alias searchproc="ps aux | grep"

This way, you can just type searchproc something and get the same result.

Steps to Create an Alias

Here are the steps to create a permanent alias on Mac or Linux:

1. Open the Terminal

First, open the terminal on your machine. You can do this by searching for “Terminal” in your applications menu or using a keyboard shortcut (Cmd + Space for Spotlight search on Mac, then type "Terminal" and hit Enter).

2. Open the Shell Configuration File

To make your alias permanent, you need to add it to your shell’s configuration file. For most users, this will be either ~/.bashrc (for Linux and macOS if you’re using Bash) or ~/.zshrc (on macOS, which uses Zsh by default for newer versions).

Open your configuration file using a text editor, like nano. For example, to edit your .bashrc file, use the following command:

nano ~/.bashrc

Or if you’re using Zsh:

nano ~/.zshrc

3. Create Your Alias

Now, you can add the alias you want at the bottom of the file. Use the alias keyword followed by your custom name and the command you want to shorten. For example:

alias findhistory="ps aux | grep apache"

In this case, the alias findhistory will search for the process apache.

4. Save and Exit

After you’ve added your alias, save the file and exit. If you’re using nano, you can save by pressing Ctrl + O, then press Enter. To exit, press Ctrl + X.

5. Apply the Changes

To make the changes take effect, you need to reload your shell configuration file. Run the following command:

source ~/.bashrc

Or for Zsh:

source ~/.zshrc

6. Test Your Alias

Now that you’ve added the alias, try it out! Simply type your alias name in the terminal and see if it works as expected.

findhistory

This should run the command you aliased, in this case, it will search for the process findhistory using ps aux | grep.

Making Aliases Even More Powerful

You can use aliases for a wide range of purposes, such as:

  • Alias a command with options: Shorten a command with long options, like ls -al.
alias ll="ls -al"
  • Create aliases for navigation: Quickly navigate to frequently used directories.
alias docs="cd ~/Documents"
  • Combine commands: Chain multiple commands together in one alias.
alias cleanup="rm -rf ~/Downloads/* && rm -rf ~/.cache/*"

Conclusion

Creating aliases in Mac or Linux is an easy way to simplify your command-line workflow. With just a few steps, you can create powerful shortcuts that can save you time and effort. Now, you can streamline your terminal experience with custom commands that suit your workflow!

--

--

Tejaksha K
Tejaksha K

Written by Tejaksha K

I'm a Full Stack Developer & Cloud Expert with experience in Google Cloud Platform & AWS.

No responses yet