Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'm a newbie in Unix/Linux environment. Can anyone guide me to create custom commands in Unix/Linux. Thanks in advance

share|improve this question

closed as too broad by paraxor, jasonwryan, Mat, Ulrich Dangel, Anthon Jul 28 '13 at 9:39

There are either too many possible answers, or good answers would be too long for this format. Please add details to narrow the answer set or to isolate an issue that can be answered in a few paragraphs.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
For future reference, you should check out the facts page. It explains the things you should put in your questions. (such as posting the things that you have researched & tried). That's likely why someone gave this question a down vote. –  spuder Jul 28 '13 at 5:36

4 Answers 4

up vote 3 down vote accepted

Create a bash script in your /usr/bin folder, it should look something like this

#!/bin/bash
Whatever combination of commands you want to run when you type this thing.

Its really that easy.

Just name the bash script what you want to type in to the terminal, and make it excecutable: chmod +x filename and you're good to go!

share|improve this answer
  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.

    export PATH=$PATH":$HOME/bin"

  3. Create a script say, "hello" and keep it in your bin directory. Give execute permission to the hello script.

    #!/bin/bash    
    echo My first program
  4. From any directory, you simply type:

    $ hello
    My first program
share|improve this answer
    
something wrong with the code formatting...its not working –  Guru Jul 28 '13 at 6:26
1  
Fixed that for you ;) –  jasonwryan Jul 28 '13 at 6:32

Easy, create an alias.

Say you want to write a command to cd into your download directory. And you want to call it cdd.

alias cdd='cd ~/Downloads' 

You can create any command you want.

Here is further information:
http://www.mediacollege.com/linux/command/alias.html

share|improve this answer
    
This doesn't persist across sessions, though. This command works, but isn't permanent. A more permanent way would be to modify your ~/.bash_aliases file and add the line you suggested there. –  Jeff Grimes Jun 10 at 17:05

Most, if not all by now, Linux distributions have a little script in ~/.bashrc that looks almost identical to this:

if [ -e ~/.bash_aliases ]
then
. ~/.bash_aliases
fi

This merely means you can create your own commands (also known as 'aliases' usually referred to an existing command with some arguments you always have to use, or a list of commands that have to be executed in order).

Your Linux distribution will most likely not have the .bash_aliases file created in your home, unless you've manually done that already. So to create the file, type in the following command: touch ~/.bash_alisaes

Now that file will be executed automatically every time you fire off a new Terminal.

What you can do now is create a list of aliases and add them to that file for later uses. For example, the rm (remove) command by default does NOT ask you to confirm your request when you tell it to delete a file/directory. However, there is an argument that tells rm to ask you to confirm your request, -i. So, rm -i filePath will display a message asking you if you were sure you want to delete the specified file. Now, if you were to accidentally delete a file, you will very likely forget to include the -i option, and that's where an alias becomes very beneficial. Typing the following command

echo "alias rm='\rm -i'" >> ~/.bash_aliases

will tell Bash that every time you request to delete a file, a confirming message will be displayed to you. Of course, there is a lot more you can do, but this is just the basics.

Afterall, if you feel like wanting to learn how to use some basic command (i.e. cd, touch, rm, mkdir, pushd, popd, etc.) and/or more sophisticated ones, I'd recommend a very good book you can have on your bookshelf as a reference called

a practical guide to linux commands editors and shell programming, by Mark G. Sobell. ISBN: 978-0133085044

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.