Você está na página 1de 50

Course Objectives

• Features
• What does the UNIX system contain?
• UNIX Flavors
• Getting started
• Friends on the network
• My Environment
• File Operations
• Directory Operations
• Short cuts
• Programs
• Frequently used commands

1
Features
• Open source executable and code made available unlike
Windows, people can customize it according to needs , hence
new variants code - have come up like Linux which is license
free.
• Tool and Utilities - for diff purposes + free add-ons can be
downloaded from internet
• Multiuser and Multitasking
• Networking - client/server architecture is supported , base
systems for internet , excellent platform for web servers, no
virus attacks
• Portability - Unix written in C, makes it portable, people can run
windows and Unix on one PC and choose the OS they want to
boot
2
What does the UNIX system contain?
User
Programs

Shell

Kernel

Hardware

3
• Kernel
– Interacts directly with hardware
– Schedules tasks manages data/file access and storage - enforces security mechanisms
– User prog rams interacts with kernel using system calls (open file, close file)
– Manages memory, handles errors and interrupts, maintains file system
• Shell Root
– command interpreter - Bash,Korn,Z,C,Bourne
• Common Utilities
– File management (rm, cat, ls, rmdir, mkdir)
– User management (passwd, chmod, chgrp) Oracle bin
– Process management (kill, ps)
• Hierarchical file system ora1 lib
– Hierarchical file structure like a tree structure
– A directory is nothing but a file containing many files lib1
– All devices are also considered as files ora1 file1
– Begins with Root (/) lib2
ora1 file2

4
Unix flavors
• AIX IBM
• Esix Unix Esix Systems
• HP-UX Hewlett-Packard Company
• Irix Silicon Graphics, Inc.
• Linux Several groups several
• LynxOS Lynx Real-Time Systems, Inc.
• MacOS X Server Apple Computer, Inc.
• NetBSD NetBSD Group
• Openstep Apple Computer, Inc.
• Red Hat Linux Red Hat Software, Inc.
• Reliant Unix Siemens AG
• SCO Unix The Santa Cruz Operation Inc.
• Solaris Sun Microsystems
• SuSE S.u.S.E., Inc.
• UNICOS Silicon Graphics, Inc.
• UTS UTS Global, LLC

5
Basic Commands

• id Current user name


• passwd To change password
• pwd Present working directory
• hostname Current host / system name
• df –kh Check disk free space, capacity, etc
• ls –l List the files in the directory
• ls –la List all files including hidden files
• date Date and time
• banner Display a string like a banner
• cal Display a calendar
• man <command> Help – Manual pages
• cat /more To view file

6
My
Environment
Redirectors
To be remembered in putty.
Select is to copy
Right Click is to paste
Redirectors
> Overwrite
>> Append
| Output of one command to another
>echo “ First Line” > new.txt
>cat new.txt
First Line
>echo “Again First Line” > new.txt
>cat new.txt
Again First Line Content of new.txt overwritten
>echo “Second Line” >> new.txt
>cat new.txt
Again First Line
Second Line Content appended to new.txt
7
Redirectors

The output of Command can be passed as a input to other command using “|”

Suppose a file has a trailer record as TRL~29


> tail -1 tail.dat |cut -d~ -f2
29
In above context o/p of first command is passed as a input to cut command

>echo “TRL~29” | cut -d~ -f2


29

Like above echo and other command can be used to explore behavior.

>echo “kArtHick” | tr [a-z] [A-Z]


KARTHICK
Standard IOE Ports

Ports
0 Standard Input <
1 Standard Output 1> or > or 1>>
2 Standard Error 2> or 2>>

> cat mynotes


cat: cannot open mynotes Error message
> cat mynotes 2>mystderr Redirecting std err into a file
> ls -l mystderr
-rw-r--r-- 1 user staff 24 Nov 4 11:20 mystderr
>cat mystderr
cat: cannot open mystderror File Content
My environment

To assign/Create a variable “$” is not necessary.

To display the value $ is mandatory

