Linux & shell programming


  • - It is a popular Multiuser and Network OS
  • - It was written by Linus Torwald in 1991 as a core program
  •    for Computer's Operating System called as Kernel.
  • - Linux is also called as Extended version of Unix OS.
  • - Linux is an open source kernel that is freely available on
  •   Internet and no licence required to use it.
  • - programmers can download from internet and edit it.

- There are many distributer companies of Linux they provide the different flavor of Linux :

  •  RedHat Linux
  • Ubuntu Linux
  • Gintu  Linux
  • Fedora Linux
  • Debian Linux
  • Linux Mint

 Linux Provides GUI as well as CUI .........

  •  CUI Environment of Linux is known as Linux shell or Terminal
  • The commands used in linux , are case sensitive and usually they are written in lower case(small letter).

====================================================

Characteristics / Properties / Advantages / Services of Linux :

1. Multi User

              More than one user can loggin  simulteneously and perform the different

              task, over a single machine.

2. Multi Programming :

              Linux can operate more than one program at same

              time  using Time Scheduling.

3.Shared Library :

              Linux uses shared libray instead of making           multiple copies of file . It makes the use of less disk space and memory.

4. Compatible with Old Plateforms :

              It can easily install on old hardware plateform and

              processors.

5. Economic :

              To purchase the Linux software we have to pay

              a vary nominal charge/low cost. many of the   applications are freely available for linux os.

 

5. Samba :(Server Message Block)

              This is a protocal which is used to share the files

              in network and also beteen the different OS.

 

6. Dos Emulator

              linux provides a software that executes all the dos            commands

7. Apache Web Server

              Linux provides the web server - apache. this make

              the machine to be used as server and handle the request of client machine.

              Apache is also used to develop the web Application

              over the java and php

 

8. Multiple Distributers

               As the linux is open source, so there are multiple

 companies that sell the linux with some additional features. so user can go through according to their choice.

 

9. Development Library and gcc

               Linux provide some library and inbuilt compiler -gcc

               to create and excute the program in c, c++ , java etc.

 

 

10. No virus Attack

               There is not virus found in Linux OS till Now. so it is

               assumed as virus free OS

 

===========================================

Linux Architecture :

               ___________________________________________________

              |Shell                  Utitilities                           Application Program |

              |-------------------------------------------------------------------------------|

                                           Kernel

             

                                           Hardware

 

 

===========================================

Linux os is made of two elements : Shell and Kernel

 Shell:

  •               - It is the Interface provided  for the User
  •               - It manages the Commands,Instructions, Utilities and
  •                  application Software
  •               - It provides a terminal where user can type the commands
  •               - The command written over the shell  are case sensitive
  •               - Shell is also called as command line interpreter ,
  •               because it translate each command to machine code
  •               and passes to the kernel for further action   
==============================================================

             Linux provides different types of shell for different  task like --

              a) Korn shell (kcsh) : denoted by #

              b) Bourn Shell(bsh) : denoted by $

              c) c shell(csh)           : denoted by symbol similar to 'c'                              

Kernel :

              - It is the System program

              - It is core linux code that manages,controls and   operates the hardwares

              - It takes the commands or instruction from shell and perform the  action. the result generated by kernel is sent back to the shell                       

=============================================================

GUI On Linux:

              - Linux provides the GUI as well CUI .

              - The CUI of linux is provided by the shell

              - The GUI system of linux is called as X-Windows  System

              - There are two types of Xwindows systems are  available for linux :

              1.KDE(K-Desktop Environment)

              2.GNOME(GNU Object Model Environment)

===========================================

Hardware Requirement for Linux :

To install the Linux in our machine we need following Configurations:

 

a) Processor :     x86 or higher

b) RAM      :        256MB or higher

c) HardDisk  :     4GB free Space

d) Keyboard

e) Mouse

f) CD-Rom (for installation)

g) Installation CD of Linux Kernel

===========================================

Directory /file Structure of Linux OS

-------------------------------------

- Linux provides an Inbuild Directory System to keep system and

   user's files.

- There are many inbuilt directories in Linux OS

 

- Each directory is followed by / sign

 

 

                                           / (root)

 

/bin       /boot    /usr       /etc       /home  /dev      /lib

   cat                                                      Ram (user)   

   cp                                                    Desktop           

   chmod                                                          Start

   ls

   ...

 

/bin:

- it store the linux utilities (commands) in Binary formate

