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
https://git-scm.com/book/en/v2/Git-Basics-Git-Aliases
https://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.