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 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.
- 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.
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
Newly configured alias can be used in the command line
To check configured aliases, along with new one executes below command
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
Re-execution of file makes the command active
$ source ~/.bashrc
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.
The CD.. command set the directory two level up from current directory
Use alias for root privilege
The sudo-i command is used to set root privilege. The way to create alias for sudo -i command
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.
$alias files_count = ‘find. -type f | wc -1’
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
You can remove bash alias using unalias command
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).
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.