Thursday 27 August 2015

Handling quotes in perl

Want to print :- Hello World' abc
----------------------------------------
perl -e 'print "Hello World'\'' abc\n";'


Want to print :- Hello World" abc\n
-------------------------------------------
perl -e "print 'Hello World"\"" abc\n';"

Wednesday 26 August 2015

Including perl module from non standard path

  1. Using perl module from standard path:
  2. ----
  3. use Getopt::Std;
  4. ----

  5. Using perl module from non-standard path(eg; from your home directory):
  6. ---- use lib '/home/<username>/perl_modules/DateTime-1.20/lib';
  7. use DateTime;

setenv PERL5LIB /home/rperiset/perl_modules/DateTime-1.20/lib (for tcsh)
export PERL5LIB=/home/rperiset/perl_modules/DateTime-1.20/lib (for bash)
  1. ----
  2. Note: Path should be upto directory where module resides.

Friday 14 August 2015

Insert a node in a sorted linked list. Write a C/C++ program.


Solutions:

void sortedInsert(Node * head, Node* newNode)
{
Node *current = head;
           
// traverse the list until you find item bigger the // new node value
     // 
while (current!= NULL && current->data < newNode->data)
{
current = current->next);
}
//
// insert the new node before the big item
//
newNode->next = current->next;
current = newNode;
}

Sort singly linked list

Solutions:

//sorting in descending order
struct node
{
int value;
node* NEXT;
}
//Assume HEAD pointer denotes the first element in the //linked list
// only change the values…don’t have to change the //pointers

Sort( Node *Head)
{
node* first,second,temp;
first= Head;
while(first!=null)
{
second=first->NEXT;
while(second!=null)
{
if(first->value < second->value)
{
temp = new node();
temp->value=first->value;
first->value=second->value;
second->value=temp->value;
delete temp;
}
second=second->NEXT;
}

first=first->NEXT;
}
}

Given a singly linked list, find the node in the middle.


Solution 1: Walk the linked list to find the length of the list. Lets say n is the length of the list. Walk the list again upto ⌊ n/2 ⌋. 

Solution 2: Use two pointers. Move one pointer at twice the speed of the second. When the 1st pointer reaches the end of the list, the 2nd pointer will be pointing to the middle node. Note: If the list has even number of nodes, the middle node will be floor of ⌊ n/2 ⌋. 

Code:
Node * FindMiddle(Node *listHead)
{
  Node *ptr1, *ptr2;  // we need 2 pointers
  ptr1 = ptr2 = listHead; // set the pointers to point to the list head initially

  int i=0;

  while(ptr1->next != NULL) // keep looping until we reach the tail
                              // (next will be NULL for the last node)
  {
      if(i == 0)
      {
          ptr1 = ptr1->next; //increment only the 1st pointer
          i=1;
      }
      else if( i == 1)
      {
          ptr1 = ptr1->next; //increment both pointers
          ptr2 = ptr2->next;
          i = 0;
      }
  }
  return ptr2;        //now return the ptr2 which points to the middle node
}

Given a singly linked list find the n-th node from the back.

Solution 1: Reverse the linked list and select the n-th node from the head of the linked list.

Solution 2: Maintain 2 pointers n nodes apart. When the 1st pointer reaches the tail, the second pointer will be pointing to the desired node.

Notes: Both solutions take O(n) time but Solution 2 is more elegant.

Code:


//define the list node
typedef struct _node
{
 int i;
 struct _node *next;
} Node;

Node * FindNthFromBack(Node *listHead, int n)
{
    Node *ptr1, *ptr2;  // we need 2 pointers
    ptr1 = ptr2 = listHead; // set the pointers to point to the list head initially

    while(ptr1->next != NULL) // keep looping until we reach the tail (next will be NULL for the last node)
    {
        if(n > 0)
        {
            ptr1 = ptr1->next; //increment only the 1st pointer
            n--;
        }
        else
        {
            ptr1 = ptr1->next; //increment both pointers
            ptr2 = ptr2->next;
        }
    }
    return ptr2;    //now return the ptr2 which points to the nth node from the tail
}

