How to Create a Own customized Command in Linux

To Create an Own customized Command in Linux 

In this tutorial, we will see how to create an own command in Linux and also execute it from a terminal. Here I am using a Linuxmint 18.3, a Debian based distro.


The steps are simple and they are listed below: 

  1. First, we create a script for our command
  2. Then make it executable once we make it executable we 
  3. Make it run from the terminal

Creating script

Let’ s consider that I want to create my own customized command for checking the disk space on my machine for which we usually use df -h  command and I am going to create a command called “ myspace

linuxhelp ~ # df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            967M     0  967M   0% /dev
tmpfs           198M  8.8M  190M   5% /run
/dev/sda1        18G  6.1G   11G  37% /
tmpfs           990M  232K  990M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           990M     0  990M   0% /sys/fs/cgroup
cgmfs           100K     0  100K   0% /run/cgmanager/fs
tmpfs           198M   40K  198M   1% /run/user/1000

Let' s check with the command that we are going to create. 

linuxhelp ~ # myspace
myspace: command not found

It says command not found so let’ s create a file named “ myspace” and insert a command which we want to execute from terminal.

linuxhelp ~ # vim myspace
df -h

Then, make it an executable file.

linuxhelp ~ # chmod 777 myspace

Let’ s execute and see the output.

linuxhelp ~ # ./myspace 
Filesystem      Size  Used Avail Use% Mounted on
udev            967M     0  967M   0% /dev
tmpfs           198M  8.8M  190M   5% /run
/dev/sda1        18G  6.1G   11G  37% /
tmpfs           990M  232K  990M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           990M     0  990M   0% /sys/fs/cgroup
cgmfs           100K     0  100K   0% /run/cgmanager/fs
tmpfs           198M   40K  198M   1% /run/user/1000

Now the file becomes executable and gives us desired output so let' s check running that command now.

linuxhelp ~ # myspace
myspace: command not found

It says command not found so we going to copy that file to “ /usr/bin/” directory to make it a global command. 

Usually, the bin directory is where the essential binary files and also files for booting present it contains the shell like bash and commonly used commands so once we added the command that we created to the bin directory it will available permanently for our machine.

linuxhelp ~ # cp myspace /usr/bin/

Now, try running the command again.

linuxhelp ~ # myspace 
Filesystem      Size  Used Avail Use% Mounted on
udev            967M     0  967M   0% /dev
tmpfs           198M  8.8M  190M   5% /run
/dev/sda1        18G  6.1G   11G  37% /
tmpfs           990M  232K  990M   1% /dev/shm
tmpfs           5.0M  4.0K  5.0M   1% /run/lock
tmpfs           990M     0  990M   0% /sys/fs/cgroup
cgmfs           100K     0  100K   0% /run/cgmanager/fs
tmpfs           198M   40K  198M   1% /run/user/1000

Now we have got the desired output so this is how we create our customized command on Linux. with this, the method to  Create an Own customized Command in Linux comes to the  end.
 

FAQ
Q
How to create my own terminal commands?
A
You can add comments to Bash scripts by beginning a line with #.
When you're done, add this exact line below as first line at the top of your script. It's called a "shebang" and tells the shell with which interpreter to execute your script.
#!/bin/bash
Q
Where all Unix commands re-written in Linux?
A
It appears that you are confusing two very different parts of the OS. It's understandable, because they are often referred to interchangeably, but it's technically incorrect, so your question is based on a faulty premise.
Q
How to create custom commands in Unix/Linux?
A
Create a bash script in your "/usr/bin folder" it should look something like this,
#!/bin/bash
Whatever combination of commands you want to run when you type this thing.
Q
How to create an alias for certain command in Linux?
A
For creating the alias for certain command in Linux, follow the steps as below,
1. open a file ".bashrc"
"nano .bashrc "
2. example for adding an alias into the .bashrc file
"some more ls aliases
alias ll='ls -l'
alias la='ls -A'
alias l='ls -CF'"
Q
Can we create multiple commands for single execution?
A
Yes you can create multiple commands for single execution.