How to Create Custom Commands in Linux

 

In this tutorial, We’ll show you how to create custom commands in Linux which will allow you to create “shortcut” commands using a simple name of your choosing. Even better, you can chain multiple commands like this together and run them all with a single word. Useful, right? As you get more and more acquainted with Linux, you’ll come across commands in forums and tutorials that can be really complex (and weird). Consider the following example to print the list of files in a single column:

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

This gives us the following output:

Output of awk command

So far so good. But what if you want to use this command frequently? It’s difficult to remember this letter for letter, it’s not easy to type out, and it takes way too long. Now add dozens of other commands with similar (or greater) levels of complexity, and it’s easy to see that we need a new solution.

Step 1: Open a File in the Text Editor with your Command Name

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

vi files

This will open up a basic editor where you can type your text. By default, it’ll be in “read” mode and you can’t add new text. Change this to insert mode by pressing the “Insert” key on your keyboard.

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

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

As you can see, this is pasted into vi when the “INSERT” mode is on:

Copy and Paste the script into vi

This is called a “script”, and it’s made up of three parts:

  1. It starts with the words “#!/bin/bash”
  2. Lines starting with a hashtag (#) are comments
  3. The rest are commands. Replace the 3rd line with your complicated command that you want to simplify

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

The file is now saved in your current folder. You can display its contents by typing:

cat files

This gives the following output:

Display the contents of our file

Step 2: Assign the Right Permissions to our Script

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

just-a-regular-file-8693210

However, it’s just a regular file and not something that can be executed. The “ls” command displays executable files in green. So we need to tell Linux that our new file is executable. We do this by typing the following:

chmod 755 files

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

Change script file permissions

It’s time to run our command!

Step 3: Specifying Paths to Our Script

Unfortunately, we can’t just type “files” into the current directory to run our new script. We get an error message that says, “command not found”:

command-not-found-6501285

This is because Linux searches for regular commands in a specific set of directories referenced by the $PATH variable. And our current directory isn’t on that list.

So we have three choices:

  1. Manually specify the full location of our script file each time
  2. Add the “current directory” to the $PATH variable
  3. Move our script file into an existing folder referenced by $PATH

Let’s see how to do all three:

Method 1: Manually Specify the Full Location

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

./files

And this works as you can see below:

Manually specify the current directory

Unfortunately, this won’t work if we’re in some other folder. We’d have to specify the fully qualified path, which is a pain.

Method 2: Add the “Current Directory” to the PATH Variable

This method suffers from the same problem as the first one. We can tell Linux to always search the “current directory” for scripts and commands. That way, we won’t have to use “./”. We can temporarily add the current directory to $PATH like this:

PATH="$PATH:."

Now just typing “files” works as shown here:

adding-the-current-directory-to-path-4134569

However, this approach has two problems:

  1. As mentioned, you can’t access the script from any other directory other than its own
  2. The change in $PATH is temporary. It’ll 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

Again, press “Insert” to go into INSERT mode, and navigate to the line specifying the PATH variable as shown here:

Navigate to the PATH variable declaration

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

:.

That’s a colon (:) followed by a dot (.). Save the file in vi by pressing “Esc” and Shift+z+z like before. However, the changes won’t take effect until you reload ~/.bash_profile . Do this using the following command:

source ~/.bash_profile

You can check if the changes have taken place by echo’ing the $PATH variable. You should see the addition at the end of the line like this:

Add Current Path to PATH

Now the change will persist even when your user session ends. However, you still can’t execute the command from any folder. For that, we need Method 3.

Method 3: Add the File to an 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 by echo’ing PATH as shown in Method 2.

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

usr-local-bin-is-one-of-the-paths-3166387

So we just move our custom command to this location (or any other folder in $PATH) via this command:

mv files /usr/local/bin

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

custom-command-working-from-new-location-2353973

In this screenshot, I’ve moved the script to /usr/local/bin and gone one folder up by “cd..”. And the command still works!

This is the right way to create custom commands in Linux. It’s 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 chain a bunch of 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 scripting!

Original Article