Unix 1
Unix 1
Unix 1
• 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
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 “|”
Like above echo and other command can be used to explore behavior.
Ports
0 Standard Input <
1 Standard Output 1> or > or 1>>
2 Standard Error 2> or 2>>
Create a variable
>myvariable=“Hello!”
Display its value
>echo $myvariable Hello!
Weak quoting
>echo “$myvariable” Hello!
Strong quoting
>echo ‘$myvariable’ $myvariable
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
11
File
Operations
12
My
Environment
Special Variables
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
14
File
Operations
File Types
File Time
Modified time ls –lt When you modify the data
Access time ls –lu When you execute the file
$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
17
File
Operations
Copy, Rename and Delete Files
$cp myfile mynewfile Copy
$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 mywc
4 5 31 two
l = line = 4 w = words = 5
c = bytes = 18 + 8 + 4 (newlines) + 1 (space) = 31
19
File
Operations
20
File
Operations
21
File
Operations
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
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
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
29
Directory
Operations
Important Directories
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 :
>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
33
Directory
Operations
Directory Operations
>pwd
/var/stage/abinitio/data/dev/serial/BCBSNC/ent_integration/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
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.
38
Programs
>a="apple";b="banana";c="carrot“
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
>X=`expr 4 \* 2`
>echo $X 2
>expr $X + 2 4
41
Looping
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
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
This document contains confidential Company information. Do not disclose it to third parties without permission from the Company.