Create custom commands simply and quickly


Are you tired of trying, unsuccessfully, to memorize those huge commands? Or searching through folders and websites in search of that script that “every now and then” you need to run? Your problems are over! Now create a custom command in a simple way and with a few steps.

Custom controls can be a “hand on the wheel” to gain those precious seconds and increase productivity. In addition to being easier to remember, since you can choose the word you want to be used as a command, it also saves us time and trouble searching through folders or the internet behind that script that we need to use with some frequency.

We have already published a blog here article teaching you how to create custom commands, the so-called “alias”, which shows a different method to arrive at a similar, but not the same, result, so that both articles will complement each other. So to get a more complete knowledge on the subject, it is strongly recommended that you read both.

Without further ado, let’s get straight to the point!

The procedure can be performed with any command or script you wish. As an example, I will use the command below, which shows the frequency in real time of all processor cores.

watch grep "cpu MHz" /proc/cpuinfo

It’s a little long command, and it would be much simpler if I could type something simpler, like the word “colors” for example. To achieve this result, I simply need to create a text file containing the original command, and save it inside the “/ bin”With the name I choose, which in this case will be“Colors”.

To create such a file you can use the text editor of your choice, but remember that it is necessary to open it in “root” mode, as the “/ bin” folder in which the file must be saved cannot be modified by ordinary user.

For the tutorial, I will use the text editor “nano”, As it is present in a large number of different distributions, making the tutorial more comprehensive.

First open the text editor in “root”In the“/ bin”And with the desired name. In my case I used the word “Colors”, As this is the custom command I want to create. Remember to replace the word “Colors”Just below the command you want to create.

sudo nano /bin/cores

With the text editor open, as shown in the image below, type the original command for which you want to create the alias. If you prefer, instead of typing you can simply paste the original command using the shortcut “Ctrl + Shift + V”.

Finally, regardless of which text editor you have chosen, it is necessary to give execution permission to the file you just created. To do this, simply run the command below. Remembering to replace “Colors”By the name of your custom command.

sudo chmod +x /bin/cores

Ready! Now if I just open the terminal, type the word «colors» and press «Enter», the result will be the same as the original command.

The same procedure can also be done for any script you want. To do this, simply paste the desired script into the “/ bin” folder, and the name of that script file will be the custom command. Remember that for the file to work, it must have permission to run, and the name cannot contain spaces.

The custom commands created using this method were tested and worked perfectly using Bash and also Zshell.

Do you already use custom commands, or have you only known about this possibility now? And what do you think about such a feature? Tell more in the comments!

Do you like Linux and technology? Have a question or problem that you can’t solve? Come join our community in the forum Diolinux Plus!

That’s all, and MERRY CHRISTMAS! ???

Have you seen any errors or would you like to add any suggestions to this article? Collaborate, click here.