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:
- First, we create a script for our command
- Then make it executable once we make it executable we
- 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.
Comments ( 0 )
No comments available