You are on page 1of 41

Unix presentation

UNIX BASICS

This training will cover :

UNIX History
UNIX Architecture
Basic Unix Commands
File System
File Commands
Vi Editor
Intro to AWK

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

Unix

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

UNIX Architecture

SHELL: No command can be executed unless it


obtains clearance of the shell.
The shell when analyzes the command and its
associated argument, often modifies and
simplifies its input before it forwards it to
another agency which actually executes the
command. This agency is called as Kernel.
Because unix permits the use of complex
command structure that cant be understood by
the Kernel, the shell has to take the role of
interpreter to translate them in the forms that
the kernel can understand.

UNIX Commands

who: Displays who is on the system.


Syntax: who
Example: who am i
who |wc l (Count of users)
PASSWD: Allows you to change your password

echo: A built-in shell command that prints


information
Example: echo $shell

UNIX Commands

EXIT or Ctrl+d : Allows you to exit


from a program, shell or log you out
of a Unix network
TELNET: Connects to another
remote computer.
Example: telnet host.com

File Permissions

There are three specific permissions on Unix-like


systems that apply to every class
The READ permission, which grants the ability
to read a file.
The WRITE permission, which grants the
ability to modify a file.
The EXCUTE permission, which grants the
ability to execute a file. This permission must
be set for executable binaries in order to allow
the operating system to run them.

File Commands

CD: cd is used to change the current working