- some linux command are :

   cat,cp,ls,dir,mkdir,cd,date,time,.....

 

/boot :

- it stores the bootable files

- these files are loaded to memory when computer starts

- they are necessory to start the linux

 

/usr:

- it stores the files that are not used in booting process

 

/etc:

- it stores all the system related files.

- It also stores the user,group,account,memory related database.

 

/home:

- it stores the home directories of all the users

- all the user accounts are itself come in home directory

- for examle if naveen is the home directory for user NAVEEN, then it is written like :

              /home/naveen

- all the files created by user NAVEEN are placed inside the home

directory of user (/naveen) like Desktop,documents,....

 

/dev :

- It  store the information about  the hardware devices like- Hard disk, pendrive,Printer, Scanner,....

- It stores the Driver related information

 

/lib

- It stores the inbuilt libraries

- These libraries are used for shell programming and other types of language features

=================================================================

File types in Linux :

 we can catagories the files in 3 types in linux :

 1. Ordinary files

 2. Directory files

 3. Special Files

 

Ordinary files:

              - These are the common types of file

              - These are created by users

              - ex:- Data files, Program files, executable file...

              - They contains the data/text/number etc.

             

Directory files :

              - These are container of other files

              - There are many of the inbuilt directory files in Linux

              that holds the different types of files.

              - These are followed by '/'

              ex:- /bin, /boot, /home, /usr,......

 

 Special Files :

              - These are the System Files

              - They can be following types :

                             a) Character Device Files

                             b) Block Device Files

                             c) Hard Disk Files

                             d) Symolic Disk Files

 

===========================================

              Working with RedHat Linux :

 

1. start up and Shutdown

2. Login and Logout

3. Working with Desktop Icons

4. Setting the Desktop Environment

5. Working with Linux Shell

6. Using Linux Basic Commands

7. Creating User Account

8. Working with basic Utilities of RedHat Linux :

                             Text Editor

                             Graphics Editor

                             Calculator

                             Music Player

                             Web Browser

                             ....

 

StartUp and ShutDown :

              - when ever we start the Linux, it alway ask for username and password

             

              - First time we have to use the System's Account to

login (start) the Linux

             

             

              - After starting the linux , we can also create our personal user account (user name and password) and the next time we use our personal account to Login.

 

              - A Desktop is get displayed just similar to windows OS

that may be either KDE or GNOME

             

              - The Desktop contains some Icons , the Start Button   and task bar

 

              - To turnoff the Linux , we use the ShutDown options

from the system or start menu

 

===========================================

Opening Linux Shell : (Login and Logout)

 

              we can open multiple (atleast 6 ) terminal in Linux at

              a time

 

              to goto the terminal (or shell) we press :

              ctrl+alt+f1..f6

 

              when we open the terminal it will ask for user name and password

             

              when you type the name and password, keep the  proper case (both are case sensitive)

 

               for security reasons , linux do not show the password

               characters on screen when you type it

 

               after successfully login, linux shows the terminal with

              a sign either # or $

             

              # denotes the root user or Administrator

              $ denotes the local user (other user)

 

              to logout from current account type the command-

                - logout

                or

                - exit

                            

              to come back to desktop environment press :

               ctrl+alt+f7

===========================================

RedHat Desktop Icons :

             

              - user's Home(like-ram's Home) // My Documents

              - Trash                               // Recycle Bin

              - Computer                      // My Computer

 

An Aditional Icon will be shown when we plug the pen drive / flash drive

 

 User's Home :

               It is the Home directory of User

               It is similar to the My-Document icon in Windows

               It stores all the data / files created by user

               Name is Like to :

                             Ramesh's Home : /home/Ramesh

                             Ram's Home : /home/Ram

                             Root's Home : /home/root

 

                             ...

                The main Directory inside the Home directory is

                'Desktop'

 

Trash :

              - This is the program  that holds the deleted files

              - It is similar to the windows Recycle Bin

Computer :

              - It shows all the information about the hardwares 

                 connected to system

              - It also shows the local disk and other directories stored over them

              - It is similar to the MyComputer in Windows OS

             

 

 

 

 

 

Linux Basic Practices :

              Changing Desktop Wallpapers

              Creating and removing Folders

              Renaming Folders

              Customzing files and folders

              Creating TextFiels

              Copy,Move,Cut,Paste,.....

              Creating User Accounts

             

 

 

Working with Linux Commands :

 