Where singly linked lists are merged?

There are 2 singly linked lists (list1 and list2) that merge at one of the nodes and share the nodes there onwards. The goal is to find the node where these linked lists merge or join.


Solution:
   1. Find length of both linked lists, say len1 and len2.
   2. Make current pointers for both lists equidistant from the common node. If one of the lists is longer we need to advance its current pointer by len1 - len2 (assuming list1 is longer). Now both current pointers should be equidistant from the common node. If both lists are same len, do nothing in step 2.
   3. Traverse both lists simultaneously while comparing the current pointers for equality. At ant point if they are equal we have found the first common node. If we reach the end of the lists without find the common node then the lists do not overlap.

Loop detection in a singly linked list.

Observation: A linked list with a loop will have a node that is being pointed from 2 different node. 

Solution: This problem was solved in the late 1960s by Robert W. Floyd. The solution is aptly named as Floyd's cycle finding algorithm a.k.a the Tortoise and Hare algorithm. It uses 2 pointers moving at different speeds to walk the linked list. Once they enter the loop they are expected to meet, which denotes that there is a loop. This works because the only way a faster moving pointer would point to the same location as a slower moving pointer is if somehow the entire list or a part of it is circular. Think of a tortoise and a hare running on a track. The faster running hare will catch up with the tortoise if they are running on circular track instead of a straight strip. 

Code: //returns true if the linked list contains a loop
bool ListContainsLoop(Node * head)
{
Node * slowPtr = head;
Node * fastPtr = head;

while(slowPtr  && fastPtr)
{
 fastPtr = fastPtr->next; // advance the fast pointer
 if(fastPtr == slowPtr)   // and check if its equal to the slow pointer
     return true;         // loop detected

 if(fastPtr == NULL)
 {
     return false;        // since fastPtr is NULL we reached the tail
 }

 fastPtr = fastPtr->next; //advance and check again
 if(fastPtr == slowPtr)
     return true;

 slowPtr = slowPtr-next;  // advance the slow pointer only once
}
return false;                // we reach here if we reach the tail
}

Thursday 13 August 2015

Git commands


Create local repository:
-----------------------
mkdir <repository name>
cd <repository name>
git init --bare

Create working directory:
------------------------
mkdir <directory name>
cd <directory name>
git init