Create a variable
>myvariable=“Hello!”
Display its value
>echo $myvariable Hello!
Weak quoting
>echo “$myvariable” Hello!
Strong quoting
>echo ‘$myvariable’ $myvariable

Note the differences in using “” and ‘’ in above context

10
My
Environment

File Creation

>touch newfile.dat
>ls –lrt newfile.dat
-rw-r--r-- 1 user staff 0 Nov 4 11:20 mystderr Touch creates 0 byte file

To create a File with content Copied from Notepad


>cat > file1.dat
<<Paste the copied Content>>
<<press Enter>>
<<press Ctrl + D>>
>cat file1.dat Content displayed

11
File
Operations

Display File Contents


$cat > myfile Write a…z
$cat myfile > myfile1 ; cat myfile >> myfile1
$cat -n myfile1 > myalpha Add line numbers

$more myalpha Press spacebar , q


$less myalpha Press arrows, spacebar , q

$head -5 myalpha First 5 lines


$tail -5 myalpha Last 5 lines
$tail +10 myalpha Last lines starting from 10th

Display the lines from 20 till 50 (Answer on Slide 54)


- Use tail first
- Use head first

12
My
Environment

Special Variables

• echo $? 0 - Success Other than 0 – Failure


• echo $$ Process id
• echo $# Number of arguments – Maximum is 9
• echo $0,$1..$9 Parameters
• echo $@, $* All parameters passed

