How to Create a Permanent Bash alias on Linux/Unix?

Rashmi Bhardwaj | Blog,Programming & Software
Advertisements

Aliases is a convenient way to execute commands in Linux and Unix. At times it is difficult to remember complete syntax of complex commands where more than one argument needs to be supplied, complex conditional statements where aliases come handy to execute commands in a faster and more accurate manner. Aliases can be created and used temporarily or permanently based on the requirement. 

In today’s blog we will cover how to create a permanent bash alias on Linux and Unix systems. 

Create a Permanent Bash alias

Create Permanent Bash alias on Linux and Unix 

Many users like to use shortcuts in running commands. There are many commands which we use on a regular basis in Linux and Unix systems. Creation of an alias for use on the command line can save effort of typing long commands on CLI, common options, or typos. This saves time by reducing effort on repetitive keystrokes. There are certain prerequisites to be taken into consideration before creating permanent bash alias on Linux and Unix.

Advertisements

  • Any Linux or Unix distribution 
  • Privileged access to Linux or Unix system as root or via a Sudo command
  • Any editor 

To create a permanent bash alias, we need to edit the ~/.bashrc . This file can be opened using any text-based editor. 

$nano ~/.bashrc

At the bottom of this file, we can add permanent alias. For example, we will create a simple alias test which executes the command cd ~/home/linuxconfig/Documents/.

alias test =’cd /home/linuxconfig/Documents/’

Post saving the change to existing file execute the following command 

$source ~/.bashrc 

Newly configured alias can be used in the command line 

$test 

To check configured aliases, along with new one executes below command 

$alias -p 

 

Another examples

Example 1 

Suppose you want to create an alias of mkdir command with alias name ‘m’ and use it permanently. 

Open ~/.bashrc file in any text editor and add alias command in the file save and run it 

$nano ~/.bashrc

$alias M=”mkdir”

Re-execution of file makes the command active

$ source ~/.bashrc

$m Testdirectory

$ls 

 

Example 2

Use alias for ‘cd’ command 

The CD command is used to change the current working directory. Run the following command to create alias for ‘CD’ and test the created alias command.

$alias C=’cd..’

$C 

The CD.. command set the directory two level up from current directory 

 

Example 3 

Use alias for root privilege

The sudo-i command is used to set root privilege. The way to create alias for sudo -i command 

$alias ad=’sudo-i’ 

 

Example 4

Use alias to count the total number of files in a current directory. This one count files, ignoring directories, but not the contents of directories.

$ls 

$alias files_count = ‘find. -type f | wc -1’

 

Example 5

Find a command in Grep history

Search I bash history can be done using CTRL+R to run a reverse search through history instead you can use below alias

$alias gr=’history|grep’ 

You can remove bash alias using unalias command 

$alias d=’date’

$d

$unalias d

$d

The alias command is included in several shells, including ksh, tcsh/csh, ash, bash functions, and others. The /etc/bashrc file can be used to create system-wide aliases (i.e., aliases for all users). 

Continue Reading:

How to Create and Use Alias Command in Linux

What Is The Difference Between UNIX and LINUX?

Are you Preparing for your Next Job Interview?

If you want to learn more about Linux, then check our e-book on Linux Interview Questions and Answers in easy to understand PDF Format explained with relevant Diagrams (where required) for better ease of understanding.

ABOUT THE AUTHOR


Leave a Comment

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

Shopping Cart