Saturday, December 28, 2013

Below are the description/usage of 10 most used linux commands:

  1. grep command:- Its name comes from the ed command g/re/p (globally search a regular expression and print).
    The grep command is used to search text or searches the given file for lines containing a match to the given strings or words. By default, grep displays the matching lines. Use grep to search for lines of text that match one or many regular expressions, and outputs only the matching lines. grep is considered as one of the most useful commands on Unix and other Linux operating systems.

    Usage
    :-
    • Find count of matching word
      Example: grep -c "queue" sanj_ms1.log
      this will find the number of occurence of queue word in the file sanj_ms1.log
    •  Showing particular number of lines from the file after the matching
      word
      Example: grep -C 2  "queue" sanj_ms1.log   or   grep --context="2"  "queue" sanj_ms1.log
    • Different varient of grep command
      • pgrep is command to give the process id for a supplied process arugment
        >  pgrep -lf java 
    • grep -i is do case insensetive search:-

      This is also a basic usage of the grep. This searches for the given string/pattern case insensitively. So it matches all the words such as “the”, “THE” and “The” case insensitively as shown below.
      grep -i "string" FILE

      here -l is for name of the process and -f is for full search any command line which containing the process name specified
      • zgrep is used on compressed files  to search for particular string within the zip file

        > zgrep -iw "less" newtestfile.txt.gz
        $ grep -i less test-file.txt   This particular command will search for exact word less

        >zgrep -i "less" newtestfile.txt.gz
        zless - file perusal filter for crt viewing of compressed text
        $ grep -i less test-file.txt
        zless - file perusal filter for crt viewing of compressed text
        in this command its searching for all lines which contains less or word containing less.

  2. SCP command:- scp or secure copy is a method of transferring file to remote machine or transfer of file between two remote machine it uses SSH as protocol
    For examples: we can refer to : http://www.hypexr.org/linux_scp_help.php
  3. Find command:-

    Example 1:
    find /path/to/files* -mtime +5 -exec rm {} \;

    • the first argument is the path to the files
    • second arugment -mtime is used to specify the number of days old that file is +5 will fetch files which are 5 days older
    • the third arugment -exec allow to pass commands like rm  {} \; is required to end the command 
    Example 2: 
    Find files ignoring case
    find  -iname *.txt
    Example 3: 
    Find files as per the size

    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 in below example of find:
    find . -size +10000c -size -50000c -print

    This find example lists all files that are greater than 10,000 bytes, but less than 50,000 bytes:
  4. # netstat -tulpn | grep --color :80
  5. To extract a tarball to a specific directory
    untar -xvf filename.tar -C /tmp/dirname  (it will unzip to particular directory)


  6. Bash find files between two dates:
    find . -type f -newermt 2010-10-07 ! -newermt 2014-10-08
    
    Returns a list of files that have timestamps after 2010-10-07 and before 2014-10-08
    Bash find files from 15 minutes ago until now:
    find . -type f -mmin -15
    
    Returns a list of files that have timestamps after 15 minutes ago but before now.
    Bash find files between two timestamps:
    find . -type f -newermt "2014-10-08 10:17:00" ! -newermt "2014-10-08 10:53:00"
    
    Returns files with timestamps between 2014-10-08 10:17:00 and 2014-10-08 10:53:00