Committing file:
---------------
1. git add <filename>
If it is a new folder(my_project) and its contents:
a) git add my_project
b) git add my_project/*
2. git commit -m "<mesage>"
a) git commit -a [To commit all modified and deleted and newly added files]
3. git remote add origin <remote repository URL>
git remote -v
4. git push <filename>
a) git push <origin> master

See all the files in repository:
-------------------------------
git ls-tree --full-tree -r HEAD

Cloning a repository:
--------------------
git clone <repository URL>

Linux commands


tar:

Create a new tar archive:
$ tar cvf archive_name.tar dirname/

Extract from an existing tar archive:
$ tar xvf archive_name.tar

View an existing tar archive:
$ tar tvf archive_name.tar


ssh:

Login to remote host:
ssh -l jsmith remotehost.example.com

Debug ssh client:
ssh -v -l jsmith remotehost.example.com

Display ssh client version:
$ ssh -V
OpenSSH_3.9p1, OpenSSL 0.9.7a Feb 19 2003


sed: When you copy a DOS file to Unix, you could find \r\n in the end of each line. This example converts the DOS file format to Unix file format using sed command.

$sed 's/.$//' filename
Print file content in reverse order

$ sed -n '1!G;h;$p' filename.txt
Add line number for all non-empty-lines in a file

$ sed '/./=' filename.txt | sed 'N; s/\n/ /'

awk:

Remove duplicate lines using awk:
$ awk '!($0 in array) { array[$0]; print }' temp

Print all lines from /etc/passwd that has the same uid and gid
$awk -F ':' '$3==$4' passwd.txt

Print only specific field from a file.
$ awk '{print $2,$5;}' employee.txt
8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

vim:

Go to the 143rd line of file
$ vim +143 filename.txt

Go to the first match of the specified
$ vim +/search-term filename.txt

Open the file in read only mode.
$ vim -R /etc/passwd

diff:

Ignore white space while comparing.
# diff -w name_list.txt name_list_new.txt
2c2,3
< John Doe --- > John M Doe
> Jason Bourne
Note: Top 4 File Difference Tools on UNIX / Linux – Diff, Colordiff, Wdiff, Vimdiff

sort:

Sort a file in ascending order
$ sort names.txt

Sort a file in descending order
$ sort -r names.txt

Sort passwd file by 3rd field.
$ sort -t: -k 3n /etc/passwd | more

export:

To view oracle related environment variables.
$ export | grep ORACLE
declare -x ORACLE_BASE="/u01/app/oracle"
declare -x ORACLE_HOME="/u01/app/oracle/product/10.2.0"
declare -x ORACLE_SID="med"
declare -x ORACLE_TERM="xterm"

To export an environment variable:
$ export ORACLE_HOME=/u01/app/oracle/product/10.2.0

xargs:

Copy all images to external hard-drive
# ls *.jpg | xargs -n1 -i cp {} /external-hard-drive/directory

Search all jpg images in the system and archive it.
# find / -name *.jpg -type f -print | xargs tar -cvzf images.tar.gz

Download all the URLs mentioned in the url-list.txt file
# cat url-list.txt | xargs wget –c

ls:

Display filesize in human readable format (e.g. KB, MB etc.,)

$ ls -lh
-rw-r----- 1 ramesh team-dev 8.9M Jun 12 15:27 arch-linux.txt.gz

Order Files Based on Last Modified Time (In Reverse Order) Using ls -ltr
$ ls -ltr

Visual Classification of Files With Special Characters Using ls -F
$ ls -F

pwd:

pwd is Print working directory. What else can be said about the good old pwd who has been printing the current directory name for ages.

cd:

Use “cd -” to toggle between the last two directories

Use “shopt -s cdspell” to automatically correct mistyped directory names on cd

gzip:

To create a *.gz compressed file:
$ gzip test.txt

To uncompress a *.gz file:
$ gzip -d test.txt.gz

Display compression ratio of the compressed file using gzip -l
$ gzip -l *.gz
         compressed        uncompressed  ratio uncompressed_name
              23709               97975  75.8% asp-patch-rpms.txt

bzip2:

To create a *.bz2 compressed file:
$ bzip2 test.txt

To uncompress a *.bz2 file:
bzip2 -d test.txt.bz2

unzip:

To extract a *.zip compressed file:
$ unzip test.zip

View the contents of *.zip file (Without unzipping it):
$ unzip -l jasper.zip
Archive:  jasper.zip
  Length     Date   Time    Name
 --------    ----   ----    ----
    40995  11-30-98 23:50   META-INF/MANIFEST.MF
    32169  08-25-98 21:07   classes_
    15964  08-25-98 21:07   classes_names
    10542  08-25-98 21:07   classes_ncomp

shutdown:

Shutdown the system and turn the power off immediately.
# shutdown -h now

Shutdown the system after 10 minutes.
# shutdown -h +10

Reboot the system using shutdown command.
# shutdown -r now

Force the filesystem check during reboot.
# shutdown -Fr now

ftp:

Both ftp and secure ftp (sftp) has similar commands. To connect to a remote server and download multiple files, do the following.
$ ftp IP/hostname
ftp> mget *.html

To view the file names located on the remote server before downloading, mls ftp command as shown below.
ftp> mls *.html -
/ftptest/features.html
/ftptest/index.html
/ftptest/othertools.html
/ftptest/samplereport.html
/ftptest/usage.html

crontab:

View crontab entry for a specific user
# crontab -u john -l

Schedule a cron job every 10 minutes.
*/10 * * * * /home/ramesh/check-disk-space

service:

Service command is used to run the system V init scripts. i.e Instead of calling the scripts located in the /etc/init.d/ directory with their full path, you can use the service command.

