LINEX

What are linux commands

by Admin

You might have used different linux commands like ls(to list files and directories), rm(to remove a file or directory), touch(to create a file), mkdir(to create a directory) etc. but have you ever wondered what are these commands and how do they work?

These are actually small programs which are stored as binaries in the /bin or usr/bin directories. If you type ls bin or ls /usr/bin command on your terminal you will be able to see list of all the binaries.

Note: when we compile a program it generates a binary or executable file.

Now question is can you also create such commands and use them the answer is of-course yes. You can create your own commands and customize them to perform specific task.

Lets write a small c++ program command.cpp to print a message and use its binary as a command.

#include<bits/stdc++.h>

using namespace std;

int main()
{
    cout<<"This is a test command :)"<<endl;
}

Now compile it using gcc compiler and give some name to the binary file instead of default i.e. a.out. I am giving binary file name xz and will refer it as command.

g++ command.cpp -o xz

Now copy the xz to /usr/bin directory using below command.

sudo cp xz /usr/bin/ 

It will copy the xz binary to /usr/bin and you can confirm using ls command. Now if you type xz in your terminal the custom message will be printed on the terminal.

Similarly you can write your own program and use them as a command.

Related Posts

Leave a Comment