How to Create Custom Commands in Linux

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

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

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

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.

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

usr local bin is one of the paths

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

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!


You don’t have to create custom commands in Linux, if you use one of our fully managed web hosting solutions, you can always ask our technical support to create a custom command line in Linux. They are available 24/7 and will help you any aspect of managing your server.

If you liked this post on how to Create Custom Commands in Linux, please share it with you friends on social media on the buttons below, or if you have any question regarding this blog post, please leave a comment and one of our system administrators will reply to it. Thanks!

2 thoughts on “How to Create Custom Commands in Linux”

  1. Having the current working directory (“.”) in your PATH is a security vulnerability. It’s better to put the script in $HOME/bin, which as shown in the picture, was already in your PATH, so you didn’t even need to edit it. Of course, the particular example in this article would be better implemented as a function than a script anyway.

    Reply

Leave a Comment