- to run the linux command on Redhat or Ubuntu , open the

terminal   (Desktop  | right click | open terminal) Or (System -> Terminal)

 

- or you can also open the shell by pressing ctrl+alt+f1..f6

 

- In shell you have to again type the user name and password

 

- The Shell will be either willbe displayed  like :-

             

[computerName / ] #                    (for root or admin)

or

[computerName/home/username] $      (for local user)

----------------------------------------------------------------------------

To run linux command online :

http://simpleshell.com/#

or

freelinuxconsole.info/terminal

or

tutorialspoint.com/execute_bash_online.php

----------------------------------------------------------------------------

Commands :

 

1. ls Command :

              - this command is used to see the list of files and

               directories

              - only the name will be displayed

 

              - we can use the options with ls command

 

              - any option is followed by a single space and dash (-)

 

syntax:

              ls  [option]

             

 

options :

              -a : all

              -d : directories

              -r  : reverse

              -l  : long listing formate

ex:

$ ls -l

 

----------------------------------------------------------------------------

2. clear command :

              - It is used to clear all the contents from display screen

              - It is similar to cls command of the dos

 

syntax:

              $ clear

----------------------------------------------------------------------------

3. pwd command :(Print working directory)

              This command is used to display the complete path

of current working directory.

 

ex:

 $ pwd

 

output:  /home/gist

--------------------------------------------------------------------------------------

4. mkdir Command (Make Directory) :

  This command is used to create new directory.

  It is similar to md command of the DOS

  Directory is created under the current directory

syntax:

              mkdir    Dir-name

ex:

 $ ls

output:-(empty)

               

$ mkdir Ram

$ ls

output:-

     Ram

---------------------------------------------------------------------------

5. cd command : (Change Directory)

              - This command is used to change the current working directory.

 

we can use this command either to go inside the directory or

jump outside of directory.

 

 

syntax :

a) cd   directory-Name

              // to open directory

 

b) cd  ..

              // back to parent directory

 

c) cd  /

              // back to root or home directory

 

ex:

$ mkdir ram

$ cd ram

$ pwd

output :/home/gist/ram

$ cd ..

$ pwd

output : /home/gist

--------------------------------------------------------------------------------------

6. cat Command :

              - This command is used for three purpose

              i)  creating a new file

              ii) displaying the content of file

              iii)adding new content at the end of file(appending)

 

i) creating new file:

 

              cat > fileName

ex:

              cat > ram.txt

              ......

              ......

              ......

              .......

              ctrl+d

 

ii) Displaying the contents of file :

              cat FileName

ex:

              cat ram.txt

              ...............

              ...............

              ................

              .................

 iii) Appending text to file :

              cat >> fileName

 

ex:

              cat >> ram.txt

              ......................

              ...................... type some text

              ^d

 

note:

              these lines are added at the end of the file- ram

              to see the new content you can use the command cat    

              like:

              cat ram.txt

              ..............

              ..............

              ..............

              .............. //old data

              .................

              ................. // new data

 

 

 

7. rm command (remove) :

              This command is used to remove(delete)any file.

 

 syntax:

              rm <FileName>

ex:

$ rm dca.txt

 

 

8. rmdir Command :

              This command is used to remove or delete any

              empty directory

 

syntax:

              rmdir <dirName>

ex:

$ mkdir ram

$ ls

ram      

$ rmdir ram

$ ls

 

 

9.cp Command : (copy command)

              This command is used to create the copy of any file

 

              Copy can be created either inside another directory

              or wihin same directory

 

              In same directory, copy is created with different name

              but in another directory copy may be created with

              same name

             

              If file already exists, this command overwrite it.

 

Syntax:

              cp <fileName>  <new Name>

              OR

              cp <fileName>  <path>

              OR

              cp <fileName> <path/fileName>

             

ex:

$ mkdir ram

$ mkdir mohan

$ cd ram

$ cat > dca

dca is a diploma course

it is a one year course

the minimum eligibility is 12th

^d

 

Now we are going to create the copy of 'dca' with a new name

'dca1'

 

$ cp dca dca1

$ cat dca1

.......

.......

.......

//same matter as dca file

 

 

Now we are going to create the copy of dca inside 'mohan' directory

 

 

$ cp dca  /home/ravi/mohan/

 

$ cd ..

$ cd mohan

$ ls

dca

 

[~/mohan]$ cat dca

.....

.....

.....

 

 

