Linux basic commands to start as a system administration part 1

Copy a remote file (using - Concatenation operator)
     ssh abc@sysloggerserver1.linuxnix.com: /var/ftp/pub/webadmin/debian/read    abc@sysloggerserver2.persistent.co.in:/opt/oradba/debadmin/logic/abc/
 
'&&' –Logical AND operator
execute second command, if the first command runs successfully
    ping -c1 abc.com && ssh abc@abc.com
    example: Check if a folder exists or not. If it's not present, create it
    [ ! -d /var/temp ] && mkdir /var/temp
    Example: Create folder, if the folder creation is successful, then only change the directory.
    mkdir abc && cd abc
 
|| –Logical OR operator
    Example: Create a folder if the folder does not exits.
    [ -d /var/temp ] || mkdir /var/temp
    Example: Ping to a machine and let users know if it's not reachable in a meaning full way.
    ping -c1 google.com &> /dev/null || echo “There is some problem with network, please connect your network admin"
 
&& and || operator use case
    Example: Check if a file exists or not. If it exists inform user it exists otherwise create it.
    [-f /var/temp.txt] && echo "file exist" || touch /var/temp.txt
    Example:Check if my previous command executed successfully or not pwd /home/surendra  
    [ $? -eq 0 ] && echo "Yes, $? command successfully executed" || echo “pwd command failed"

| — PIPE operator
output of first command as an input to second command
    Example: Count no of files/folder located in a folder
    ls -l | wc -l
    Example: Display all the partition names in the system.
    df -m | awk '{print $1}'     //  -m is to check space in MB
    Example: to count no of partitions:
    df |wc -l
 
! -NOT operator (Negation operator)
    Example:Remove all the files, which are not a .doc file.
      ls | grep -v doc | xargs rm -rf or rm -rf *.{xls, txt, pdf}
      better way to achieve this: rm !(*.doc)
      Example: Move all the files expect .doc files to /opt
      mv !(*.doc) /opt/
   
{ } –Command combination operator
combine two or more commands to be executed depending on the previous command
    Example:  Check if /opt/temp.txt exists or not?
    [-f /opt/temp.txt] && echo "file exits" || {"The file does not exist"; touch /opt/temp.txt}
 
() –Precedence operator
execute command in precedence order
( caommand1 && command2 ) || ( command3 && command4 )

Service is running or not in Linux
    Method 1: Using service command
    $service servicename status
    Method 2: Using init.d scripts which are located in /etc/init.d folder
    /etc/init.d/cron status

List file with soft link
 ls -lrt | grep ^l    // ^ character is a special regular expression which means start of line
 find . -type l
 find . -maxdepth 1 -type l

 Count occurrence of a word
    $ grep -c "Error" logfile.txt
 
Printing lines before and after of matching word
    $ grep --context=6 error logfile.txt or grep -C 6 "Error" logfile.txt
 
Pattern using egrep and regular expression
    $ egrep 'Error|Exception' logfile.txt

Search patterns in gzip files using zgrep
    $ zgrep -i Error *.gz
 
Search whole word in a file using grep
    $grep -W Error logfile.txt
 
More control, use `\<' and `\>' to match the start and end of words
    $ grep 'ERROR>' *

Display files names which contains given word 
    $ grep -l ERROR *.log

Option to display lines numbers
    $ grep -n ERROR logfile.txt

0 comments:

My Instagram