directory.
Example:
$ cd users/students.
$ cd
#To move to the user's
home directory
$ cd .. To move 1 directory back.
$ cd to move to previous directory
LS: Lists the contents of a directory
SYNTAX:
ls [-a] [-d] [-l] [ [-r] [-s] [-t] [pathnames]

File Commands

CHMOD: Changes the


permission of a file.
SYNTAX: chmod MODE[,MODE]
FILE...
Example: chmod 644

file.htm

File Commands

RM: Deletes a file without confirmation (by default).

SYNTAX: rm [-i] [-R] [-r] [filenames | directory]


Example: rm -r directory - remove a directory, even
if files existed in that directory

MKDIR: Create a new directory.


SYNTAX: mkdir dirname

RMDIR: Deletes a directory.

SYNTAX: rmdir [-p] [-s] file directory


Example: rmdir mydir - removes the directory
mydir

File Commands

CP: Copies files from one location to another.


SYNTAX: cp [OPTION]... SOURCE DEST
Example: cp *.txt newdir
Copy all files ending in .txt into the newdir directory.

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


another directory.
SYNTAX: mv [-f] [-i] oldname newname
Example: mv myfile.txt newdirectory

File Commands

WC: Short for word count, wc displays a count of


lines, words, and characters in a file.
Syntax: wc [-c ] [-l] [-w] [ file ... ]
Example: wc myfile.txt

PIPE: The special symbol | (PIPE) performs the job of


connecting two commands. We can connect a number
of UNIX commands in this way to perform difficult
task quite easily.
Example: $ ls | wc

File 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

File Commands

Sort Command

Read and sorts infile and display the result on


standard output, which has been redirected to
outfile:
sort infile > outfile # sorts infile and writes the
results to outfile

Write sorted output directly to outfile.


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

Read and sort infile "in place" writing to the same


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

Sort command continued


cat l1 |sort u
Display the sorted uniq records
ls -al | sort +4n
This 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.

vi Editor

vi is the standard UNIX text editor.

Command mode

move cursor, save, delete text, quit vi, etc.

Input mode

for inserting text


start by typing i; finish with ESC
cannot quit, delete, etc. in this mode
If in doubt, press ESC a few times. This will
put you back in command mode.

Inserting Text

No RETURN

Move to insertion point

Switch to input mode:

Start typing; BACKSPACE or DELETE for deletion

ESC finish; back in command mode

Over a slow network, the screen may not refresh


properly
^L refresh screen (in command mode)

When to type RETURN

Colon commands (e.g. :q!) and search


commands (e.g. /text) require a RETURN.
Commands that start with a letter
(e.g. ZZ, G) and control characters (e.g. ^L)
do not require a RETURN
Changes are stored in a buffer, so you
must save to change the file.

Finishing a vi Session

Get to command mode (press ESCs)


ZZ

:wq
quit
:q!

save changes to the file and quit


(no RETURN)
save changes to the file and
quit without saving
(press RETURN)

Basic Cursor Movements


h
j
k
l

move cursor one place to


left
down one
up one
right one

move forward one word


b
back one word
e
moves forward to end of
word
w

No RETURN
required!

Larger Moves-> All in ESC mode


G go to last line
<number>G
1G
$
0
^G
^F
^B

go to line

number

Go to first line
Move to end of line
Move to beginning of line

shows the current line number


Forward a screen
Back a screen

Type RETURN!

/text

Search forward for text

/func

search for func

?text

Search backward for text

?foo

searchforfoobackward

Deletion

Must be in command mode.


x

Delete character that cursor is on.

dd Delete current line.


D

Delete from cursor position to end of line

Undo last command

:i,jd
Example

Delete lines i to j
:23,29d
Delete lines 23 to 29

Special line numbers:


. means the current line number
^ means line number 1
$ means last line

:.,$d

Delete from current line


to the end of file.
replace character at the cursor position.

R replace character to the right of the cursor to


end of line

Cut & Paste with Deleted Text

Cut commands remove text from the screen, and


store it in a buffer
Paste commands copy text from the buffer to the
screen
d or dd or D
in a buffer

delete from screen and store text

Move cursor to new location

paste contents of buffer

to right of cursor posn

Moving Text

Cut and Paste with move

:i,jmk move lines i through j to

start after line k

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

move line 20 to after the current line


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

File-related Commands
:w file writes vi contents to new file
:w >> file

appends to file

:w! file writes over file


:w! writes over input file
:r file read in file; places it starting

at current cursor position

Text Substitution
:1,$s/old/new/g

:1,$s/Hat/Cat/g

replace every
occurrence of old by
new.

:i,js/old/new/g

replace every
occurrence of old by
new between lines i and j

:2,200s/darshan/DARSHAN/g
:1,.s/fc/function/g
current

from line 1 to

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

Customizing VI
:set ai(automatic indentation).
:set ic Ignore case when searching for
a patterns.
:set nomagic Searches a pattern like g*
:set nu will set the line number
/string :- search a perticular pattern with
in a file

AWK

It is an excellent filter and report writer. Many UNIX utilities generates rows and
Some
important
AWK
commands
which
can
columns
of information.
AWK is an
excellent
tool for processing
these
rows and
help us
ineasier
Daily
activities.
colu`mns,
and is
to use
AWK than most conventional programming

1)To display all the language


records with a particular

item like X03 from a file.


Awk /X03/ {print } xxxxxx.imp
2)To display only particular columns of a item
from a file
Awk /X03/ {print $2, $3, $5} xxxxx.imp

3) To display a certain range of


rows lets says from Row 3 to 6.
awk 'NR ==3, NR == 6 { print
$2}'
sp_onhand1_sap_mgarusb3.imp

4) To display all the records of


certain values in or option
awk '$2 == "X387" || $4 ==
"82.000" { printf "%d\n", $3 }'
sp_onhand1 _sap_mgarusb3.imp
Will display the 3rd field with 2nd
field = "X387" or 4th field =82.000

Write a Shell script


First line :-

#!/bin/ksh -x

Originally, we only had one shell on unix. When ran a


command, the shell would attempt to invoke one of the
exec() system calls on it. It the command was an
executable, the exec would succeed and the command
would run. If the exec() failed, the shell would not give up,
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
one shell on the system. But what if you are using one shell
as your interactive and want to run a script written in
another shell's language?

Write a Shell script


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

Sample/Simple script
vi example.ksh
Now in insert mode (i)
#!/bin/ksh
clear
echo "Good morning, world."
Esc:wq and execute
./example.ksh

To run a script

This will not run .


chmod 777 example.ksh
./example.ksh

Run a script in background

nohup example.ksh &


vi nohup.out

Include all the functionality


described earlier in a single shell
1#!/bin/ksh
script.

2clear
3echo "Good morning, world.
4cp abc.txt bcd.txt
5mv abc.txt bcd.txt
grep I who abc.txt >> mno.txt | cat
mno.txt
.
..esc:/ clear :1,$s/Good/Bad/ :set nu

QUESTIONS?

You might also like