+ All Categories
Home > Documents > Unix Basics

Unix Basics

Date post: 01-Feb-2016
Category:
Upload: sunilrgurav
View: 223 times
Download: 0 times
Share this document with a friend
Description:
Unix Basics
Popular Tags:
41
Unix presentation Unix presentation UNIX BASICS UNIX BASICS
Transcript
Page 1: Unix Basics

Unix presentation Unix presentation

UNIX BASICSUNIX BASICS

Page 2: Unix Basics

This training will cover :This training will cover :

UNIX HistoryUNIX History UNIX ArchitectureUNIX Architecture Basic Unix CommandsBasic Unix Commands File SystemFile System File CommandsFile Commands Vi EditorVi Editor Intro to AWKIntro to AWK

Page 3: Unix Basics

UnixUnix

UNIX HistoryUNIX History

UNIX UNIX was developed at AT&T Bell was developed at AT&T Bell Laboratories by Ken Thompson and Laboratories by Ken Thompson and Denis Ritchie with the intention of Denis Ritchie with the intention of creating a portable operating creating a portable operating system. It was finally written in C, system. It was finally written in C, and now runs on all machines from and now runs on all machines from notebook to mainframes. notebook to mainframes.

Page 4: Unix Basics

UNIX ArchitectureUNIX Architecture

UNIX Architecture is aUNIX Architecture is a C Computer Operating omputer Operating System Architecture that embodies the Unix System Architecture that embodies the Unix philosophy.philosophy.

Page 5: Unix Basics

UNIX ArchitectureUNIX Architecture

SHELL: No command can be executed unless it SHELL: No command can be executed unless it

obtains clearance of the shell. obtains clearance of the shell. • The shell when analyzes the command and its The shell when analyzes the command and its

associated argument, often modifies and associated argument, often modifies and simplifies its input before it forwards it to simplifies its input before it forwards it to another agency which actually executes the another agency which actually executes the command. This agency is called as Kernel. command. This agency is called as Kernel.

• Because unix permits the use of complex Because unix permits the use of complex command structure that can’t be understood by command structure that can’t be understood by the Kernel, the shell has to take the role of the Kernel, the shell has to take the role of interpreter to translate them in the forms that interpreter to translate them in the forms that the kernel can understand. the kernel can understand.

Page 6: Unix Basics

UNIX CommandsUNIX Commands whowho: Displays who is on the system.: Displays who is on the system. Syntax: whoSyntax: who Example: who am i Example: who am i who |wc –l (Count of users)who |wc –l (Count of users)

PASSWD: PASSWD: Allows you to change your password Allows you to change your password echoecho: A built-in : A built-in shellshell command that prints command that prints

informationinformation Example: echo $shellExample: echo $shell

Page 7: Unix Basics

UNIX CommandsUNIX Commands

EXIT or Ctrl+d : EXIT or Ctrl+d : Allows you to exit Allows you to exit from a program, shell or log you out from a program, shell or log you out of a Unix networkof a Unix network

TELNET: CTELNET: Connects to another onnects to another remote computer.remote computer.

Example: telnet host.comExample: telnet host.com

Page 8: Unix Basics

File PermissionsFile Permissions

There are three specific permissions on Unix-like There are three specific permissions on Unix-like systems that apply to every class systems that apply to every class • The The READREAD permission, which grants the ability permission, which grants the ability

to read a file.to read a file.• The The WRITE WRITE permission, which grants the permission, which grants the

ability to modify a file. ability to modify a file. • The The EXCUTEEXCUTE permission, which grants the permission, which grants the

ability to execute a file. This permission must ability to execute a file. This permission must be set for executable binaries in order to allow be set for executable binaries in order to allow the operating system to run them. the operating system to run them.

Page 9: Unix Basics

File CommandsFile Commands CDCD: cd is used to change the current working : cd is used to change the current working

directorydirectory.. ExampleExample: : $ cd users/students.$ cd users/students. $ cd #To move to the user's home $ cd #To move to the user's home

