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 :-
- Removing Log file or any other file from some directory
- Starting multiple of service with 1 command etc.,
So, How to do it ? Let's follow below :-
- Create a directory say "bin" under your home directory.
- Update your path variable to include this bin directory. Put this in
.bash_proflefile to make it permanent. These files are available in your
- Create a script say, "cleanlog" and keep it in your
/home/username/bindirectory, which you created in first step.
#!/bin/bash `sudo rm -f /var/log/*.gz` `sudo rm -f /var/log/apache2/*.gz`
- Give execute permission to your script by :-
chmod +x cleanlog
- Now, From any directory, you simply type:
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 :-
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 :-
.bash_aliaseswhich is available in your "username" directory like "/home/username"
.bashrcfile which is also available in your "username" directory like "/home/username"