<\/span><\/h2>\nLet’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:<\/p>\n
vi files<\/pre>\nThis 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.<\/p>\n
Now copy and paste the following text. You can quickly paste into the terminal by right-clicking inside it.<\/p>\n
#!\/bin\/bash\r\n# Print list of files in one column\r\nls -l --color | awk '{ print $9 }'<\/b><\/pre>\n As you can see, this is pasted into vi when the “INSERT” mode is on:<\/p>\n
<\/p>\n
This is called a “script”, and it’s made up of three parts:<\/p>\n
\nIt starts with the words “#!\/bin\/bash”<\/li>\n Lines starting with a hashtag (#) are comments<\/li>\n The rest are commands. Replace the 3rd line with your complicated command that you want to simplify<\/li>\n<\/ol>\nNow 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”.<\/p>\n
The file is now saved in your current folder. You can display its contents by typing:<\/p>\n
cat files<\/pre>\nThis gives the following output:<\/p>\n
<\/p>\n
<\/span>Step 2: Assign the Right Permissions to our Script<\/span><\/h2>\nIf we just list the files in the directory, you will see that our new file is one of them.<\/p>\n
<\/p>\n
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:<\/p>\n
chmod 755 files<\/pre>\nThis changes the file permissions and makes it executable. Now “ls” shows the file as green:<\/p>\n
<\/p>\n
It’s time to run our command!<\/p>\n
<\/span>Step 3: Specifying Paths to Our Script<\/span><\/h2>\nUnfortunately, 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”:<\/p>\n
<\/p>\n
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.<\/p>\n
So we have three choices:<\/p>\n
\nManually specify the full location of our script file each time<\/li>\n Add the “current directory” to the $PATH variable<\/li>\n Move our script file into an existing folder referenced by $PATH<\/li>\n<\/ol>\nLet’s see how to do all three:<\/p>\n
Method 1: Manually Specify the Full Location<\/h3>\n 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:<\/p>\n
.\/files<\/pre>\nAnd this works as you can see below:<\/p>\n
<\/p>\n
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.<\/p>\n
Method 2: Add the “Current Directory” to the PATH Variable<\/h3>\n 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:<\/p>\n
PATH=\"$PATH:.\"<\/pre>\nNow just typing “files” works as shown here:<\/p>\n
<\/p>\n
However, this approach has two problems:<\/p>\n
\nAs mentioned, you can’t access the script from any other directory other than its own<\/li>\n The change in $PATH is temporary. It’ll be reset when the user’s session is over!<\/li>\n<\/ol>\nTo make the changes to $PATH permanent, we need to edit this file:<\/p>\n
~\/.bash_profile<\/pre>\nAs before, we can use the vi editor:<\/p>\n
vi ~\/.bash_profile<\/pre>\nAgain, press “Insert” to go into INSERT mode, and navigate to the line specifying the PATH variable as shown here:<\/p>\n
<\/p>\n
Now add the following to the end of the PATH line:<\/p>\n
:.<\/pre>\nThat’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:<\/p>\n
source ~\/.bash_profile<\/pre>\nYou 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:<\/p>\n
<\/p>\n
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.<\/p>\n
Method 3: Add the File to an Existing $PATH Destination (Recommended)<\/h3>\n In my opinion, this is the best way to add your custom command so that you can access it from anywhere.<\/p>\n
First, get a list of $PATH locations by echo’ing PATH as shown in Method 2.<\/p>\n
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.<\/p>\n
<\/p>\n
So we just move our custom command to this location (or any other folder in $PATH) via this command:<\/p>\n
mv files \/usr\/local\/bin<\/pre>\nAnd now we can change our working folder and access the script from wherever we want!<\/p>\n
<\/p>\n
In this screenshot, I’ve moved the script to \/usr\/local\/bin and gone one folder up by “cd..”. And the command still works!<\/p>\n
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.<\/p>\n
Happy scripting!<\/p>\n
\nYou 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.<\/p>\n
If you liked this post on how to\u00a0Create 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!<\/p>\n","protected":false},"excerpt":{"rendered":"
In this tutorial, We’ll show you how to create custom commands in Linux which will allow you to create “shortcut” … <\/p>\n
Read More<\/a><\/p>\n","protected":false},"author":4,"featured_media":27328,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1236,13],"tags":[1725,27],"yoast_head":"\nHow to Create Custom Commands in Linux - RoseHosting<\/title>\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\t \n\t \n\t \n \n \n \n \n \n\t \n\t \n\t \n