directorydirectory $ cd .. To move 1 directory back.$ cd .. To move 1 directory back. $ cd – to move to previous directory$ cd – to move to previous directory

LS: LS: Lists the contents of a directory Lists the contents of a directory SYNTAX: SYNTAX: ls [-a] [-d] [-l] [ [-r] [-s] [-t] [pathnames]ls [-a] [-d] [-l] [ [-r] [-s] [-t] [pathnames]

Page 10: Unix Basics

File CommandsFile Commands

CHMOD: CHMOD: Changes the Changes the permission of a file.permission of a file.

SYNTAX: SYNTAX: chmod MODE[,MODE] chmod MODE[,MODE] FILE...FILE...

ExampleExample: : chmod 644 file.htmchmod 644 file.htm

Page 11: Unix Basics

File CommandsFile Commands RMRM: : Deletes a file without confirmation (by default).Deletes a file without confirmation (by default). SYNTAX: SYNTAX: rm [-i] [-R] [-r] [filenames | directory]rm [-i] [-R] [-r] [filenames | directory] ExampleExample: rm -r directory - remove a directory, even : rm -r directory - remove a directory, even

if files existed in that directoryif files existed in that directory

MKDIR: MKDIR: CCreate a new directory.reate a new directory. SYNTAX: SYNTAX: mkdir dirnamemkdir dirname

RMDIRRMDIR: : Deletes a directory.Deletes a directory. SYNTAX: SYNTAX: rmdir [-p] [-s] file directoryrmdir [-p] [-s] file directory Example: Example: rmdir mydirrmdir mydir - removes the directory - removes the directory

mydirmydir

Page 12: Unix Basics

File CommandsFile Commands

CPCP: : Copies files from one location to another.Copies files from one location to another.

SYNTAX: cp [OPTION]... SOURCE DEST SYNTAX: cp [OPTION]... SOURCE DEST

Example: Example: cp *.txt newdircp *.txt newdir

Copy all files ending in .txt into the newdir directory. Copy all files ending in .txt into the newdir directory.

MV: MV: Renames a file or moves it from one directory to Renames a file or moves it from one directory to another directory.another directory.

SYNTAX: SYNTAX: mv [-f] [-i] oldname newnamemv [-f] [-i] oldname newname

Example: Example: mv myfile.txt newdirectorymv myfile.txt newdirectory

Page 13: Unix Basics

File CommandsFile Commands

WC: WC: Short for word count, wc displays a count of Short for word count, wc displays a count of lines, words, and characters in a file. lines, words, and characters in a file.

SyntaxSyntax: wc [-c ] [-l] [-w] [ file ... ] : wc [-c ] [-l] [-w] [ file ... ]

Example: Example: wc myfile.txtwc myfile.txt

PIPEPIPE: The special symbol | (PIPE) performs the job of : The special symbol | (PIPE) performs the job of connecting two commands. We can connect a connecting two commands. We can connect a number of UNIX commands in this way to perform number of UNIX commands in this way to perform

difficult task quite easily.difficult task quite easily. ExampleExample: $ ls | wc: $ ls | wc

Page 14: Unix Basics

File CommandsFile Commands

GREP: Finds text within a file. (-v,-i,-c) SYNTAX: grep [options] PATTERN [FILE...]

Example: grep "unix" *.htm

UNIQ: Report or filter out repeated lines in a file.

EXAMPLES: uniq myfile1.txt > myfile2.txt

Page 15: Unix Basics

File CommandsFile Commands

Sort CommandSort Command Read and sorts Read and sorts infileinfile and display the result on and display the result on

standard output, which has been redirected to standard output, which has been redirected to outfileoutfile: :

sort infile > outfile # sorts sort infile > outfile # sorts infileinfile and writes the and writes the results to results to outfileoutfile  

Write sorted output directly to  Write sorted output directly to  outfileoutfile. .

sort -o outfile infile  # same as in 1, but using an option -osort -o outfile infile  # same as in 1, but using an option -o   

Read and sort Read and sort infileinfile  "in place" writing to the same   "in place" writing to the same filefile

sort -o infile infile  # sort file "in place sort -o infile infile  # sort file "in place

Page 16: Unix Basics

Sort command continuedSort command continued

cat l1 |sort –uDisplay the sorted uniq records ls -al | sort +4nThis command performs a numeric sort on

the fifth column of the "ls -al" output. This results in a file listing where the files are listed in ascending order, from smallest in size to largest in size.

Page 17: Unix Basics

vi Editorvi Editor vivi is the standard UNIX text editor.is the standard UNIX text editor.

Command modeCommand mode• move cursor, save, delete text, quit vi, etc.move cursor, save, delete text, quit vi, etc.

Input modeInput mode• for inserting textfor inserting text• start by typing i; finish with start by typing i; finish with ESCESC• cannot quit, delete, etc. in this modecannot quit, delete, etc. in this mode• If in doubt, press If in doubt, press ESCESC a few timesa few times. This will . This will

put you back in command mode.put you back in command mode.

Page 18: Unix Basics

Inserting TextInserting Text

Move to insertion pointMove to insertion point

Switch to input mode:Switch to input mode: i i

Start typing; BACKSPACE or DELETE for deletionStart typing; BACKSPACE or DELETE for deletion

ESCESC finish; back in command modefinish; back in command mode

Over a slow network, the screen may not Over a slow network, the screen may not refresh properlyrefresh properly^L^L refresh screen (in command mode)refresh screen (in command mode)

No RETURN

Page 19: Unix Basics

ColonColon commands (e.g. commands (e.g. :q!:q!) and ) and searchsearch commands (e.g. commands (e.g. /text/text) require a ) require a RETURNRETURN..

Commands that start with a Commands that start with a letterletter (e.g. (e.g. ZZZZ, , GG) and control characters (e.g. ) and control characters (e.g. ^L^L) ) do do notnot require a require a RETURNRETURN

Changes are stored in a buffer, so you Changes are stored in a buffer, so you must save to change the file. must save to change the file.

When to type When to type RETURNRETURN

Page 20: Unix Basics

Finishing a vi SessionFinishing a vi Session

Get to command mode (press Get to command mode (press ESCESCs)s)

ZZZZ save changes to the file and quitsave changes to the file and quit(no (no RETURNRETURN))

:wq save changes to the file and :wq save changes to the file and quitquit

:q!:q! quit without saving quit without saving (press (press RETURNRETURN))

Page 21: Unix Basics

Basic Cursor MovementsBasic Cursor Movements

hh move cursor one place to move cursor one place to leftleft

jj down onedown onekk up oneup onell right oneright one

ww move forward one wordmove forward one wordbb back one wordback one worde moves forward to end of e moves forward to end of

wordword

No RETURNrequired!

Page 22: Unix Basics

Larger Moves-> All in ESC modeLarger Moves-> All in ESC mode

GG go to last linego to last line<number>G<number>G go to line go to line numbernumber

1G Go to first line1G Go to first line

$ Move to end of line$ Move to end of line

0 Move to beginning of line0 Move to beginning of line

^G^G shows the current line shows the current line numbernumber

^F^F Forward a screenForward a screen

^B^B Back a screenBack a screen

Page 23: Unix Basics

/text/text Search forward for textSearch forward for text

/func/func search for funcsearch for func

?text?text Search backward for Search backward for text text

?foo?foo search for search for foofoo backward backward

Type RETURN!

Page 24: Unix Basics

DeletionDeletion

Must be in command mode.Must be in command mode.

xx Delete Delete charactercharacter that cursor is on. that cursor is on.

dddd Delete current Delete current lineline..

DD Delete from cursor position Delete from cursor position to end of lineto end of line

uu UndoUndo last command last command

:i,jd:i,jd Delete lines i to jDelete lines i to jExampleExample :23,29d:23,29d Delete lines 23 to 29Delete lines 23 to 29

Page 25: Unix Basics

Special line numbersSpecial line numbers::.. means the current line numbermeans the current line number^̂ means line number 1means line number 1$$ means last linemeans last line

:.,$d:.,$d Delete from current line Delete from current line to the end of file.to the end of file.

rr replace character at the cursor replace character at the cursor position.position.

RR replace character to the right of the cursor replace character to the right of the cursor to end of lineto end of line

Page 26: Unix Basics

Cut & Paste with Deleted TextCut & Paste with Deleted Text CutCut commands remove text from the screen, and commands remove text from the screen, and

store it in a bufferstore it in a buffer

PastePaste commands copy text from the buffer to the commands copy text from the buffer to the screenscreen

d or dd or D d or dd or D delete from screen and store text in a delete from screen and store text in a buffer buffer

Move cursor to new locationMove cursor to new location

pp paste contents of bufferpaste contents of buffer to right of to right of cursor posncursor posn

Page 27: Unix Basics

Moving TextMoving Text Cut and Paste with moveCut and Paste with move

:i,j:i,jmmkk move lines i through j to move lines i through j to start after line kstart after line k

:3,8m10:3,8m10 move lines 3 to 8 to start after line move lines 3 to 8 to start after line 1010

:20m.:20m. move line 20 to after the current linemove line 20 to after the current line

:1,.m$:1,.m$ move lines 1 through current line move lines 1 through current line to the bottomto the bottom

Page 28: Unix Basics

File-related CommandsFile-related Commands

:w file:w file writes writes vivi contents to contents to newnew filefile

:w >> file:w >> fileappends to fileappends to file

:w! file:w! file writes over filewrites over file

:w!:w! writes over input filewrites over input file

:r file:r file read in file; places it read in file; places it starting starting

at current cursor positionat current cursor position

Page 29: Unix Basics

Text SubstitutionText Substitution

:1,$s/old/new/g:1,$s/old/new/g replace every replace every occurrence of occurrence of oldold

by by newnew. .

:1,$s/Hat/Cat/g:1,$s/Hat/Cat/g

Page 30: Unix Basics

::i,js/old/new/gi,js/old/new/g replace every replace every occurrence of old by occurrence of old by new between lines i new between lines i

and jand j

:2,200s/darshan/DARSHAN/g:2,200s/darshan/DARSHAN/g

:1,.s/fc/function/g:1,.s/fc/function/g from line 1 to currentfrom line 1 to current

:23,$s/pd/procedure/g:23,$s/pd/procedure/g from line 23 to endfrom line 23 to end

Page 31: Unix Basics

Customizing VI Customizing VI

:set ai(automatic indentation). :set ai(automatic indentation).

:set ic Ignore case when searching for a :set ic Ignore case when searching for a patterns.patterns.

:set nomagic Searches a pattern like g*:set nomagic Searches a pattern like g*

:set nu will set the line number:set nu will set the line number

/string :- search a perticular pattern with /string :- search a perticular pattern with in a file in a file

Page 32: Unix Basics

AWKAWKIt is an excellent filter and report writer. Many UNIX utilities generates rows and It is an excellent filter and report writer. Many UNIX utilities generates rows and columns of information. AWK is an excellent tool for processing these rows and columns of information. AWK is an excellent tool for processing these rows and

colu`mns, and is easier to use AWK than most conventional programming colu`mns, and is easier to use AWK than most conventional programming languagelanguage Some important AWK commands which can Some important AWK commands which can

help us in Daily activities.help us in Daily activities. 1)To display all the records with a particular 1)To display all the records with a particular