10. mv command(move command) :

              - This command is much similar to the cp command

             

              - It is used to move the file one directory to another

              directory

 

              - when we move any file, the original file is get    repalced

 

              - When we move any file in same directory, it will

              work like rename

 

 

              - the syntax is same to cp command

 

 

Syntax:

              mv <fileName>  <new Name>

              OR

              mv <fileName>  <path>

              OR

              mv <fileName> <path/fileName>

             

ex:

$ cd ram

$ cat > dca

dca is a diploma course

it is a one year course

the minimum eligibility is 12th

^d

 

$ mv dca /home/ravi/mohan/

 

$ cd ..

$ cd mohan

$ ls

dca1

 

$ cat dca1

.....

.....

.....

 

-----------------------------------------------------------------------

rm Command :

              It is used to remove or delte the file

 

  syntax:

              rm FileName

 ex:

              [~/ravi] $ rm dca

 

 more Command :

              It is used to diplay the metter or text

              page by page

 

              $ more dca

 Note:

 we can not scroll down if we scrollup once

 

 

 less Command:

              It is similar to more command and used to

viwe the content page wise but we can scrollup as

well as scrolldown the page

 

              $ less dca

 

Working with Wild Cards :

              *  to specify any number of characters

              ?  to specify single character      

              [] to specify a set of character

 

 

 

 ex:

   $ ls r*

              (ram,ramesh,ramakant,rgpv....)

   $ ls r????

              (ram,rgvp,  ->only names upto 5 characters)

 

   $ ls [hit]

               

              (rohit, hitwada, mohit, hitlar,...)

 

 

  date Command :

   It is used to show the date and Time of the system

   We can use following options with date command :-

 

 %m       number of Month

 %d        day of month (number)

 %y        Year (last two digits)

 %D       date in mm/dd/yy formate

 %H       Hourse (00 to 23)

 %M      Minuts (00 to 59)

 %S        Seconds (00 to 59)

 %T        time in formate -  HH:MM:SS     

 %h        Name of month like(jan,feb,mar,...)

 

 

 Note : these options are used with single ''

  or double quote " "  with + sign

 

 ex:

              $ date "+%D"

              10/07/13

 

 who Command :            

              It is used to display the name of user who           

              is logged in.

 

              It shows the name of termintal, date, time

              also

 

              $ who

 

 man Command :

              It is used to show the manual of any command

             

              we can get the help about any command using

              the man command.

              Ex:

              $ man ls

              $ man mkdir

 

--------------------------------------------------------------------

wc Command :

              This command is used to count the characters , word

              or lines in a file

 

Synatx:

              $ wc <options> <fileName>

 

Options :

              -l            : count line

              -w          : count words

              -c           : count characters

 

example :

              $ cat > ram

              ram is my friend

              ram is a good boy

              ^d

 

              $ wc -l ram

              output: 2

 

              $ wc -w ram

              output: 9

 

              $ wc -c ram

              output: 33  (count with space)

 

              $ wc ram

              2     9     33   (line words characters)

 

 sort Commnad :

              This command is used to re-arrange the row of

              file in ascending or descending order based on

              given option.

 

Syntax:

              $ sort <option> <fileName>

 

Options:

              -r            : reverse order (descending order)

              -n           : according to numeric filed

             

example:

              $ cat > students

               ram

               aakash

               amar

               vineeta

               bani

               chhavi

               pinky

               zeenat

               fatima

               ^d

 

              $ sort students

 output :

              aakash

              amar

              bani

              chhavi

              fatima

              pinky

              ram

              vineeta

              zeenat

 

              $ sort -r students

output:

              zeenat

              vineeta

              ram

              pinky

              fatima

              chhavi

              bani

              amar

              aakash

 

 

find Command :

              - In Linux find command is used to  lacate any

              file

              - It searchs for the given file and show the path or

              location where file is found

 

Syntax:

              $ find <directory>           <option>  <filename>

Option :

              -name  : to specify the name of file

 

 

example:

              $ find /home/ravi           -name  dca

                             this command search the file 'dca' in directory 'ravi'

 

              $ find -name dca

                             this command search file 'dca' in current directory

             

 

----------------------------------------------------------------------------

grep/egrep/fgrep Command :

              - This command is used to search for any text/string/

              or any pattern of text inside the file

 

              - This will show the lines that contains the specified

              pattern

 

              -It is a filter

             