Check the status of a service:
# service ssh status

Check the status of all the services.
service --status-all

Restart a service.
# service ssh restart

ps: ps command is used to display information about the processes that are running in the system.
While there are lot of arguments that could be passed to a ps command, following are some of the common ones.

To view current running processes.
$ ps -ef | more

To view current running processes in a tree structure. H option stands for process hierarchy.
$ ps -efH | more

free: This command is used to display the free, used, swap memory available in the system.

Typical free command output. The output is displayed in bytes.
$ free
             total       used       free     shared    buffers     cached
Mem:       3566408    1580220    1986188          0     203988     902960
-/+ buffers/cache:     473272    3093136
Swap:      4000176          0    4000176

If you want to quickly check how many GB of RAM your system has use the -g option. -b option displays in bytes, -k in kilo bytes, -m in mega bytes.
$ free -g
             total       used       free     shared    buffers     cached
Mem:             3          1          1          0          0          0
-/+ buffers/cache:          0          2
Swap:            3          0          3

If you want to see a total memory ( including the swap), use the -t switch, which will display a total line as shown below.

sundar@sundar-laptop:~$ free -t
             total       used       free     shared    buffers     cached
Mem:       3566408    1592148    1974260          0     204260     912556
-/+ buffers/cache:     475332    3091076
Swap:      4000176          0    4000176
Total:     7566584    1592148    5974436

top: top command displays the top processes in the system ( by default sorted by cpu usage ). To sort top output by any column, Press O (upper-case O) , which will display all the possible columns that you can sort by as shown below.

Current Sort Field:  P  for window 1:Def
Select sort field via field letter, type any other key to return

  a: PID        = Process Id              v: nDRT       = Dirty Pages count
  d: UID        = User Id                 y: WCHAN      = Sleeping in Function
  e: USER       = User Name               z: Flags      = Task Flags
  ........

To displays only the processes that belong to a particular user use -u option. The following will show only the top processes that belongs to oracle user.
$ top -u oracle

df: Displays the file system disk space usage. By default df -k displays output in bytes.

$ df -k
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/sda1             29530400   3233104  24797232  12% /
/dev/sda2            120367992  50171596  64082060  44% /home

df -h displays output in human readable form. i.e size will be displayed in GB’s.
sundar@sundar-laptop:~$ df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1              29G  3.1G   24G  12% /
/dev/sda2             115G   48G   62G  44% /home

Use -T option to display what type of file system.
sundar@sundar-laptop:~$ df -T
Filesystem    Type   1K-blocks      Used Available Use% Mounted on
/dev/sda1     ext4    29530400   3233120  24797216  12% /
/dev/sda2     ext4   120367992  50171596  64082060  44% /home

kill: Use kill command to terminate a process. First get the process id using ps -ef command, then use kill -9 to kill the running Linux process as shown below. You can also use killall, pkill, xkill to terminate a unix process.

$ ps -ef | grep vim
sundar    7243  7222  9 22:43 pts/2    00:00:00 vim
$ kill -9 7243

rm: 

Get confirmation before removing the file.
$ rm -i filename.txt

It is very useful while giving shell metacharacters in the file name argument.

Print the filename and get confirmation before removing the file.
$ rm -i file*

Following example recursively removes all files and directories under the example directory. This also removes the example directory itself.
$ rm -r example

cp:

Copy file1 to file2 preserving the mode, ownership and timestamp.
$ cp -p file1 file2

Copy file1 to file2. if file2 exists prompt for confirmation before overwritting it.
$ cp -i file1 file2

mv:

Rename file1 to file2. if file2 exists prompt for confirmation before overwritting it.

$ mv -i file1 file2
Note: mv -f is just the opposite, which will overwrite file2 without prompting.

mv -v will print what is happening during file rename, which is useful while specifying shell metacharacters in the file name argument.
$ mv -v file1 file2

cat:

You can view multiple files at the same time. Following example prints the content of file1 followed by file2 to stdout.
$ cat file1 file2

