Monday, 20 January 2014

Linux Command



Linux Commands






Find Command 

## To search for files in /target_directory and all its sub-directories, that have been modified in the last 60 minutes:

    $ find /target_directory -type f -mmin -60

## To search for files in /target_directory and all its sub-directories, that have been modified in the last 2 days:

    $ find /target_directory -type f -mtime -2

## To search for files in /target_directory and all its sub-directories no more than 3 levels deep, that have been modified in the last 2 days:

    $ find /target_directory -type f -mtime -2 -depth -3

## You can also specify the range of update time. To search for files in /target_directory and all its sub-directories, that have been modified in the last 7 days, but not in the last 3 days:

    $ find /target_directory -type f -mtime -7 ! -mtime -3

## To search for files in /target_directory (and all its sub-directories) that have been modified in the last 60 minutes, and print out their file attributes:

    $ find /target_directory -type f -mmin -60 -exec ls -al {} \;
    $ find /target_directory -type f -mmin -60 | xargs ls -l



## Find all files in current directory and subdirectory, greater than some size using find command in Unix:

find . -size +1000c -exec ls -l {} \;

Always use a c after the number, and specify the size in bytes, otherwise you will get confuse because find -size list files based on size of disk block. To find files using a range of file sizes, a minus or plus sign can be specified before the number. The minus sign means "less than," and the plus sign means "greater than." Suppose if you want to find all the files within a range you can use find command as below.

## It return files which size is greater than 50M

find . -size +50000000c -exec ls -lh {} \;

## It return files which extension are .java and end with other character

find . -type f -name "*.java*" -print


Change Shell by Cmd 
   
##First, find out available shell list:

    $ less /etc/shells
   
##Now change your shell name to /bin/tcsh:

    $ chsh -s /bin/tcsh
   
##When promte for password, type your own password. If you just type chsh command, it will prompt for shell name interactively:

    $ chsh

Owner Ship Cmd on File System 

    $chmod u+x <filename>
    $chmod uog+xwr <filename>
           u : User           
           g : Group   
           o : Other
           ---------
           r : Read  : 4
           w : Write : 2
           x : Execute : 1
    $chmod 660 <Filename> // owner group other

    $chmod 751 tech

    $chmod u=rwx, g=rx, o=x tech
   
    <chown> change ownership of file system.


##As the owner, jim can use the chmod command to permit or deny other users access to program.c.

    $chown jim program.c


##To change the owner and group of all files in the directory /tmp/src to owner john and group build:

    $chown -R john:build /tmp/src 

   
Linux Version Info

## see your current Linux kernel version:

    $ uname -r
    $ uname -mrs

## To print all information, enter:

    $ uname -a

## see Linux version info:

    $ cat /proc/version

## Here is another output from my Debian based serve

    $ lsb_release -a
   
 Process Command line Info

## Display a tree of processes

    $ pstree

## Dynamic real-time view of a running system

    $ top

## Display all running process:

    $ ps aux | less

## Print a process tree using ps

    $ ps -ejH
    $ ps axjf

## Get info about threads

    $  ps -eLf

## Get security info

    $ ps -eo euser,ruser,suser,fuser,f,comm,label
    $ ps axZ
    $ ps -eM

## Save Process Snapshot to a file

    $ top -b -n1 > /tmp/process.log
    <<you can email result to yourself>>

    $ top -b -n1 | mail -s 'Process snapshot' you@example.com


Sunday, 5 January 2014

how-to-remove-duplicates-from-array-java-without-collection-API


In this program, we have not used any collection class to remove duplicates, In this example, we are removing duplicates elements from array and copying them into newly result array.


1:  import java.util.Arrays;  
2:  /**  
3:   * @author Ajaykumar.gupta  
4:   *  
5:   */  
6:  public class RemoveDublicateElements {  
7:      /**  
8:       * Suppose let have a arrays of integer elements.  
9:       */  
10:      private static int[] array = { 1, 2, 3, 4, 2, 4, 5, 6, 7, 1, 3, 5, 9, 8, 7,  
11:              6, 7, 8, 9, 0, 8, 6, 5, 0, 5, 4, 3, 2, 5, 6, 0, 1, 2, 3, 4, 5, 6 };  
12:      public static void main(String args[]){  
13:          int[] values = removeDublicate(array);  
14:          for(int i=0; i < values.length ;i++ ){  
15:              System.out.print(values[i]+" ");  
16:          }  
17:      }  
18:      private static int[] removeDublicate(int[] array){  
19:          /**  
20:           * Sort Array elements first.  
21:           */  
22:          Arrays.sort(array);  
23:          // Allocated a new array with same size.  
24:          int []result= new int[array.length];  
25:          // Assigned a first element into a previous variable.  
26:          int previous = result[0];  
27:          // Assigned a smallest elements into Array zero position.  
28:          result[0] = previous;  
29:          int num = 1;  
30:          for(int i=1; i <array.length;i++){  
31:              int ch = array[i];  
32:              // Logic is applied for removed duplicate elements and assigned to result array  
33:              if(previous != ch){  
34:                  result[num++] = ch;  
35:              }  
36:              previous = ch;  
37:          }  
38:          // Removed Blank space(Zero) and copy exect size of array and return it to caller.  
39:          return Arrays.copyOf(result, num);  
40:      }  
41:  }  

Output : 0 1 2 3 4 5 6 7 8 9