Can anyone guide me to create custom commands in Unix/Linux.

| improve this question | | | | |
  • 2
    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

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

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!

| improve this answer | | | | |
  • 1
    Could you modify this to be a step-by-step guide, including every necessary step? I am still unsure how to make my own custom command. – Yoda May 2 '16 at 14:15
  • 5
    @AndersMB, you need to read a book. There are many good ones, even available for free. Start with "The Linux Command Line". – Wildcard May 13 '16 at 19:15
  • 1
    Umm.. he was joking. He was joking, right?! – Somenath Sinha Sep 29 '17 at 16:55
  • 1
    execute with ./filename – Squirrl Apr 1 '18 at 23:10
  • 1
    /usr/bin folder is intended for the Linux distributor. ~/bin is the right place to put personal stuff and /usr/local/bin is for custom software intended for everyone on the system to use. One would like to keep a degree of logical separation / easy navigation over their own files. – bloody Mar 15 at 21:07
  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 by $ chmod +x hello.

    echo My first program
  4. Reload .profile or .bash_profle:

    $ . ~/.bash_profile

  5. From any directory, you simply type:

    $ hello
    My first program
| improve this answer | | | | |
  • I know this is very old but I have seen your suggestion of creating a bin folder in a lot of answers here. Is there any problem if it's .bin instead of bin?. I am really picky about how my home looks and I dont want an extra folder just because. – M.O. May 28 '18 at 16:37
  • I have tried using this unsuccessfully and was very frustrated for a while thinking there was something wrong with my zsh install or $PATH but it ended up being that the chmod -x hello wasn't working, neither does a capital -X. Instead I tried chmod 755 hello, while I am not sure of the security risks in regards to this command it actually let me run hello. Does anyone have an explanation for this? I am assuming it is a problem in regards to age? – ConstantFun Apr 15 '19 at 8:57
  • 2
    @ConstantFun Use chmod +x hello to add 'run' privileges to the script. chmod -x hello does the exact opposite - removes the 'run' privileges. (Notice the plus/minus sign difference in the answer and in your comment.) – Filip Happy Jul 23 '19 at 9:16

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:

| improve this answer | | | | |
  • 6
    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 '14 at 17:05
  • 2
    Also works by adding it to your ~/.bashrc file – Delicia Brummitt Aug 16 '15 at 15:14
  • @JeffGrimes you can put it in the ~/.profile file (or .bashrc as Deilicia mentioned) and it will be permanent enough. very useful on hostings with limited file editing rights btw – user151496 Apr 30 at 9:09

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

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

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_aliases

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—this is just the basics.

If you want to learn how to use some basic commands (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

| improve this answer | | | | |
  • 2
    why the backslash? – phil294 Mar 5 '16 at 13:32
  • @phil294 perhaps you mean forward slash? So when you see ~/ that is a shortcut to saying /home/yourname/.bashaliases else here is info about backslashes as escape characters: unix.stackexchange.com/questions/146663/meaning-of-backslash – Robert Houghton Feb 5 at 15:46
  • 1
    @RobertHoughton thanks! But I guess I had been confused to the backslash inside rm=\rf instead. I now know that its purpose is to point to the real rm binary (at /usr/bin or wherever), and to ignore any possible functions or other aliases named rm. – phil294 Feb 5 at 18:45

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