Saturday, August 13, 2011

Secure remote connection without password

If you ssh, scp and/or rsync very often and annoyed to enter the password every time, then this information is for you.
Execute the following command from your host machine as the usual user. This will generate a public-private key for this user on that host.
ssh-keygen
The keys will by default be created under ~/.ssh and usually named as id_rsa (private key) id_rsa.pub (public key)
Now the public key can be copied to any number of remote computers and all further secure log-ins to those hosts will not prompt for the password.
ssh-copy-id user@machine

Friday, May 27, 2011

Perl command line execution

Following parameters are very important when executing the perl commands from the command line. Following is the general format.

perl <options> -e '<perl commands>' <input file>

Following options are very important.

-i.bak ==> reads the input file and backs it up (as <filename>.bak) and all the output will be written to the input file. The backup extension can be customised. However this option is useful only when used with the below options

-n  ==> Each line of the input file is passed to the expression as $_.

-p ==> Same as -n. Each line of the input file is passed as $_ to the expression. And adds a implicit print $_ at the end of the expression. If the expression modified $_ ( search & replace for e.g.) then the modified string will be written.

Ideally-p or -n will be used along with -i. 
For e.g. I have my input file
>cat temp.txt
1
2
3
4
5
>perl -n -e 'print "My $_"' temp.txt
My 1
My 2
My 3
My 4
My 5
>perl -p -e 'print "My $_"' temp.txt
My 1
1
My 2
2
My 3
3
My 4
4
My 5
5
>perl -pi.bak -e 's/1/ONE/' temp.txt
>cat temp.txt
ONE
2
3
4
5
>cat temp.txt.bak
1
2
3
4
5


Saturday, April 16, 2011

Effectively using history command

    1) To display timestamp with commands  
    # set HISTTIMEFORMAT='%F %T '# history
    2) To search the command line history, press Ctrl + R and start typing the command. The closest match from the history will be displayed. When the expected command appears press enter to execute. Press the right arrow to select the command and start editing. 
    3) To execute a particular command from history, find out the command number. Command number can be found from the left most column when the history command is executed. Then type !<command number>. For e.g. !5 to execute the command with number 5.
    4) To execute a previous command from history that starts with a particular word, type !<starting few letters of the command>. Then the immediate previous command that start with those words will be executed.
    5) By default the history file is stored in ~/.bash_history
    6)  To avoid duplicates in the command history
    export HISTCONTROL=ignoredups



            Sunday, March 27, 2011

            Grub2 tips & tricks

            How to remove old kernels?

                Use the following command to search for installed kernels
                   sudo aptitude search linux-image | grep -E "^i"
                Above command will provide the list of installed kernels. Unwanted/old kernels can be removed with the following command. 
                   sudo aptitude purge <kernel name from the above list>
                Finally the boot menu can be updated with the following command
                   sudo update-grub

              Tuesday, February 8, 2011

              Using sed

              For searching and replacing text in a file

              sed 's/<text to be searched>/<replacing text>/' <input file>
              For example sed 's/foo/bar/' test, replaces the word foo with bar in the file test

              Monday, February 7, 2011

              SVN Quick Reference


              1. SVN Properties
              • SVN Externals (svn:externals)
              Setting this property works like a soft link in the unix file system. Assume we have an application that uses some common utilities which are located in a different path in the repository. Using this properly we can create a soft link to the common utilities from our current application. So anyone who checks out the application will also get the common utilities automatically through  this link. It looks as though the common utilities are present as part of the application.

              To set the property
              svn propset svn:externals <local dir> <repository>

              To get the property. Go the directory containing externals
              svn propget svn:externals 

              To update the property. Make sure the SVN_EDITOR environment variable is set to a valid editor ( for e.g export SVN_EDITOR=vi). Go the directory containing externals and type the following.

              svn propedit svn:externals .

              The editor will open where you can modify the external symbols. 
              Use svn update to get the updated version and then svn commit to commit the changes.