Back Quote:
Use ` (below ~ in keyboard) to execute any command within a commad
>date +”%Y%m%d” 20140414
Special
To get the sameFiles
date value in to a variable
>a=` date +”%Y%m%d” `
.profile
>echo $a Profile file for each user present in the20140414
home directory
Special variables are assigned values here
Settings special to this user is done here.
13
File Operations

File has a type


File has a permission, an owner and a group
File has a size
File has a date
File has a name

14
File
Operations

File Types

Ordinary file -rwxrwxrwx


Directory drwxrwxrwx
Character Special crwxrwxrwx
Block Special brwxrwxrwx
Pipe
File Permissions, Owner and Group prwxrwxrwx
Socket srwxrwxrwx
111 111 111 = 777
Links lrwxrwxrwx
rwx rwx rwx
Owner(u) Group(g) Others(o)
-r-x---rw- = ?
drwxr-xr-x = ?

$chmod 755 mystdout -rwxr-xr-x


$chmod u-x mystdout -rw-r-xr-x
$chmod g+w mystdout -rw-rwxr-x
15
File
Operations

File Time
Modified time ls –lt When you modify the data
Access time ls –lu When you execute the file

To change these times use the command “touch”


$touch bbb
$ls –lt bbb
-rw-r--r-- 1 xtrnshz dbuser 0 Jul 30 10:12 bbb
$ls –lu bbb
-rw-r--r-- 1 xtrnshz dbuser 0 Jul 30 10:12 bbb

$ksh bbb
$ls –lt bbb
-rw-r--r-- 1 xtrnshz dbuser 0 Jul 30 10:12 bbb
$ls -lu bbb
-rw-r--r-- 1 xtrnshz dbuser 0 Jul 30 10:24 bbb

16
File

File Operations
Operations

$cat > bbb


hehe haha hohoh
$ls -lt bbb
-rw-r--r-- 1 xtrnshz dbuser 16 Jul 30 10:26 bbb
$ls -lu bbb
-rw-r--r-- 1 xtrnshz dbuser 16 Jul 30 10:24 bbb

• Max 255 characters


• Case sensitive
• Containing special characters, except
Tab ,space, slash (/),backslash (\)
Ampersand (&),left and right angle brackets (< and >)
Question mark (?),dollar sign ($),left bracket ([)
Asterisk (*) ,tilde (~),pipe symbol (|)

17
File
Operations
Copy, Rename and Delete Files
$cp myfile mynewfile Copy
$ls –l

$mv myfile myghostfile Rename / Move


$ls –l

$rm mynewfile Delete


$ls -l

String Operations

Cut
$cat > mycut $cut -d ':' -f 1-2 mycut $cut -d ':' -f 1-2 -s mycut
aa:bb:cc aa:bb aa:bb
dd ee ff dd ee ff gg:hh
gg:hh:ii gg:hh

18
File

Word Count
Operations

Wc – Word count - counts lines, words, and bytes (LWC)


$cat > mywc
orange
yellow
indigo
hehe haha

$wc mywc
4 5 31 two

l = line = 4 w = words = 5
c = bytes = 18 + 8 + 4 (newlines) + 1 (space) = 31

19
File
Operations

Search /Grep Operation


Grep – Global Regular Expression and Print – Search patterns in files
There are several types: grep, fgrep, egrep

REGEXP = is a string which describes how to identify the pattern


$ grep <REGEXP> <file-name1> <file-name2>

$mkdir mygrep ; cd mygrep


$cat > file1 $cat>file2 $cat>file3 $cat > file4
Apple Apron africa Banana
Bamboo

$ grep A * $grep –i A * $ grep –n Bam * $ grep Ap..n *


file1:Apple file1:Apple file4:2:Bamboo file2:Apron
file2:Apron file2:Apron
file3:africa $ grep -vi o * $grep Ba[nm] *
$ grep -l A * file4:Banana file1:Apple file4:Banana
file1 file4:Bamboo file3:africa file4:Bamboo
file2 file4:Banana

20
File
Operations

Search /Grep Operation


$grep ”^B” * $ grep "a$" *
file4:Banana file3:africa
file4:Bamboo file4:Banana

-c No of lines matching the pattern


-w matches exact word
-i ignore case sensitive
cat > test.dat
Nttdata keane
Nttdata
Nttdatainc
[nttdat45@svcawabd][/home/nttdat45] > grep -ic nttdata test.dat
3
[nttdat45@svcawabd][/home/nttdat45] > grep -icw Nttdata test.dat
2

21
File
Operations

Starting the vi Editor


$vi myfile Vi Editor

Most powerful editor in the world


Available on all the flavors of Unix system
Command Description
More user friendly than any other editors like ed or ex
vi 3filename
modes of operation – Command,
Creates a new file Edit
if it already does
and Last not
line exist, otherwise opens
mode
existing file.
vi –R filename Opens an existing file in read only mode.

view filename Opens an existing file in read only mode.

vi –x filename Prompts for a password. Next time if password is wrong, then


encrypted version is displayed.
vi file1 file2 file3 Open multiple files

22
File
Operations Exiting the vi Editor – Command mode operations

Command Description
:q Exit with warning
:q! Exit without warning
:w Save changes
:wq Exit after saving changes
:w file2 Save a copy of the current file file1 as a different name file2

$view myalpha $vi –x myalpha


<<Press ESC : q>> Key: <<enter key>>
$vi myalpha <<Press ESC : q!>>
<<Press ESC : wq>> $vi –x myalpha
$vi myalpha Key:<<enter wrong key>>
<<Press ESC : q!>> <<Press ESC : q!>>

23
File
Operations

Commands
Insert Description
Mode
ESC i Insert at left
ESC I Insert at beginning of line
ESC a Append to right
ESC A Append at end of line
ESC ^ Go to Beginning of line
ESC $ Go to End of line
ESC 1G Go to Beginning of file
ESC G Go to End of file
ESC 16G Go to 16th line of file
ESC o Insert new line below
ESC O Insert new line above

24
File
Operations

$cat > myfile


aa bb cc
dd ee ff $vi myfile
<<ctrl+d>> <<press ESC o>>
$vi myfile Hello world
<< ESC i>> <<press ESC :w >>
Sherin <<press enter>> <<press ESC G A>>
<<Press ESC : wq>> this is the end!
$cat myfile <<press ESC :q! >>
Sherin $cat myfile
aa bb cc Sherin
dd ee ff Hello World
aa bb cc
dd ee ff

25
File
Operations

DeleteDescription
Command Operations
x Deletes the character under the cursor
location.
X Deletes the character before the cursor
location.
dw Deletes from the current cursor location
to the next word.
d^ Deletes from current cursor position to
the beginning of the line.
d$ Deletes from current cursor position to
the end of the line.
D Deletes from the cursor position to the
end of the current line.
dd Deletes the line the cursor is on.

26
File
Operations
Copy Paste Operations
Command Description
yy Copies the current line
yw Copies the current word from the cursor
position till end of the word
p Pastes copied text after the cursor
P Pastes copied text before the cursor
Set Operations

Command Description
:set ic Ignores case when searching
:set ai Sets auto indent
:set nu Displays lines with line numbers on the
left side.
:set ro Changes file type to "read only“
:set term Prints terminal type

27
File
Operations
Search Operations
Command Description
/pattern Search for pattern
n Search forward for next occurrence
N Search backward for previous occurrence

$vi myfile
<<insert World>>
<<save the file>> World
<<press /World>> Sherin
<<press n>> World
<<copy YY>> Hello World
<<move down 2 lines using arrow keys>> World
<< press p>> aa bb cc
<<move up 1 line and press P >>
dd ee ff
<<save and exit>>

28
Directory Operations

• File system is arranged in a hierarchical structure Root


• Upside-down tree with the root at the top and the branches at the bottom
• Root directory Oracle bin

– Root directory is the apex directory ora1 lib


ora1 file1 lib1
– There is no directory above root directory
lib2
– There can be many directories below root ora1 file2
– There is no concept of drives
– It is represented by ‘ / ’ ( forward slash ) symbol
– It is the reference point for all files, directories and subdirectories
– Every system has a root directory
– Most of the directories found in root are common on all UNIX machines

29
Directory
Operations
Important Directories

• / The home directory for the root user


• home Contains the user home directories
• bin Commands needed during bootup
• usr Contains all cmds, lib, man pages, games and static files
• lib Shared libraries needed by the programs
• dev Device files for devices such as disk drives
• etc Configuration files specific to the machine
Differences
• var Contains files that change like log files,emails, etc.
• tmp ENTITYTemporary filesREAD WRITE EXECUTE
•FILE •cat/view contents •Change the contents •Execute the file
of the file

•DIRECTORY •List the contents •Delete/Create files •Change to the


using ls only directory (cd)

30
Directory Operations
• In Unix Directory Operations “.” refers to Current Directory, “..“ refers to previous/parent Directory.
• cd change directory is used to navigate from one dir to other
• pwd command is used to print name of current working Dir

Paths :

Absolute path /dir1/dir2/dir3 Starts from root


Relative Path ./dir3 Starts from (.) Parent (..) Parent of parent

>pwd
/home/nttdat45
>cd ./final_graph
>pwd
/home/nttdat45/final_graph
>cd ..
>pwd
/home/nttdat45
Directory Operations

“.” And “..” Also be used to move and copy the files
>pwd
/home/nttdat45/final_graph
>ls –lrt tail.dat
-rw------- 1 nttdat45 abinitio 17 Mar 24 04:43 tail.dat
>mv tail.dat ../
>cd ..
>pwd
/home/nttdat45
> ls -lrt tail.dat
-rw------- 1 nttdat45 abinitio 17 Mar 24 04:43 tail.dat
> cp tail.dat ./final_graph
>cd final_graph
> ls -lrt tail.dat
-rw------- 1 nttdat45 abinitio 17 Apr 14 08:53 tail.dat
Directory

Directory Operations
Operations

• $mkdir mytemp Make a directory


• $chmod 777 mytemp Change permissions
• $cd mytemp Change to the directory
• $cat > mytempfile <write something> Create a file
• $ls –l List the file
• $cd .. Go back to parent directory

• $chmod 400 mytemp ; ls -l Read for owner


• $ cd mytemp Permission denied (Execute)
• $ls mytemp Will show files (Read)
• $ls –l mytemp Permission denied
• $ cat > mytemp/text Permission denied (Write)
• $ cat mytemp/mytempfile Permission denied

• $chmod 600 mytemp ; ls -l Read+Write for owner


• $ cd mytemp Permission denied (Execute)
• $ls mytemp Will show files (Read)
• $ls –l mytemp Permission denied
• $ cat > mytemp/text Permission denied (Write)

33
Directory
Operations

Directory Operations

$chmod 700 mytemp ; ls -l Read+Write+Execute for owner


$ cd mytemp ; cd .. Execute
$ls mytemp Will show files (Read)
$ls –l mytemp Will show files
$ cat > mytemp/text Write

$rmdir mytemp Permission denied


$rm –r mytemp Delete a directory with files

$dirname /path/filename Get pathname from full filename


$basename /path/filename Get filename from full filename
$cd ~ Your home directory
$cd $HOME Your home directory
$ cd / Go to root directory
$cd ../.. Go up two directory levels from here
34
Directory Substitution
To navigate Log directory of one project to another.

>pwd
/var/stage/abinitio/data/dev/serial/BCBSNC/ent_integration/log]

>cd ent_integration CARE_RADIUS/cre_authorization


>pwd
/var/stage/abinitio/data/dev/serial/BCBSNC/CARE_RADIUS/cre_authorization/log

Please See the Bolded Directory Path. Indicating the change in dir Paths
Alias
Short Cuts
– create or remove a shorthand for a command or series of commands

$alias
$ alias openfile=“vi /path/myfile“
$openfile

Alias can be included in .profile to avoid declaring it for every session.


History

% history - Displays the history of commands used

% history -20 – Displays last 20 commands used

36
Programs
Variables
Passing parameters

vi myscript.sh
#!/bin/ksh
vi myscript.sh zero=$0
#!/bin/ksh one=$1
x=“Sherin” two=$2
y=10 three=$3
echo $x $y echo “zero=" $zero “one=" $one “two="
<save and exit> $two “three=" $three
<save and exit>
$myscript.sh
Sherin 10 $myscript.sh aa bb
zero= myscript.sh one=aa two=bb
three=
$ksh –x myscript.sh aa bb

37
Programs
What is a shell script?
A File - With Instructions in a specific syntax– Executable
A script file may or may not have an extension.

Open a new script file using vi.


$vi myscript.sh

Invoking the Shell #!/bin/ksh


echo “Hello World”
Must be the first line in the script <save and exit>
$myscript.sh
#!/bin/ksh Korn Shell $ksh myscript.sh
#!/bin/sh Bash Shell $chmod u+x myscript.sh
#!/bin/csh C Shell $./myscript.sh
#!/bin/ksh –x Debug in Korn Shell $/full path/ myscript.sh
$ksh –x myscript.sh

38
Programs

And , Or and Not (&& , || , !)

>a="apple";b="banana";c="carrot“

if [ `date +"%Y"` == "2014" ]; then


> echo "Valid"
> else
> echo "Invalid"
> fi
Valid

&& - will go to part 2 if part 1=1


1 && 0 = 0 , 0 && 1 = 0
0 && 0 = 0 , 1 && 1 = 1

> [ $a == "apple" ] && echo "haha"


39
haha
File tests
Programs
Conditional Statements
-a File exists?
if condition1 -r File readable?
then -w File writable?
.. . -x File executable?
elif condition2 -f File exists+ordinary file?
then -d A directory?
..... -s Size greater than 0 bytes?
else Integer tests
then -eq Equal to
...... -ne Not equal to
fi -gt Greater than
-ge Greater then or equal to
Using “test” Using [ and ] -lt Less than
$x=“Wed” $if [ $x="Wed" ] -le Less than or equal to
String tests
$if test $x="Wed" > then
-z Length is 0
> then > echo haha
-n Length non zero
> echo haha > fi
= , !=
> fi haha Str String is not null

40
Arithmetic Operations
Programs

Expr – Expression

>echo $(( 1 + 2 )) 3
>expr 1 + 2 3
>x=`expr 3 + 5` Command substitution
>i=$(($x + 1))
>echo $i 9

* is a wildcard char in unix so use \ to escape

>X=`expr 4 \* 2`
>echo $X 2
>expr $X + 2 4
41
Looping
Programs

x=0; while [ $x –le 10 ] for x in 1 2 3 4 …10


do do
echo $x; x=`expr $x + 1` echo $x; x=`expr $x + 1`
done done

x=0;until [ $x –eq 10 ] case x in


do 1) echo one ;;
echo $x; x=`expr $x + 1` 2) echo two;;
done *) echo “Invalid number” ;;
esac
while read RECORD
do
echo $RECORD
done < filename

read x?“Please enter name“ Prompt user to assign value to x


break [n] Break out of loop n 42
continue [n] Continue loop n
Programs

Arrays
Index/ Subscript values start from 1 vi myarray
#! /bin/ksh
vi myarray set -A Week Mon Tue Wed Thurs Fri Sat Sun
#! /bin/ksh for day in ${Week[@]}
a[1]=x do
a[2]=y echo "The day is" $day
echo ${a[1]} ${a[2]} done

vi myarray
#! /bin/ksh
set -A myarray First Second Third
echo "myarray has ${#myarray[*]} elements"
echo "myarray contains: ${myarray[*]}“
echo “another way to get all elements: ${myarray[@]}“
43
unset A
To transfer file from one server to another (FTP)
Use Below steps in Command prompt To transfer a
file from Windows to unix.

C:\Users\nttdat45>cd desktop
C:\Users\nttdat45\Desktop>ftp svcawabd
Connected to svcawabd.bcbsnc.com.
User (svcawabd.bcbsnc.com:(none)): nttdat45
Password:
230 User nttdat45 logged in.
ftp> pwd
257 "/home/nttdat45" is current directory.
ftp> put alis_balan.txt
200 PORT command successful.
150 Opening data connection for alis_balan.txt.
226 Transfer complete.
ftp: 796 bytes sent in 0.08Seconds 10.21Kbytes/sec.
ftp> bye
221 Goodbye.
To transfer file from one server to another

SCP Command

Use Below steps in Command prompt To transfer a file from one Unix server to other

scp <source_file> username@hostname:<Target Dir>

If target dir is not specified it will land in home dir

>scp vir.dat nttdat45@hq1adws01:


nttdat45@hq1adws01's password:
vir.dat 100% 10 0.
0KB/ 00:00
>
Find Command
find a search for files in a directory hierarchy

find [path...] [expression]

To find files with name a.out


find . -name “a.out” –print

To find all log files created by user nttdat45


find . -name “*.log” –user nttda45

Finding files which has been modified less than one day in Unix:
find . -mtime -10

find all text file which contains word Exception using find command
in Unix ?
find . –name "*.txt" –print | xargs grep “Exception”
Frequently Used Commands

1. To get last 10 Files in a directory


ls –lrt | tail -10
2. To get rec_count excluding header and trailer
sed '1d;$d' <file_name> |wc –l
3. Suppose a file has a trailer record as TRL~29
tail -1 tail.dat |cut -d~ -f2
29
4. To create a file ignoring header and trailer record
sed '1d;$d' file_name > new_file_name
5. To read a log file which is being loaded/generated
tail –f file_name
6. To mail
echo “ Mail Content” | mailx –s “subject” xxx@yyy.com
7. To Mail a file as attachment
uuencode filename filename | mailx –s “subject” xxx@yyy.com
Frequently Used Commands
1. To invoke previously used command
ESC + k
2. To break/stop the process
ctrl +c or ctrl +z
3. To know the current working Directory
pwd
4. To comeback to home Directory
cd
cd ~
5. To switch back to previous Directory
cd –

6. To invoke any environmental script


. ./<script Name>
7. To call/invoke a script
ksh < script Name>
./ < script Name>
This document contains confidential Company information. Do not disclose it to third parties without permission from the Company.
Thank You

This document contains confidential Company information. Do not disclose it to third parties without permission from the Company.