item like ‘X03’ from a file.item like ‘X03’ from a file. Awk ‘/X03/ {print } xxxxxx.impAwk ‘/X03/ {print } xxxxxx.imp 2)To display only particular columns of a item 2)To display only particular columns of a item

from a filefrom a file Awk ‘/X03/ {print $2, $3, $5}’ xxxxx.impAwk ‘/X03/ {print $2, $3, $5}’ xxxxx.imp

Page 33: Unix Basics

3) To display a certain range of 3) To display a certain range of rows lets says from Row 3 to 6.rows lets says from Row 3 to 6.

awk 'NR ==3, NR == 6 { print awk 'NR ==3, NR == 6 { print $2}' $2}' sp_onhand1_sap_mgarusb3.imp sp_onhand1_sap_mgarusb3.imp

Page 34: Unix Basics

4) To display all the records of 4) To display all the records of certain values in or optioncertain values in or option

awk '$2 == "X387" || $4 == awk '$2 == "X387" || $4 == "82.000" { printf "%d\n", $3 }' "82.000" { printf "%d\n", $3 }' sp_onhand1sp_onhand1 _sap_mgarusb3.imp_sap_mgarusb3.imp

Will display the 3rd field with 2nd Will display the 3rd field with 2nd field = "X387" or 4th field =82.000 field = "X387" or 4th field =82.000

