How to create custom commands in Linux

In this article, we will show you how to create custom commands in Linux that will allow you to create “shortcut” commands using a simple name of your choice. Better yet, you can combine multiple commands like this and run them all in one word. Useful, right? As you become more familiar with Linux, you will come across commands in forums and tutorials that can be very complex (and weird). Consider the following example to print a list of files in one column:

ls -l --color | awk '{print $ 9}'

This gives us the following output:

So far, so good. But what if you want to use this command often? It is difficult to remember this letter to write, it is not easy to print, and it is too lengthy. Now add dozens of other teams with similar (or greater) difficulty levels, and it’s easy to see that we need a new solution.

Step 1. Open the file in a text editor using the name files

Let’s say we want to name our new command “files”. We create a new file named “files” using the text editor “vi” by issuing the following command:

vi files

This command will open a basic editor where you can enter text. By default, it will be in “reading” mode and you will not be able to add new text. Change this to insert the mode by pressing the “Insert” key on your keyboard.

Now copy and paste the following text. You can quickly paste it into the terminal by right clicking it.

#!/bin/bash
# Print list of files in one column
ls -l --color | awk '{ print $9 }'

As you can see, this is inserted into vi when INSERT mode is on:

How to create custom commands in Linux

This is called a “script” and it has three parts:

  1. It starts with the words “#! / Bin / bash”
  2. Lines starting with hashtag (#) are comments
  3. The rest of the teams. Replace the third line with a complex command that you want to simplify

Now exit the “Insert” mode by pressing the “Esc” key. Save the file by pressing Shift + Z + Z (Hold the shift key and press z twice).

The file will now be saved in the current folder. You can display its contents by entering:

cat files

This gives the following output:

How to create custom commands in Linux

Step 2: Assign the correct permissions for our script

If we just list the files in the directory, you can see that our new file is one of them.

How to create custom commands in Linux

However, this is a regular file and not one that can be executed. The ls command displays executable files in green. Therefore, we need to tell Linux that our new file is executable. We do this by introducing the following:

chmod 755 files

This changes the file permissions and makes them executable. Now “ls” shows the file as green:

How to create custom commands in Linux

It’s time to launch our team!

Step 3: Specifying the Paths to Our Script

Unfortunately, we cannot just type “files” in the current directory to run our new script. An error message appears: “command not found”:

How to create custom commands in Linux

This is because Linux looks for regular commands in a specific set of directories referenced by the $ PATH variable. And our current directory is not on this list.

So we have three options:

  1. Manually provide the complete location of our script file each time
  2. Add “current directory” to your $ PATH variable
  3. Move our script file to the existing folder referenced by $ PATH

Let’s see how to do all three:

Method 1. Enter the complete location manually

When we tried to just type “files” earlier, we got an error message. But we can specify the current directory for our script file as follows:

./files

And it works as you can see below:

How to create custom commands in Linux

Unfortunately it won’t work if we’re in a different folder. We have to show the full path, which is pain.

Method 2. Add “Current directory” to the PATH variable

This method has the same problem as the first one. We can say that Linux always looks for the “current directory” for scripts and commands. This way we don’t have to use “./”. We can temporarily add the current directory to $ PATH like this:

PATH="$PATH:."

Now just by typing “files” as shown here:

How to create custom commands in Linux

However, this approach has two problems:

  1. As mentioned, you cannot access the script from any directory other than your own
  2. The change in $ PATH is temporary. It will be reset when the user’s session is over!

To make the changes to $ PATH permanent, we need to edit this file:

~/.bash_profile

As before, we can use the vi editor:

vi ~/.bash_profile

Press Paste again to enter INSERT mode and navigate to the line with the PATH variable, as shown below:

How to create custom commands in Linux

Now add the following to the end of your PATH line:

:.

This is a colon (smilefollowed by a period (.). Save the file in vi by pressing Esc and Shift + z + z as before. However, the changes will not take effect until ~ / .bash_profile is reloaded. Do it using the following command:

source ~/.bash_profile

You can check if the change has occurred by calling the $ PATH variable. You should see an addition at the end of the line like this:

How to create custom commands in Linux

The change will now persist even after the user’s session ends. However, you still cannot execute the command from any folder. For this we need method 3.

Method 3: add the file to your existing $ PATH destination (recommended)

In my opinion, this is the best way to add your custom command so that you can access it from anywhere.

First, get a list of $ PATH locations using echo $ PATH as shown in Method 2.

Each folder location is separated by a colon (smile You can see in the screenshot above that the / usr / local / bin folder is one of the paths.

How to create custom commands in Linux

Therefore, we will simply move our custom command to this location (or any other folder in $ PATH) with this command:

mv files/usr/local/bin

And now we can change our working folder and access the script from anywhere!

How to create custom commands in Linux

In this screenshot, I’ve moved the script to / usr / local / bin and moved one folder to “cd ..”. And the team is still working!

This is the correct way to create custom commands in Linux. This is just the tip of the iceberg when it comes to scripting in Linux. You can write complex logic loops and execute conditional statements. You can also use this to create aliases for existing commands, or combine them together for automatic execution! If this is your first foray into Linux scripting, be prepared to access a wealth of power and functionality in the future.

Happy scripts!

Sidebar