How to add command shortcut in Ubuntu shell

When you need to write long commands in Linux shell, like me, you must have been looking for a way (if you haven’t found the way already :D) to create shortcuts for those long commands.
Based on your need, you can do any of the following:
alias: If you need a shortcut for only the current shell session, use alias.

  • To check current aliases list: alias
  • To check an existing alias: alias ALIAS_NAME
  • To add new alias: alias COMMAND_SHORTCUT = ‘LONG_COMMAND’ [example: alias apacheReload = ‘/etc/init.d/apache2 reload”’]
  • To remove an existing alias: unalias ALIAS_NAME
  • Learn more about alias at Wikipedia, Bash Man Page for Alias

Symbolic Links: This is actually my favorite. Does not depend on current shell session. FYI. each command is actually an executable script.

  • To create a shortcut for all users, create a soft symbolic link of the executable script in /usr/local/bin. For example, to execute apache related commands using just apache, issue this command
    ln -s /etc/init.d/apache2 /usr/local/bin/apache #this command will create a soft symbolic link of apache2 in /usr/local/bin directory. Now you can just manage apache by using apache command.
  • To create a shortcut only for current user, create a soft symbolic link of the executable script in $HOME/bin

Using your own shell scripts: This requires understanding of shell scripting. You can create a shell script which essentially provide the full ability to create new system commands. Make the script executable, keep it in home directory.

 

I hope it helps some one. Please use the comment section to provide your feedback.

  • iTekBlog

    In my page i had given an way for Shortcut for long linux commands using the command “alias”.check it http://goo.gl/dXd0P

  • thanks a lot, but the second option of symbolic links didn’t work with me
    here’s what I did, I created a file called “lll” inside a folder called “scripts” that’s in my home directory, then did this
    ln -s /home/myusername/scripts/lll /home/myusername/bin/lll
    that worked, and there was a file called “lll” created in my bin directory
    but when I call the “lll” command I get an error telling me that it’s not a command

    btw, I didn’t have a bin directory in my home folder, I created it