Into the Linux-verse

Arfat KadvekarArfat Kadvekar
6 min read

Hey Guys, here is another blog after the roller coaster ride of life... The continuation of the workshop series... Hope you love the blog from a naive blogger... Let's see what I have added to my mind palace...

Filters

Filters are programs that take plain text(either stored in a file or produced by another program) as standard input, transform it into a meaningful format, and then return it as standard output. Linux has a number of filters. Some of the most commonly used filters are explained below -

  1. Sort -

    Commands are as follows

     sort [filename] #This sorts by alphabetical order
     sort -r [filename] #This sorts by reverse alphabetical order
     sort -n [filename] #This sorts the numeric content of the file
     sort -M [filename] #This sorts the month names in the file
    
  2. Tail -

    Commands are as follows

     tail -f [filename] #To get the dynamic output
     tail -[line number] [filename] #To get the content on the particular line of the file
     tail -c [number of bytes] [filename] #To display specific bytes of content of the file
     tail [file1] [file2] #To display content of multiple files
    
  3. Head -

    Commands are as follows

     head -n [number of lines] [filename] #To show specific number of lines from the content of the file
     head -c [number of bytes] [filename] #To display the specific number of bytes of content of the file
     head [file1] [file2] #To show the content of multiple files
     head -q [file1] [file2] #To show merged content of multiple files
     head -v [filename] #To show the content of the file preceded by it's file name
    
  4. WC (Word Count) -

    Commands are as follows

     wc -l [filename] #To show the number of lines in the file
     wc -m [filename] #To show characters(letters) of the content in the file
     wc -w [filename] #To show number of words of the content in the file
     wc -c [filename] #To show the specific number of bytes of content of the file
    
  5. Uniq -

    Commands are as follows

     uniq -c [filename] #To show the count of repeated lines in the file
     uniq -d [filename] #To print repeated lines in the file
     uniq -D [filename] #To display duplicate lines in sorted file
     uniq -u [filename] #To show unique lines in the content of the file
    
  6. Comm -

    Commands are as follows

     #Syntax 
     comm [file1] [file2] #It shows the common content between two files
    
  7. Diff -

    Commands are as follows

     #Syntax 
     diff [file1] [file2] #It shows the different content between two files
    
  8. grep -

    Commands are as follows

     grep ["word"] -i [filename] #To search word - case insensative.
     grep ["word"] -o [filename] #To print only the matched parts of a matching line.
     grep ["word"] -A [filename] #To prints output after the result.
     grep ["word"] -B [filename] #To prints output before the result.
     grep ["word"] -C [filename] #To prints output after and before the result.
     grep ["word"] -c [filename] #To print the count of the word in the file.
     grep ["word"] -w [filename] #To print the matched word in the file.
     grep ["word"] -e [filename] #To print multiple matched words.
     grep ["word"] -n [filename] #To print the matched lines with line numbers.
    
  9. awk -

    Commands are as follows

     awk '{print}' [filename] #To print the content of the file
     awk '/word/' '{print}' [filename] #To print the lines which matches the given word in the file
     awk '{print $[line number], $[line number]}' [filename] #To print the specific words of the lines in the file
     awk '{print $NF}' [filename] #To print the last line of the file
     awk 'NR==[number of word], NR==[number of word]' '{print $[number of line], $[number of line]}' [filename] #To print the particular words of particular lines of the file
    
  10. cut -

    Commands are as follows

    #Syntax 
    cut -d '<deluminator>' -f <filename>
    
  11. sed -

    Commands are as follows

    sed 's/[word to be replaced]/[replaced with]/[occurance]' [filename] #To replace the word by occurance
    sed 's/[word to be replaced]/[replaced with]/g' [filename] #To replace the word globally
    sed '[line number] s/[word to be replaced]/[replaced with]/ ' [filename] #To replace the word on the specific line
    sed '$d' [filename] #To delete the last line
    sed '[number of line]d' [filename] #To delete the particular lines
    sed '/[word to be matched]/d' [filename] #To delete the word matching lines
    

Shell Scripting

Shell scripting is an important part of process automation in Linux. Scripting helps us write a sequence of commands in a file and then execute them. This saves us time because we don't have to write certain commands again and again. We can perform daily tasks efficiently and even schedule them for automatic execution. We can schedule tasks at startups and many more...

Bash Scripts -

A bash script is a series of commands written in a file. These are read and executed by the bash program. The program executes line by line. By naming conventions, bash scripts end with a .sh. Certainly, bash scripts can run perfectly fine without the sh extension.

Shebang -

Scripts are also identified with a shebang. Shebang is a combination of bash# and bang! followed the bash shell path. This is the first line of the script. Shebang tells the shell to execute it via bash shell. Shebang is simply an absolute path to the bash interpreter.

#Syntax
#!/bin/bash

Creating the script -

# Create a file with .sh extension 
vim Agenda.sh

Modify the same file -

#Syntax
#!/bin/bash/
[command]

#Example
#!/bin/bash/
cd /tmp/
touch Agenda
ls -l

#Save and exit.

Run the script -

#Run by giving the execute permissions
chmod u+x Agenda.sh
./Agenda.sh
#Run without giving execute permissions
bash Agenda.sh

Variable declaration -

  • Temporary variables

      #Syntax
      <variable name>=<content>
    
  • Permanent variables

#Navigate through /etc/bashrc file 
vim /etc/bashrc
#Add your variables here
#Syntax
<variable name>=<content>

If-Else Statement

  • Simple If

      #Syntax
      if [expression]
       then
          statement
      fi
    
      #Example 
      if [ ${A} -gt ${B} ]
       then
          echo "${A} is greater than ${B}"
      fi
    
  • If-Else

      #Syntax
      if [expression]
      then
          statement
      else 
          statement
      fi
    
      #Example 
      if [ ${A} -gt ${B} ]
      then
          echo "${A} is greater than ${B}"
      else 
          echo "${B} is greater than ${A}"
      fi
    
  • Multiple If

      #Syntax
      if [expression]
      then
          Statement
      elif [expression]
      then
          Statement
      elif [expression]
      then
          Statement
      else
          Statement
      fi
    
      #Example 
      if [ ${age} -gt 0 -a ${age} -lt 13 ]
      then
          echo "You are a kid"
      elif [ ${age} -gt 13 -a ${age} -lt 18 ]
      then
          echo "You are a teen"
      else
          echo "Invalid age"
      fi
    
  • Nested - If

      #Syntax
      if [expression]
      then
          Statement
          if [expression]
          then
              Statement
          else
              Statement
          fi
      else
          Statement
      fi
    
      #Example 
      if [ ${Fname} == Kakashi]
      then
          echo "First name matched"
          if [ ${Sname} == Hatake ]
          then
              echo "First name and Clanname both matched"
          else
              echo "First name matched but Clanname doesn't matched"
          fi
      else
          echo "First name doesn't matched stopped execution"
      fi
    

Hope you loved the blog from a naive blogger...

10
Subscribe to my newsletter

Read articles from Arfat Kadvekar directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Arfat Kadvekar
Arfat Kadvekar

A Naive Developer