Linux operating system allows users to create commands and execute them over the command line. To create a command in Linux,the first step is to create a bash script for the command. The second step is to make the command executable.The script can be run only after file permissions have been changed to executable mode. Once changed, copy it to the binary path of Linux so that it can be run just like system-defined commands of operating systems. Follow these easy steps to create a command in Linux.
Step 1:- First step to create bash file which will run your command on command line. Open vim or any editor, and type following code on file,
[root@server ashutosh]# vim file-name
echo "this is my first chance to create my own command'"
#then the rest of the files
echo "files of current directory are"
:wq (Save & Quit )
Step2:- Now, you have to make this file executable. To make it executable use chmod command with option +x. CHMOD command is basically used to give access permission of files to user accounts. For more information about chmod command, open your terminal and type man chmod, you will get all information about chmod.
(Example for making file executable)
[root@server ashutosh]# chmod +x filel-name
Step3:- Now you have to copy this executable file to binary path of Linux that is “/usr/bin/”.
[root@server ashutosh]# cp -rvf file-name /usr/bin
Sometimes for sudo (super user do) account user, it will not give you permission to copy this file to binary path of Linux. So to solve this we have to switch our account to root account, for this just type su command on terminal, it will asked you the current account password (only if you don’t created root account else you have to give root account password). After switching to root account copy your file into binary path that is “/usr/bin/”.
Now, try to execute your command, you can modify bash scripting file as per your requirement but you should have some basic knowledge of shell scripting to make perfect command that you want to create.
[root@server ashutosh]# file-name