Monday, July 21, 2014

Know the Physical RAM Size: Linux/Unix

Commands:

1. less /proc/meminfo
2. free -m
3. vmstat -s
4. gnome-system-monitor
5. sudo dmidecode --type memory

Monday, July 7, 2014

find: Make UNIX easy



Description Command
find a file in a directory find <directory> -name “<file/dir name>”
case insensitive find -i (eg: find . -iname '<file/dir name>'


find for regular file types find . -name '<name>' -type f
find for directories types find . -name '<name>' -type d
find for symbolic links find . -name '<name>' -type l


find all empty files in a directory find . -empty


execute another command on the find results:
Syntax: <find command> -exec <other command> {} \;

grep: Make UNIX easy

Global search for a Regular Expression and Print



Description Command
Match count -c
case insensitive search -i
recursive search -r
select non-matching lines -v
show matched text in color --color
show matched text and next n lines after it -A n (eg: grep -A 3 "example" demo.txt
show file name along with matched line -H
exclude hidden directories in search results --exclude="\.*" (eg: grep -iHr --exclude="\.*" "text" *
show line numbers -n
search in only specific file types *.fileType (eg: grep -n "print" *.java)

jar: Make UNIX easy

1. unjar a jar file
jar -xf file.jar

2. List files in jar file, without extracting it
jar -tvf file.jar

3. unzar to a specific directory
unzip file.jar -d destinationDir

vi: Visual Editor: Make UNIX easy


Description Command
Go to start of file gg
Go to end of file G (Shift + g)
Go to start of line 0 (zero)
Go to end of line $ (dollar)
Highlight similar words # (Press # with curson on required word)


Go to next line j
Go to previous line k
Go to next character h
Go to previous character l


Delete current character x
undo last action u
redo Ctrl + u


Delete a line dd
Delete all lines, from current line to end of the file dG
Delete all lines, from current line to start of the file dg


Copy Use mouse to copy required lines
Paste Shift + Insert
Refresh the current file :e (

Saturday, January 18, 2014

using diff and patch commands in linux to get original/modified files

diff and patch tools are very useful while using version controlling applications.
Here I am going to explain a simple and powerful usage of patch command.

Lets say we have a file StringOne.java
Then we modified the file and committed the changes.
Lets assume that currently we have only two files:
1. diff file(between original and modified version)
2. file after modifications

Suppose after some days I want to view the original file before modifications. Since I do not have that, its not possible to view it directly as we didnt store it anywhere.
But with "patch" command, we can get it.

File Before modifications: PatchExample.java

package com.str1;

import java.io.*;

public class StringOne {
    static public void main(String[] args) {
        System.out.println("This is String one");
    }
}

File After modifications: PatchExample_p1.java

package com.str2;

public class StringOne {
    static public void main(String[] args) {
        System.out.println("This is String one");
        System.out.println("This is the extra line that got added...");
    }
}

diff file (original to modified): diff.patch
1,3c1
< package com.str1;
<
< import java.io.*;
---
> package com.str2;
7a6
>         System.out.println("This is the extra line that got added...");


Case 1: You have original file and diff file --> You want to get the modified file:

patch -i diff.patch -o PatchExample_p1.java PatchExample.java

This will produce a required new file named "PatchExample_p1.java".

Here:
    -i : input diff file name (diff.patch)
    -o : output file name to be generated (PatchExample_p1.java)
       : input file (PatchExample.java)

Case 2: You have modified file and diff file --> You want to get the original file:

patch -R -i diff.patch -o PatchExample.java PatchExample_p1.java

This will produce a required new file named "PatchExample.java".

Here:
    -R : does the reverse patching

Wednesday, January 1, 2014

automate unix scp command in shell scripts : password less scp with expect shell command

expect is a unix shell command.
This is like a separate shell, as we use it as shebang in the shell script.

you can check the presence of this command by:
which expect

As you know, scp (secury copy) is a tool to copy files/directories from one host to another.

syntax of scp:

scp user@source_host:source_file_path user@destination_host:destination_file_path

(as you know, you can avoid corresponding user/host, if the source/destination host is localhost)

this prompts user to enter the password.

few ways to avoid entering password for scp:
1. generate public/private keys and export them to the required host
2. use expect command

you can get links for first method by google'ng. Here I am going to describe the second method.

script for password less scp with expect command:

#!/usr/bin/expect

# use the scp command
spawn scp <user>@<source_host>:<source_file_path> <user>@<destination_host>:<destination_file_path>

# use the expect to answer the prompts
# we are providing inputs to two propts
# 1. answer for "do you realy want to connect to this host: (yes/no)?"
# 2. "enter password"

expect {
  -re ".*es.*o.*" {
    exp_send "yes\r"
    exp_continue
  }
  -re ".*assword.*" {
    exp_send "<your_password>\r"
  }
}
interact



Note: Please change the following accordingly in the above script:
<user>, <source_host>, <source_file_path>, <destination_host>, <destination_file_path>, <your_password>