Syntax:

              $ grep <pattern>  <fileName>

 

 

Expression for pattern:

              []            : match to any character

              ^            : match that text is placed at starting of line

              $            : match taht text is placed at end of line

                             it is used after text.

 

example:

              $ cat > dca

              dca is a one year computer program

              this program can be done by any student

              dca offers many computer related subjects

              fundamental, pc pkg and access are the main subjects

              ^d

 

              $ grep "dca" dca

              output:

                dca is a one year computer program

                dca offers many computer related subjects       

               

              $ grep "ma[ni]" dca       

              output:

              dca offers many computer related subjects

              fundamental, pc-pkg and access are the main subjects

             

              $ grep "program$" dca

              output:

              dca is a one year computer program

 

Other Options :

              -n           : print line with line numbers

              -c           : only count the line (not to show)

              -v           : show line where text is not found

 

---------------------------------------------------------------------------

bc Command :

              - This command is used to open binary calculator

              - This calculator performs simple arithmetic expression

              - To close it , type 'quit'

              - You have to type the expression like:

                             10 + 5

              when you press enter the result will be displayed

 

syntax:

              $ bc

 

example:

              $ bc

             

               4 + 4 [enter]

               8

             

              2+2-1 [enter]

              3

 

              2+2*2-2 [enter]

              4

 

              quit

----------------------------------------------------------------------------

cal Command:

              - This command is used to show the calander

              - we can show the calander of current month, or

              any other month of any year or calender of 12 months

 

Syntax:

              $ cal                     : show calander of current month

              $ cal 2011           : show calander of year 2011

              $ cal 05 2013     : show calander of May of year 13

---------------------------------------------------------------------------

cmp Command:

              - This command is used to compare two files.

              - If the files are same , it will show nothing

              - If the files are different,it will show the byte(char.)   and line number where first different is found.

 

 

Syntax:

              $ cmp  <file1>   <file2>

 

Example :

               $ cat > file1

              this is file1

              created by vikas pathak

              ^d

 

               $ cat > file2

              this is file2

              created by vikas pathak

              ^d

 

               $ cat > file3

                this is file1

                created by vikas pathak

              ^d

 

              $ cmp file1 file2

              output:

              file1 file2 differ: char 13, line 1

 

               $ cmp file1 file3

                            //No output (same file)

---------------------------------------------------------------------------

tty Command :

              print the file name of the terminal connected to  standard input (keyboard)

 

Syntax:

              $ tty

 

----------------------------------------------------------------------------

File Access Permissions and Owership :

              - In linux any file have following types of acessing permissions

              a) read  : denoted by r

              b) write : denoted by w

              c) execute          : denoted by x

 

 

owenrship of file, determine the user who access the file.

in linux there may be following types of user can access any file -

              a) owner (one who creates file)  : denoted by u

              b) group (set of multiple users)  : denoted by g

              c) other  (user belongs to other group): denoted by o

              d) all      : denoted by a

 

to work with the ownership and access permission, following commands are used:

a) chgrp              : to change the group

b) chown            : to change the ownership

c) chmode          : to change the access mode

 

Note : to see all the users and access mode, we use the command ls -l

              $ ls -l

              output:

               -rw- r-- r--  1   rob    rob 35  Jun 13 21:14      file.txt

                  |                |     |                                         |

                  |                |     |                                         |

                  | owner <-----        ------>group              ----> File name

                  |

                  |

              Access permissions

              for user,group and others

 

chgrp Command:

 The chgrp command is used to change the group that has access  to files and directories.

 

 All files in linux belong to an owner, and a group.

 

 The owner is set by the chown command, and the group by the  chgrp command.

 

Syntax:

              $ chgrp <groupName>  <fileName>

 

Example :

              $ ls -l

              output:

               -rw- r-- --- 1 rob rob 35 Jun 13 21:14 file.txt

                                     |     |                                         |

                                     |     |                                         |

                    owner           <-----      ------>group                     ----> File

 

              $ chgrp clients file.txt

                             |

                             |

                             -->new group

             

              $ ls -l

              output:

              -rw- r-- --- 1 rob clients 35 Jun 13 21:14 file.txt

 

chown Command :

              This command is used to change the owner of the file

 

syntax:

              $ chown <user> <fileName>

 

ex:

              ls -l

 

