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:
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.
Table of Contents
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:
This is called a “script”, and it’s made up of three parts:
- It starts with the words “#!/bin/bash”
- Lines starting with a hashtag (#) are comments
- 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:
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.
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:
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”:
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:
- Manually specify the full location of our script file each time
- Add the “current directory” to the $PATH variable
- 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:
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:
However, this approach has two problems:
- As mentioned, you can’t access the script from any other directory other than its own
- 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:
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:
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.
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!
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!
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.
Adding the file to an existing $PATH is the recommended method, as mentioned in the same article.