Thursday, May 23, 2013

Create your own Linux (custom) commands

Hi,
Most of the time we need to use some commands repeatedly while working. It's just annoying to type them again and again, of course we can use reverse search [ctrl+r] for reverse command search but I am lazier than that ;)

Here are couple of ways to customize (or get more lazy) Linux commands,

  • Use Aliases to Customize Linux Commands:

This is actually for small commands (can be used for large ones also). You can set it in terminal as bellow,
 $alias ll='ls -l'  

Now onward whenever you execute 'll' command it will work as 'ls -l', so simple.

Another way is to add aliases in .bashrc file in your home directory so it will become permanent.
e.g. put following at end of ~/.bashrc

 # Aliases:   
 alias ll='ls -l'   
 alias la='ls -a'   

  • Write shell scripts for your commands:
This is when you need some larger scripts as your shorten command. In this case you can write script and put them under bin directory in your home directory. You also need to give exe permission to the script.

e.g  I want to make following script as my Linux command. This script initializes the current directory as root for  cscope and also creates cscope database for recursive directory structure,

 #!/bin/bash    
 # cscope_init script   
 # Creating cscope database for large project    
 # with recursive deirectory structure    
 # First go to root dir of the source code    
 # Run this script - cscope_init $PWD   
 if test $# -ne 1; then    
  echo "usage - $0 <PROJ_ROOT>"    
  exit 1    
 fi    
 echo "PROJ_ROOT - $1"    
 cd /    
 find $1 -name "*.[ch]" -o -name "*.asm" > $1/cscope.files    
 cd $1    
 cscope -b -q -k    
 echo "Done!   
write this script to cscope_init file, and add permission to execute it.

 $chmod +x cscope_init   

Now copy this file to ~/bin/cscope_init. Now onward you can run following command in any directory to create recursive cscope database for that directory,

 $cd /path/to/project/root/directory
 $cscope_init $PWD  

Result,
 PROJ_ROOT - /path/to/project/root/directory   
  Done!   

Done!!!!!!

0 comments:

Post a Comment