Example :

              $ ls -l

              output:

               -rwxrwx--- 1 rob rob 35 Jun 13 21:14 file.txt

                                     |     |                                         |

                                     |     |                                         |

                    owner           <-----      ------>group                    ----> File

 

              $ chown ram file.txt

                             |

                             |

                              -->new owner

             

              $ ls -l

              output:

              -rwxrwx--- 1 ram rob 35 Jun 13 21:14 file.txt

----------------------------------------------------------------------------------

chmod Command :

              This command is used to change the access permission of

              file

 

 

syntax :

              chmod   <user> +/- <permission>   <fileName>

 

ex:

              $ chmod o+w  file.txt

 

              $ ls -l

              output:

              -rw- r-- rw- 1 rob rob 35 Jun 13 21:14 file.txt      

 

              $ chmod o-r file.txt

              $ ls -l

              output:

               -rw- r--  -w- 1 rob rob 35 Jun 13 21:14 file.txt

 

              $ chmod g+x file.txt

              $ ls -l

              output:

               -rw- r-x r-- 1 rob rob 35 Jun 13 21:14 file.txt

 

 

              $ chown g+w file.txt

              $ ls -l

              output:

               -rw-  rw- --- 1 rob rob 35 Jun 13 21:14 file.txt

 

 

Text Editors in Linux :

              Text Editor is a software or program that comes with

              the Operating System.

              It is used for  editing and Management of the Text Files.

             

              Task of Editor :

              ->To create documents and Manage them

              ->To write Program and Utilities

              ->To write Mails

              ->To Open & Edit the Text files

              ->Cut,Copy,Paste of the Text

              ->Find or Search the Text

 

Following types of Editors are available in Linux OS :

vi           : visual Editor

vim        : visual editor improved

emacs   : edit macro editor

sed        : stream editor

led         : line editor

red        : restricted ed editor

joe         :joe's own editor

 

 

the most popular editors are - vi and emacs

 

 

vi Editor :

              It is available in all of the versions of linux and unix operating

              System

 

              It is used to create , open, edit ,append or change the text

              in the file.

 

how to Open vi editor :

              $ vi  <file Name>

 

If the File already exist, then the text of file is get displayed other wise

a new blank file is created.

 

 

 

vi Editor Modes:

              Command Mode

              Insert Mode

              Append Mode

 

Command Mode :

              It is the default mode of the vi editor

              when we open the vi editor, it is set on command mode

              all vi command are executed in this mode

Insert Mode :

              It is used to write the text before current cursor position

              To change the mode from command to insert mode, we press

              'i'

 

Append Mode :

              It is similar to insert mode and used to type the text

              It do not over write the text instead it add the character 

              after the currnet character

              We press 'a' to initialize append mode

 

Note :

              we press 'esc' key to go back to command mode

              to save and exit we press-   :wq <enter>

 

ex:

              $ cat > dca

              dca is a computer course

              it is a one year computer diploma

              ^d

 

 

              $ vi dca

                

              dca is a computer course

              it is a one year computer diploma

              -

              -

              -

              -

              -

              -

              :wq

             

             

 

Commands of vi Editor :

 

h            : to move previous character

l             : to move next character

k            : to move one line above

j             : to move one line down

x            : to delete current cursor position character

 

dd          : to delete current line

:wq        : to save and exit

:w          : to save only

:q!         : to exit without saving

Zero(0) : to move starting of line

$            : to move end of line

 

w           : to move next word

b            : to move previous word

e            : to move end of word

dw         : to delete word

 

a            : to insert after the cursor

A            : to insert end of line

i             : to insert before cursor

I             : to insert begning of line

o            : to insert blank line below

O           : to insert blank line above

Sed command

expr command in Linux with examples

  • Difficulty Level : Medium
  • Last Updated : 15 May, 2019

·  Read

·  Discuss

The expr command in Unix evaluates a given expression and displays its corresponding output. It is used for:

·       Basic operations like addition, subtraction, multiplication, division, and modulus on integers.

·       Evaluating regular expressions, string operations like substring, length of strings etc.

Syntax:

$expr expression

Options:

·       Option –version : It is used to show the version information.

Syntax:

$expr --version

Example:

·       Option –help : It is used to show the help message and exit.

Syntax:

$expr --help

Example:

Below are some examples to demonstrate the use of “expr” command:

1. Using expr for basic arithmetic operations :

Example: Addition

$expr 12 + 8

Example: Multiplication

$expr 12 \* 2

Output

Note:The multiplication operator * must be escaped when used in an arithmetic expression with expr.