While displaying the file, following cat -n command will prepend the line number to each line of the output.
$ cat -n /etc/logrotate.conf
    1 /var/log/btmp {
    2     missingok
    3     monthly
    4     create 0660 root utmp
    5     rotate 1
    6 }

mount:

To mount a file system, you should first create a directory and mount it as shown below.
# mkdir /u01
# mount /dev/sdb1 /u01

You can also add this to the fstab for automatic mounting. i.e Anytime system is restarted, the filesystem will be mounted.
/dev/sdb1 /u01 ext2 defaults 0 2

chmod: chmod command is used to change the permissions for a file or directory.

Give full access to user and group (i.e read, write and execute ) on a specific file.
$ chmod ug+rwx file.txt

Revoke all access for the group (i.e read, write and execute ) on a specific file.
$ chmod g-rwx file.txt

Apply the file permissions recursively to all the files in the sub-directories.
$ chmod -R ug+rwx file.txt

chown: chown command is used to change the owner and group of a file. \

To change owner to oracle and group to db on a file. i.e Change both owner and group at the same time.
$ chown oracle:dba dbora.sh

Use -R to change the ownership recursively.
$ chown -R oracle:dba /home/oracle

passwd:

Change your password from command line using passwd. This will prompt for the old password followed by the new password.
$ passwd

Super user can use passwd command to reset others password. This will not prompt for current password of the user.
# passwd USERNAME

Remove password for a specific user. Root user can disable password for a specific user. Once the password is disabled, the user can login without entering the password.
# passwd -d USERNAME

mkdir:

Following example creates a directory called temp under your home directory.
$ mkdir ~/temp

Create nested directories using one mkdir command. If any of these directories exist already, it will not display any error. If any of these directories doesn’t exist, it will create them.
$ mkdir -p dir1/dir2/dir3/dir4/

ifconfig: Use ifconfig command to view or configure a network interface on the Linux system.

View all the interfaces along with status.
$ ifconfig -a

Start or stop a specific interface using up and down command as shown below.
$ ifconfig eth0 up
$ ifconfig eth0 down

uname: Uname command displays important information about the system such as — Kernel name, Host name, Kernel release number,
Processor type, etc.,

Sample uname output from a Ubuntu laptop is shown below.
$ uname -a
Linux john-laptop 2.6.32-24-generic #41-Ubuntu SMP Thu Aug 19 01:12:52 UTC 2010 i686 GNU/Linux

whereis: 

When you want to find out where a specific Unix command exists (for example, where does ls command exists?), you can execute the following command.
$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz /usr/share/man/man1p/ls.1p.gz

When you want to search an executable from a path other than the whereis default path, you can use -B option and give path as argument to it. This searches for the executable lsmk in the /tmp directory, and displays it, if it is available.
$ whereis -u -B /tmp -f lsmk
lsmk: /tmp/lsmk

whatis: Whatis command displays a single line description about a command.

$ whatis ls
ls  (1)  - list directory contents

$ whatis ifconfig
ifconfig (8)         - configure a network interface

locate: Using locate command you can quickly search for the location of a specific file (or group of files). Locate command uses the database created by updatedb.

The example below shows all files in the system that contains the word crontab in it.
$ locate crontab
/etc/anacrontab
/etc/crontab
/usr/bin/crontab
/usr/share/doc/cron/examples/crontab2english.pl.gz
/usr/share/man/man1/crontab.1.gz
/usr/share/man/man5/anacrontab.5.gz
/usr/share/man/man5/crontab.5.gz
/usr/share/vim/vim72/syntax/crontab.vim

man: 

Display the man page of a specific command.
$ man crontab

When a man page for a command is located under more than one section, you can view the man page for that command from a specific section as shown below.
$ man SECTION-NUMBER commandname
Following 8 sections are available in the man page.

General commands
System calls
C library functions
Special files (usually devices, those found in /dev) and drivers
File formats and conventions
Games and screensavers
Miscellaneous
System administration commands and daemons
For example, when you do whatis crontab, you’ll notice that crontab has two man pages (section 1 and section 5). To view section 5 of crontab man page, do the following.