Page 35: Unix Basics

Write a Shell scriptWrite a Shell scriptFirst line :-First line :-

#!/bin/ksh -x#!/bin/ksh -xOriginally, we only had one shell on unix. When ran a Originally, we only had one shell on unix. When ran a

command, the shell would attempt to invoke one of the command, the shell would attempt to invoke one of the exec() system calls on it. It the command was an exec() system calls on it. It the command was an executable, the exec would succeed and the command executable, the exec would succeed and the command would run. If the exec() failed, the shell would not give up, would run. If the exec() failed, the shell would not give up, instead it would try to interpret the command file as if it instead it would try to interpret the command file as if it were a shell script. This works fine as long as there is only were a shell script. This works fine as long as there is only one shell on the system. But what if you are using one shell one shell on the system. But what if you are using one shell as your interactive and want to run a script written in as your interactive and want to run a script written in another shell's language?another shell's language?

Page 36: Unix Basics

Write a Shell scriptWrite a Shell script

This is where the #! trick comes in. The idea of This is where the #! trick comes in. The idea of using # to represent a comment originated with using # to represent a comment originated with csh and was quickly added to the bourne shell. csh and was quickly added to the bourne shell. Now all shells know to ignore stuff after a #. So Now all shells know to ignore stuff after a #. So we can add a leading line something like "#! we can add a leading line something like "#! /usr/bin/ksh". To the shell this line is just a /usr/bin/ksh". To the shell this line is just a comment. But if the kernel tries to execute a file comment. But if the kernel tries to execute a file with this line, it will exec the specified interpreter with this line, it will exec the specified interpreter and pass the script to it. So shell scripts become and pass the script to it. So shell scripts become executable pretty much like real executables are. executable pretty much like real executables are. Now when a shell tries to exec a shell script, it Now when a shell tries to exec a shell script, it succeeds succeeds

