Adding a command (global executable) to linux
Wuru (41)

Hey.

So don't you hate it when you have to type "./" before an executable's name in the linux terminal before it will execute?

Probably not.

Anyway, it can be fun to make it so you dont have to type './' before an executable's name.

It has other benefits too.

For example, if you ever compile with gcc you just type this:

gcc hello.c

And somehow it works right?

Well all those commands like mkdir and cd are just executables that are in a special folder that makes them the way they are.

What is this folder?

The folder

Navigate to /usr/bin/ on your machine (im using kubuntu).

Ok that is the folder.

But how can we do things in here?

Create a file in your home directory called hello.c.

$ touch hello.c

Then open it up with your favorite text editor and type this in it:

#include "stdio.h"

int main() {
        printf("You made your own command!");
        return 0;
}

Then save it.

Then compile with this command.

gcc -o hello hello.c

Then copy the executable it creates to the special folder with this command.

sudo ln -s /path/to/your/exe /usr/local/bin/hello

Note that the folder is /usr/local/bin/ this is the folder for user created binaries whilst /usr/bin/ is for system binaries.
And then if you type

hello

It will run the executable.

Further Development With This

If you run the command

sudo ln -s /path/to/your/file /usr/local/bin/nameOfNewCommand

With the old command nameOfNewCommand was hello.

And there ya go!

Thank you for reading and please consider an upvote. (I just joined repl.it yesterday and I am in love with the tutorials feature!)

You are viewing a single comment. View All
DynamicSquid (4597)

Welcome to repl.it! And awesome first tutorial :)