$ whatis crontab
crontab (1)          - maintain crontab files for individual users (V3)
crontab (5)          - tables for driving cron

$ man 5 crontab

tail:

Print the last 10 lines of a file by default.
$ tail filename.txt

Print N number of lines from the file named filename.txt
$ tail -n N filename.txt

View the content of the file in real time using tail -f. This is useful to view the log files, that keeps growing. The command can be terminated using CTRL-C.
$ tail -f log-file

less:

less is very efficient while viewing huge log files, as it doesn’t need to load the full file while opening.
$ less huge-log-file.log

Once you open a file using less command, following two keys are very helpful.
CTRL+F – forward one window
CTRL+B – backward one window

su:

Switch to a different user account using su command. Super user can switch to any other user without entering their password.
$ su - USERNAME

Execute a single command from a different account name. In the following example, john can execute the ls command as raj username. Once the command is executed, it will come back to john’s account.

[john@dev-server]$ su - raj -c 'ls'

[john@dev-server]$

Login to a specified user account, and execute the specified shell instead of the default shell.
$ su -s 'SHELLNAME' USERNAME

mysql:

mysql is probably the most widely used open source database on Linux. Even if you don’t run a mysql database on your server, you might end-up using the mysql command ( client ) to connect to a mysql database running on the remote server.

To connect to a remote mysql database. This will prompt for a password.
$ mysql -u root -p -h 192.168.1.2

To connect to a local mysql database.
$ mysql -u root -p
Note: If you want to specify the mysql root password in the command line itself, enter it immediately after -p (without any space).

yum: 

To install apache using yum.
$ yum install httpd

To upgrade apache using yum.
$ yum update httpd

To uninstall/remove apache using yum.
$ yum remove httpd

rpm:

To install apache using rpm.
# rpm -ivh httpd-2.2.3-22.0.1.el5.i386.rpm

To upgrade apache using rpm.
# rpm -uvh httpd-2.2.3-22.0.1.el5.i386.rpm

To uninstall/remove apache using rpm.
# rpm -ev httpd

ping:

Ping a remote host by sending only 5 packets.
$ ping -c 5 gmail.com

date:

Set the system date:
# date -s "01/31/2010 23:59:53"

Once you’ve changed the system date, you should syncronize the hardware clock with the system date as shown below.
# hwclock –systohc
# hwclock --systohc –utc

wget:

The quick and effective method to download software, music, video from internet is using wget command.
$ wget http://prdownloads.sourceforge.net/sourceforge/nagios/nagios-3.2.1.tar.gz

Download and store it with a different name.
$ wget -O taglist.zip http://www.vim.org/scripts/download_script.php?src_id=7701

grep:

Search for a given pattern(not case sensitive) in a file 
$ grep -i "pattern you want to search" file1

Print the 3 lines after the matched string
$ grep -A 3 -i "pattern you want to search" file1

Search for a given string in all files recursively
$ grep -r "ramesh" *


find:

Find files using file name(not case sensitive) 
$ find -iname "file1.cpp"

Execute commands on files found by the find command
$ find -iname "file1.cpp" -exec md5sum {} \;

Find all empty files in home directory
$ find ~ -empty

rsync:

Sync a single file on a local machine from one location to another location.
$ rsync -zvh backup.tar /tmp/backups/

Sync all the files of from one directory to a different directory in the same machine
$ rsync -avzh /root/rpmpkgs /tmp/backups/

Sync a directory from a local machine to a remote machine
$ rsync -avz rpmpkgs/ root@192.168.0.101:/home/

Sync a remote directory to a local directory
$ rsync -avzh root@192.168.0.100:/home/tarunika/rpmpkgs /tmp/myrpms

wc:

Count number of newlines in a file.
$ wc -l filename.txt

Count number of words in a file.
$ wc -w filename.txt

Count number of bytes in a file.
$ wc -c filename.txt (or) $ wc -m filename.txt