Page 37: Unix Basics

Sample/Simple scriptSample/Simple script

vi example.kshvi example.ksh Now in insert mode (i)Now in insert mode (i) #!/bin/ksh#!/bin/ksh

clearclearecho "Good morning, world." echo "Good morning, world."

Esc:wq and execute Esc:wq and execute

./example.ksh./example.ksh

Page 38: Unix Basics

To run a scriptTo run a script

This will not run This will not run ..

chmod 777 example.kshchmod 777 example.ksh ./example.ksh./example.ksh

Page 39: Unix Basics

Run a script in backgroundRun a script in background

nohup example.ksh &nohup example.ksh & vi nohup.outvi nohup.out

Page 40: Unix Basics

Include all the functionality Include all the functionality described earlier in a single shell described earlier in a single shell

script.script. 1#!/bin/ksh1#!/bin/ksh

2clear2clear3echo "Good morning, world.“3echo "Good morning, world.“

4cp abc.txt bcd.txt4cp abc.txt bcd.txt

5mv abc.txt bcd.txt5mv abc.txt bcd.txt

grep –I who abc.txt >> mno.txt | cat grep –I who abc.txt >> mno.txt | cat mno.txtmno.txt

……..

……..esc:/ clear :1,$s/Good/Bad/… :set nu..esc:/ clear :1,$s/Good/Bad/… :set nu

Page 41: Unix Basics

QUESTIONS?QUESTIONS?


Recommended