How to Create Bash Aliases

Creating aliases in bash is very straight forward

The syntax is as follows:

alias alias_name="command_to_run"

Aliases are different names for the same command

Probably one of the most commonly used commands I use on the Linux command line is the cd command. I want to create an essential shortcut to save me time from having to type long commands and eliminate a great deal of typing. I will set a memorable shortcut command for a longer command.

I will create a simple bash alias named dir for navigating this very long directory structure

This will be a shortcut for this long command

cd /var/very/long/directory/structure/that/is/too/lengthy

Open the file in your text editor

vi ~/.bashrc

I will declare the following in the ~/.bashrc file

alias dir="cd /var/very/long/directory/structure/that/is/too/lengthy"

Once done, save and close your file

:wq

Type the following to make the alias available in your current session

source ~/.bashrc

You can combine multiple commands in an alias by using &&

For example:

alias dir="cd /var/very/long/directory/structure/that/is/too/lengthy && ls -la"

Gives you a long listing of all files including hidden files

alias ll="ls -la"

Edit your Apache Default Configuration File

If you're customizing your files frequently, you might want an alias to edit the file. You can use conf (httpd.conf) as an alias to call your editor of choice on the file you prefer.

alias conf="vim /etc/httpd/conf/httpd.conf"

Clear the terminal screen

alias c="clear"

Display disk space in human readable format and exclude directories that we don't care to see in our output

alias df="df -h -x tmpfs -x devtmpfs"

The ports alias

This alias is used when we need information about ports that services are running on. 

alias ports="netstat -tunlp"

Navigate up directory levels

alias ..="cd .."
alias ..2="cd ../.."
alias ..3="cd ../../.."
alias ..4="cd ../../../.."
alias ..5="cd ../../../../.."

You can list all of your configured aliases by passing the alias command without any arguments:

alias

For information on how to add alias commands in Git

http://git-scm.com/book/en/v2/Git-Basics-Git-Aliases

http://stackoverflow.com/questions/2553786/how-do-i-alias-commands-in-git

By using aliases, you can save a lot of time by reducing the typing needed for complex or frequently used long commands.

Learn

Related articles

Getting Started with React

In this guide, we will cover the basics of setting up a local development environment, starting a simple React project, and the basics of how it works.

Using Git for PHP Development

This guide walks through the fundamentals of Git. In this tutorial, we will show you Git commands. For Linux you can use the built-in terminal.

How to Connect to MySQL with Laravel

In this guide, you will learn how to connect your Laravel application to your MySQL database.

How do you change the default SSH Port on CentOS Stream 9?

Changing the default SSH port adds an extra layer of security by reducing the risk of your password being cracked from a brute force attack.

What is Inheritance in PHP?

In this tutorial we will explain inheritance in PHP, a mechanism that allows a child class to inherit properties and behaviors from a parent class.

How to Allow Remote Access to MySQL

In this guide, we will show you how to how to setup a user account and access a MySQL server remotely on a Linux system.