Create custom command line commands for scripts 05-20-2013, 02:59 PM
#1
Source: My blog
When writing your own or download scripts and you want to run them from the command line it can be a hassle to always have to be in the correct folder and write ./filename etc.
It is much easier to run the scripts using the same method as the rest of the Linux commands. In this article I will show you how easy that can be.
The original files can live anywhere really, but I like to keep them in the /opt folder and this is what I will be using in this tutorial
Part 1 – Create a folder to house the custom scripts
Step 1: Create a customscripts folder inside /opt
Step 2: Copy the script(s) to our newly created folder
Step 3: Make the file(s) executable
When this has been done we need to create the command that we will use to trigger the script. To do this we need to add a symbolic link (symlink) to the /usr/bin folder
Part 2 – Create the command
Step 1: Change the current working directory to /usr/bin
Step 2: Create the command / symlink
The command is normally the same as the filename, unless something else is more suitable. For example if the executable file is named something like executable or execute it would make more sense to name it after the application name
Step 3: Verify the command / symlink
If this show that the command is executable, i.e lrwxrwxrwx, you’re all good and you can now execute this script from anywhere in your system.
To try it out, navigate to anywhere and try running the script using the command you just created in /usr/bin
Example
I have made a couple of python scripts, and one of them is named splitfile.py, you can find the code here. Here’s the approach to make it executable from anywhere in my system
I can now use the following approach to execute this script …
… instead of …
When writing your own or download scripts and you want to run them from the command line it can be a hassle to always have to be in the correct folder and write ./filename etc.
It is much easier to run the scripts using the same method as the rest of the Linux commands. In this article I will show you how easy that can be.
The original files can live anywhere really, but I like to keep them in the /opt folder and this is what I will be using in this tutorial
Part 1 – Create a folder to house the custom scripts
Step 1: Create a customscripts folder inside /opt
Code:
$ mkdir /opt/customscriptsStep 2: Copy the script(s) to our newly created folder
Code:
$ cp /path/to/script/filename /opt/customscripts/filenameStep 3: Make the file(s) executable
Code:
$ chmod +x /opt/customscripts/filenameWhen this has been done we need to create the command that we will use to trigger the script. To do this we need to add a symbolic link (symlink) to the /usr/bin folder
Part 2 – Create the command
Step 1: Change the current working directory to /usr/bin
Code:
$ cd /usr/binStep 2: Create the command / symlink
The command is normally the same as the filename, unless something else is more suitable. For example if the executable file is named something like executable or execute it would make more sense to name it after the application name
Code:
$ ln -s /opt/customscripts/filename commandStep 3: Verify the command / symlink
Code:
$ ls -l | grep commandIf this show that the command is executable, i.e lrwxrwxrwx, you’re all good and you can now execute this script from anywhere in your system.
To try it out, navigate to anywhere and try running the script using the command you just created in /usr/bin
Example
I have made a couple of python scripts, and one of them is named splitfile.py, you can find the code here. Here’s the approach to make it executable from anywhere in my system
Code:
$ mkdir /opt/customscripts
$ cp /path/to/splitfile.py /opt/customscripts
$ chmod +x /opt/customscripts/splitfile.py
$ cd /usr/bin
$ ln -s /opt/customscripts/splitfile.py splitfileI can now use the following approach to execute this script …
Code:
$ splitfile /path/to/file chunksize… instead of …
Code:
$ cd /path/to/splitfile.py
$ ./splitfile.py /path/to/textfile chunksize
or
$ python splitfile /path/to/textfile chunksize



![[+]](https://sinister.li/images/modern/collapse_collapsed.png)
![[Image: 2YpkRjy.png]](http://i.imgur.com/2YpkRjy.png)
Will do that on my system for that folder, and use the method from the tutorial on those not living in the customscripts folder