Create your own commands in linux

Creating your own commans in linux can be very handy for system administrators or normal linux users.

Custom commands become handy, when you have to do some event repeatedly and requires more than 1 step to complete , Some of them are like :-

  1. Removing Log file or any other file from some directory
  2. Starting multiple of service with 1 command etc.,

So, How to do it ? Let's follow below :-

  1. Create a directory say "bin" under your home directory.
  2. Update your path variable to include this bin directory. Put this in .profile  or .bash_profle file to make it permanent. These files are available in your /home/username directory
    export PATH=$PATH":$HOME/bin"
  3. Create a script say, "cleanlog" and keep it in your /home/username/bin directory, which you created in first step.
    #!/bin/bash    
    
    `sudo rm -f /var/log/*.gz`
    `sudo rm -f /var/log/apache2/*.gz`
  4. Give execute permission to your script by :-
    chmod +x cleanlog
  5. Now, From any directory, you simply type:
    cleanlog

And it will do your job of cleaning your gunzipped log file from your log directory. Similarily you can create your own. Commands are writen inside backtick (`). These tells script to execute command, So, if you have gone with all previous tutorial, you must have learn various command which you could combine and create your own script.

alias - Life saviour too

Yes, alias is very handy command which can help us create short-cut of our command, for an instance, changing my directory in terminal to web server directory. One does by :-

cd /var/www/html

But with alias, you can create one word command and will do magic for you.

alias cdweb='cd /var/www/html' 

But, it will not persist across sessions, for that you may need to add above alias in any of the below files :-

  1. .bash_aliases which is available in your "username" directory like "/home/username"
  2. .bashrc file which is also available in your "username" directory like "/home/username"

Loading ...

Related Results :

  1. Create your own commands in linux
  2. bind bash
Note :
  • Related Posts are generally User Blog posts.
  • or Other tutorials from other networks of w3clan.com.
  • Any registered user can create related posts based on search term tags.

About the Author