Linux - custom commands

A really nifty feature of Linux is, that you can easily create your own customized commands.

Why?

  • Lets say you use tar only once in a while and always forget the parameters
  • Or you use rdesktop instead of remmina since remmina always crashes and you don't want to remember the remote IP and also want to set a shared folder and fixed resolution
  • In case you frequently switch workplace with a notebook and quickly want to change monitor or network settings

I think you could roughly sum this up as "benefits in efficency through automation". Also it's kind of cool!

Actually there are different ways to do this - I'll show you three ways:

Aliases

Single commands - like ssh or rdesktop with a certain IP/User/Port - don't have to be scripted - you can just define an alias in your ~/.bashrc or ~/.bash_aliases.

Usually .bash_aliases should already be included in your .bashrc, therefore .bash_aliases would be the preferred location to keep things tidy.

Here you can define one-liners like:

alias myvm='ssh volker@192.168.35.123 -p 1044'

Shell scripts

With shell scripts you can create more complex commands which for example run several Linux commands, use variables, user input or control flow (if, else, while, case, etc).

First things first, have a look at your profile settings:

vi ~/.profile

image profile settings

In an Ubuntu distribution you will find these lines already there (which some people consider a security risk). They check your home directory for a "bin" subdirectory and if they find it, they include it into the PATH.

Thereby scripts placed in this folder (given they are made executable) will be treated more or less like regular commands, meaning autocomplete in a terminal will find them and you can run them from anywhere in the directory tree.

If you are working on a distribution where these lines are not present, you have to include them on your own.

Next you have to create the "bin" folder in your home directory:

mkdir ~/bin
cd ~/bin

Then create your first scipt:

echo "echo 'my first custom script'" > myscript

Though it works (at least for me) without a shebang it is good practice to include it. So the first line of your shell script should be #!/bin/sh or #!/bin/bash so your operating system knows how to run the script.

For more elaborate scripts you might want to look into bash scripting! An example which I use, is to mount and unmount my external usb harddrive with a series of udsisctl commands from command-line as described here.

Now make your script executable and restrict access rights for others as needed:

sudo chmod 700 myscript

Python

Python custom commands are not that suitable for running Linux commands (though it is possible) but offer you on the other hand the possibilities of a full blown programming language. For example you could automagically grep a web page, search the content for information you are interested in, return the output to your shell and ask you if the script should send an e-mail to your colleague.

Since most Linux distributions come with Python installed you also can place Python scripts in your users ~/bin folder (as described above), thereby including them to your PATH, and make them executable with:

sudo chmod 700 mypythonscript.py

The only difference to the shell script approach is to start your python script with a shebang pointing to your Python installation (make sure to get the right location, since it varies depending on your distribution):

#! /usr/bin/python3
print("my second script")

You even can remove the .py ending. By the way: I used Python 3 for this example - in Python 2.x print is a statement instead of a function and therefore comes without the brackets.


PS: If you come up with cool custom commands - let me know: ed.rutkafunamnetadoeg@tkatnok
PPS: I find it helpful to name my scripts in a common syntax, like always starting with "my" (e.g. mytar, mysync, myftp), to distinguish them from regular commands.