This is the right way to create custom commands in Linux. In this screenshot, I’ve moved the script to /usr/local/bin and gone one folder up by “cd.”.
So we just move our custom command to this location (or any other folder in $PATH) via this command: mv files /usr/local/binĪnd now we can change our working folder and access the script from wherever we want! In my opinion, this is the best way to add your custom command so that you can access it from anywhere.įirst, get a list of $PATH locations by echo’ing PATH as shown in Method 2.Įach 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. Method 3: Add the File to an Existing $PATH Destination (Recommended)
However, you still can’t execute the command from any folder. Now the change will persist even when your user session ends. You should see the addition at the end of the line like this: You can check if the changes have taken place by echo’ing the $PATH variable. Do this using the following command: source ~/.bash_profile However, the changes won’t take effect until you reload ~/.bash_profile. Save the file in vi by pressing “Esc” and Shift+z+z like before. That’s a colon (:) followed by a dot (.). Now add the following to the end of the PATH line. To make the changes to $PATH permanent, we need to edit this file: ~/.bash_profileĪs before, we can use the vi editor: vi ~/.bash_profileĪgain, press “Insert” to go into INSERT mode, and navigate to the line specifying the PATH variable as shown here: It’ll be reset when the user’s session is over! As mentioned, you can’t access the script from any other directory other than its own.Now just typing “files” works as shown here: We can temporarily add the current directory to $PATH like this: PATH="$PATH." We can tell Linux to always search the “current directory” for scripts and commands. This method suffers from the same problem as the first one. Method 2: Add the “Current Directory” to the PATH Variable We’d have to specify the fully qualified path, which is a pain. Unfortunately, this won’t work if we’re in some other folder. But we can specify the current directory for our script file like this. When we tried to just type “files” earlier, we got an error.
Let’s see how to do all three: Method 1: Manually Specify the Full Location Move our script file into an existing folder referenced by $PATH.Add the “current directory” to the $PATH variable.Manually specify the full location of our script file each time.And our current directory isn’t on that list. This is because Linux searches for regular commands in a specific set of directories referenced by the $PATH variable. We get an error message that says, “command not found”: Unfortunately, we can’t just type “files” into the current directory to run our new script. It’s time to run our command! Step 3: Specifying Paths to Our Script This changes the file permissions and makes it executable. We do this by typing the following: chmod 755 files So we need to tell Linux that our new file is executable. The “ls” command displays executable files in green. However, it’s just a regular file and not something that can be executed. If we just list the files in the directory, you will see that our new file is one of them. Step 2: Assign the Right Permissions to our Script You can display its contents by typing: cat files The file is now saved in your current folder. Save the file by pressing Shift+Z+Z (Hold the shift key down and press “z” two times”. Now exit “Insert” mode by pressing the “Esc” key. Replace the 3rd line with your complicated command that you want to simplify Lines starting with a hashtag (#) are comments.This is called a “script”, and it’s made up of three parts: #!/bin/bashĪs you can see, this is pasted into vi when the “INSERT” mode is on: You can quickly paste into the terminal by right-clicking inside it. Change this to insert mode by pressing the “Insert” key on your keyboard. By default, it’ll be in “read” mode and you can’t add new text. This will open up a basic editor where you can type your text. We create a new file with the name “files” using the text editor “vi” by issuing this command: vi files Let’s say we want to call our new command “files”. Step 1: Open a File in the Text Editor with your Command Name Step 2: Assign the Right Permissions to our Script.
Step 1: Open a File in the Text Editor with your Command Name.