2. Performing operations on variables inside a shell script

Example: Adding two numbers in a script

echo "Enter two numbers"

 

read x

 

read y

 

sum=`expr $x + $y`

 

echo "Sum = $sum"

Output:

Note: expr is an external program used by Bourne shell. It uses expr external program with the help of backtick. The backtick(`) is actually called command substitution.

3. Comparing two expressions

Example:

x=10

 

y=20

 

# matching numbers with '='

res=`expr $x = $y`

echo $res

 

# displays 1 when arg1 is less than arg2

res=`expr $x \< $y`

echo $res

 

# display 1 when arg1 is not equal to arg2

res=`expr $x \!= $y`

echo $res

Output:

Example: Evaluating boolean expressions

# OR operation

$expr length  "geekss"  "<"  5  "|"  19  -  6  ">"  10

Output:

# AND operation

$expr length  "geekss"  "<"  5  "&"  19  -  6  ">"  10

Output:

4. For String operations

Example: Finding length of a string

x=geeks

 

len=`expr length $x`

 

echo $len

Output:

Example: Finding substring of a string

x=geeks

 

sub=`expr substr $x 2 3`

#extract 3 characters starting from index 2

 

echo $sub

Output:

Example: Matching number of characters in two strings

$ expr geeks : geek

Output:

 

 

grep command in Unix/Linux

  • Difficulty Level : Easy
  • Last Updated : 15 Nov, 2022

·  Read

·  Discuss

The grep filter searches a file for a particular pattern of characters, and displays all lines that contain that pattern. The pattern that is searched in the file is referred to as the regular expression (grep stands for global search for regular expression and print out). 
Syntax: 
 

grep [options] pattern [files]

 

Options Description
-c : This prints only a count of the lines that match a pattern
-h : Display the matched lines, but do not display the filenames.
-i : Ignores, case for matching
-l : Displays list of a filenames only.
-n : Display the matched lines and their line numbers.
-v : This prints out all the lines that do not matches the pattern
-e exp : Specifies expression with this option. Can use multiple times.
-f file : Takes patterns from file, one per line.
-E : Treats pattern as an extended regular expression (ERE)
-w : Match whole word
-o : Print only the matched parts of a matching line,
 with each such part on a separate output line.
 
-A n : Prints searched line and nlines after the result.
-B n : Prints searched line and n line before the result.
-C n : Prints searched line and n lines after before the result.

Awk is a scripting language used for manipulating data and generating reports. The awk command programming language requires no compiling and allows the user to use variables, numeric functions, string functions, and logical operators. 

Awk is a utility that enables a programmer to write tiny but effective programs in the form of statements that define text patterns that are to be searched for in each line of a document and the action that is to be taken when a match is found within a line. Awk is mostly used for pattern scanning and processing. It searches one or more files to see if they contain lines that matches with the specified patterns and then perform the associated actions. 

Awk is abbreviated from the names of the developers – Aho, Weinberger, and Kernighan. 

WHAT CAN WE DO WITH AWK? 

1. AWK Operations: 
(a) Scans a file line by line 
(b) Splits each input line into fields 
(c) Compares input line/fields to pattern 
(d) Performs action(s) on matched lines 

2. Useful For: 
(a) Transform data files 
(b) Produce formatted reports 

3. Programming Constructs: 
(a) Format output lines 
(b) Arithmetic and string operations 
(c) Conditionals and loops 

Syntax:

awk options 'selection _criteria {action }' input-file > output-file

Options:  

-f program-file : Reads the AWK program source from the file 
                  program-file, instead of from the 
                  first command line argument.
-F fs            : Use fs for the input field separator

Sample Commands 

Example: 

Consider the following text file as the input file for all cases below: 

$cat > employee.txt 
ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000 

1. Default behavior of Awk: By default Awk prints every line of data from the specified file.  

$ awk '{print}' employee.txt

Output:  

ajay manager account 45000
sunil clerk account 25000
varun manager sales 50000
amit manager account 47000
tarun peon sales 15000
deepak clerk sales 23000
sunil peon sales 13000
satvik director purchase 80000 

In the above example, no pattern is given. So the actions are applicable to all the lines. Action print without any argument prints the whole line by default, so it prints all the lines of the file without failure. 

2. Print the lines which match the given pattern. 

$ awk '/manager/ {print}' employee.txt 

Output:  

ajay manager account 45000
varun manager sales 50000
amit manager account 47000 

In the above example, the awk command prints all the line which matches with the ‘manager’. 

3. Splitting a Line Into Fields : For each record i.e line, the awk command splits the record delimited by whitespace character by default and stores it in the $n variables. If the line has 4 words, it will be stored in $1, $2, $3 and $4 respectively. Also, $0 represents the whole line.  

$ awk '{print $1,$4}' employee.txt 

Output:  

ajay 45000
sunil 25000
varun 50000
amit 47000
tarun 15000
deepak 23000
sunil 13000
satvik 80000 

In the above example, $1 and $4 represents Name and Salary fields respectively. 

Touch command:-

touch command in Linux with Examples

  • Difficulty Level : Easy
  • Last Updated : 19 Feb, 2021

·  Read

·  Discuss

The touch command is a standard command used in UNIX/Linux operating system which is used to create, change and modify timestamps of a file. Basically, there are two different commands to create a file in the Linux system which is as follows:

·       cat command: It is used to create the file with content.

·       touch command: It is used to create a file without any content. The file created using touch command is empty. This command can be used when the user doesn’t have data to store at the time of file creation.

Using touch Command

Initially, we are in home directory and this can be checked using the pwd command. Checking the existing files using command ls and then long listing command(ll) is used to gather more details about existing files. As you can see in the below figure there is no existing files.

Touch command Syntax to create a new file: You can create a single file at a time using touch command.

Syntax:

touch file_name

The file which is created can be viewed by ls command and to get more details about the file you can use long listing command ll or ls -l command . Here file with name ‘File1‘ is created using touch command.

Touch command to create multiple files: Touch command can be used to create the multiple numbers of files at the same time. These files would be empty while creation.

Syntax:

touch File1_name File2_name File3_name 

Multiple files with name Doc1Doc2Doc3 are created at the same time using touch command here.

touch Command Options

Like all other command Touch command have various options. These options are very useful for various purpose.

touch -a: This command is used to change access time only. To change or update the last access or modification times of a file touch -a command is used.

Syntax:

touch -a fileName

Here touch -a command changes access time of the file named Doc1.

touch -c : This command is used to check whether a file is created or not. If not created then don’t create it. This command avoids creating files.

Syntax:

touch -c fileName

touch -c-d : This is used to update access and modification time.

Syntax:

touch -c-d fileName

touch -m : This is used to change the modification time only. It only updates last modification time.

Syntax:

touch -m fileName

touch -r : This command is used to use the timestamp of another file. Here Doc2 file is updated with the time stamp of File 1.

Syntax:

touch -r second_file_name first_file_name

touch -t : This is used to create a file using a specified time.

Syntax:

touch -t YYMMDDHHMM fileName

====================
Uniq command:

The linux uniq command is basically used to remove all repeated lines in a file. This command is used when a line is repeated multiple times and replace these multiple lines with one line. This command is designed to work on sorted files.

Purpose : Remove repetitious lines from the sorted ‘input-file’ and send unique lines to the ‘output-file’. If no ‘output-file’ is specified, the output of the command is send to standard output. if no ‘input-file’ is specified, the command takes input from standard output.

Syntax :

$ uniq [option] [input-file] [output-file]

Options :

-c : Precede each output line by the number of times it occur.
-d : Display the repeated lines.
-u : Display the lines that are not repeated.

Examples :
Create a sample input file.

$ cat sample

Input :
This is a test file for the uniq command.
It contains some repeated lines.
It contains some repeated lines.
And some are different.
It contains some repeated lines.
It contains some repeated lines.

1. Removing duplicate lines when file is not sorted.

$ uniq sample

Output :
This is a test file for the uniq command.
It contains some repeated lines.
And some are different.
It contains some repeated lines.

2. Removing duplicate files after sorting.

$ sort sample -> sample1
$ uniq sample1

Output :
And some are different.
It contains some repeated lines.
This is a test file for the uniq command.

3. Finding number of times the lines are repeated.

$ uniq -c sample1

Output :

1 And some are different.
4 It contains some repeated lines.
1 This is a test file for the uniq command.

4. Finding repeated lines.

$ uniq -d sample1

Output :

It contains some repeated lines.

5. Finding lines which are unique and store in another file.

$ uniq -u sample1 out
$ cat out

Output :

And some are different.
This is a test file for the uniq command.
 
 
 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

             

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Comments

Popular posts from this blog