Create Custom Commands in Ubuntu
Save time by converting frequently used command and option combinations as custom command.
β Sagar Sharma
As a Linux user, I can relate to your urge to create a custom command that is specific to your needs.
Those commands could be anything from combining different commands to a script containing multiple arguments.
So in this tutorial, I will walk you through the following methods and both will include steps for the local user and using them globally:
- Using alias
- Using a bash script
Let's start with the first one.
How to create custom commands using an alias
To create a temporary alias (for the current session only), you can use the following command syntax:
alias short_name="your custom command here"
For example, here, I created an alias to update repositories and install new packages named update
:
alias update="sudo apt update && sudo apt upgrade -y"
When executed, it gave me the following output:
=
while using the alias command.But as I said, it will only work for the current session. This means the reboot will wipe down the alias.
So if you want to make the alias permanent, here's how you do it.
How to create permanent custom commands using an alias
If you want to create a permanent alias for the currently logged-in user, you have to add the alias to the bashrc file.
First, open the bashrc file using the following command:
nano ~/.bashrc
Now, go to the end of the line in the nano text editor by pressing Alt + /
and enter the alias for the command by following the given syntax:
alias custom_command='enter one or more commands with different flags'
For example, here, I created an alias for the ls
command which will automatically execute ls -lah
:
alias ls='ls -lah'
Save changes and exit from the nano text editor.
To take effect from the changes you've made, source the bashrc file using the following command:
source ~/.bashrc
And here's what it gave me when I executed the ls
command:
How to create custom commands for all users
If you want to create custom commands for all the users on the system, you have to make changes to the /etc/bash.bashrc
file.
So the first step would be to open the file:
sudo nano /etc/bash.bashrc
Now, press Alt + /
to go to the end of the file and add the alias by following the given command syntax:
alias custom_command='enter one or more commands with different flags'
For your reference, here I created two aliases to open and edit the bashrc file:
# Edit your .bashrc file
alias ebashrc='nano ~/.bashrc'
# Source your .bashrc file to apply changes
alias rbashrc='source ~/.bashrc'
Save changes and exit from the nano text editor.
Finally, source the bashrc file using the following:
source /etc/bash.bashrc
Now, you can use the custom commands systemwide.
How to create custom commands using scripts
For those who don't know, every command you have on Linux is a script itself so don't hesitate to create scripts for creating custom commands.
The first step is to create a directory where you will store the scripts that you later will be using as commands.
To create a directory, use the mkdir command in the following manner:
mkdir /path/to/directory
I created a directory inside the /usr/local/bin/
directory so I will need superuser privileges (as it is in the root directory):
sudo mkdir /usr/local/bin/scripts
Now, create a script inside the recently created directory:
.sh
extension to have a similar execution syntax as normal commands.nano /path/to/directory/to/store/script
For example, here, I created a script named update
:
sudo nano /usr/local/bin/scripts/update
Which contains the following:
#!/bin/bash
sudo apt update && sudo apt upgrade -y
Sure, you can enter multiple arguments, and multiple commands with different arguments.
Once you are done, save changes and exit from the text editor.
Use the chmod command to make your file executable:
chmod +x /path/to/file
My script is inside the /usr/local/bin/scripts
directory which simply means I have to use sudo
with the chmod command:
sudo chmod +x /usr/local/bin/scripts/update
Next, text the script by executing it:
./script_name
If everything goes smoothly, in the next step, you can add this path to your bashrc file so you can use this command from everywhere without giving path.
First, change your directory to where the script is stored:
cd /path/to/directory
Now, use the pwd command to know the absolute path of the directory:
pwd
Now, copy this path (the given output by the pwd command).
Use this command for a logged-in user only
If you want to use the script as a command for the currently logged-in user, then you have to insert the PATH variable in the ~/.bashrc
file.
First, open the ~/.bashrc
file:
nano ~/.bashrc
Go to the end of the file and add a line of the PATH variable in the following manner:
export PATH=$PATH:/path/to/script/directory
Make sure you change the /path/to/script/directory
Β part with yours (output given by the pwd command).
For example, if you stored my script inside the /home/sagar/scripts
directory, then, my command would look like this:
export PATH=$PATH:/home/sagar/scripts
Save changes, exit from the text editor, and source the bashrc file to take effect from the changes you've made:
source ~/.bashrc
Now, you can use the script from anywhere and it will behave like normal command:
Use this command globally (for every user)
If you want this custom command accessible to every user on the system, you have to enter the PATH variable in the /etc/bash.bashrc
file.
First, open the /etc/bash.bashrc
file using the following command:
sudo nano /etc/bash.bashrc
Go to the end of the file using Alt + /
and add the line of the PATH variable as shown here:
export PATH=$PATH:/output/of/pwd/command
In my case, the command would look like this:
export PATH=$PATH:/usr/local/bin/scripts
Once done, save changes and exit from the text editor.
To enable changes you've made, source the /etc/bash.bashrc
file using the following command:
sudo source /etc/bash.bashrc
That's it! From now on, every user on the system can use the custom command that you've made.
For example, here I've executed the update command as a root user from the root directory:
Wrapping Up...
This was a detailed guide on how you can create a custom command using two different methods and both includes ways for local and global users.
I hope you will find this guide helpful.
A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.