unix structure
TRANSCRIPT
-
5/20/2018 Unix Structure
1/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
Welcome
Welcome
Unix structure and Commands1
-
5/20/2018 Unix Structure
2/122
Copyright IBM Corporation 2011
IBM Global Business Services
IBM
Unix Structure andCommands
Copyright IBM Corporation 2011Unix structure and Commands2
-
5/20/2018 Unix Structure
3/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
Objectives
By the end of this module on Unix structure and commands, you should be able to:
Discuss the basics of Unix File System
Put into practice file handling commands
Define vi Editor basics
Identify command substitution, filters, and pipes
Illustrate locating files with find
Demonstrate the xargs command
Unix structure and Commands3
-
5/20/2018 Unix Structure
4/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
Agenda
This module on Unix structure and commands includes:
Introduction
Unix File System
File Handling Commands
vi Editor Basics
Command substitution, filters and pipes
Locating files with find
xargs command
Unix structure and Commands4
-
5/20/2018 Unix Structure
5/122
IBM Global Business Services
Copyright IBM Corporation 2010
Topic 1: Introduction
-
5/20/2018 Unix Structure
6/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
Sub-topics covered
The sub-topics covered in this module include:
Operating system
History of Unix
Features of Unix
Architecture of Unix
Unix structure and Commands6
-
5/20/2018 Unix Structure
7/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
Operating system
The operating system can be defined as follows:
It is the interface between hardware and user.
It is responsible for management and coordination of
activities and sharing of resources of computer.
It acts as host for computing applications that run
on the machine.
It provides applications access OS services throughAPIs or System Calls.
It provides two types of interfaces to the users, namelyCommand Line Interface and Graphical User Interface.
Unix structure and Commands7
-
5/20/2018 Unix Structure
8/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
History of Unix
Here is a timeline that shows how Unix developed over time:
In 1969, UNIX first originated in form of Multics at Bell Laboratories.
In 1974, Thompson and Ritchie both published a paper regarding UNIX in
Communications of ACM.
By 1977, several UNIX systems were used in Universities. Between 1977 to 1982,UNIX
System III was released by Bell Laboratories.
By the start of 1984, the use of UNIX systems significantly increased.
Linux is a complete rewrite of Unix, developed by Linus Tourvalds, when he was a
Finnish undergraduate.
Unix structure and Commands8
-
5/20/2018 Unix Structure
9/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
Features of Unix
The feature of Unix are listed below:
Portable: Unix OS can be installed on any architecture.
Multi-user: Multiple users can access the system and share its resources.
Multi-tasking: Multiple tasks can be initiated and run simultaneously.
Time-sharing: Server shares CPU time between requesting processes.
Hierarchical file organization: / (root) is at the top of hierarchy and the various other
file systems are below that.
Unix structure and Commands9
-
5/20/2018 Unix Structure
10/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
The architecture of Unix includes:
System components
Kernel
Shell
The system components are
shown in the diagram here.
Architecture of Unix
Hardware
Kernel
Shell Shell
sed
ps
grep
lswho
tar
sort
cp
X-Window
cc
Other
Compilers
Various Text
Processors
Spread
Sheets
Browsers
Different
Databases
Other
Softwares
User 2User 1
Unix structure and Commands10
-
5/20/2018 Unix Structure
11/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
The architecture of Unix, especially kernel, is definedbelow:
This is the core of Operating System.
It functions as the Hardware interface.
It contains system calls that perform
low-level tasks.
It generates inode-numbers for newly created files andmaintains inode tables.
It generates process-IDs for newly created processesand maintains process control blocks.
Architecture of Unix: Kernel
Unix structure and Commands11
-
5/20/2018 Unix Structure
12/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
The architecture of Unix, especially shell, is defined below:
User interface: Shell provides an interface to the user, wherein the user could issue hisor her commands, and shell displays output and error messages to the user.
Command interpreter: Shell accepts command from user and interprets it to the kernel.
Command processor: Shell parses the command line arguments, expands the special
meaning of meta characters, searches for the command, and if the command is found,then transfers control to the command.
Programming language: Shell provides a native programming language.
Architecture of Unix: Shell
Unix structure and Commands12
-
5/20/2018 Unix Structure
13/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
Questions and answers
Unix structure and Commands13
-
5/20/2018 Unix Structure
14/122 Copyright IBM Corporation 2011
IBM Global Business Services IBM
Knowledge check
What is the interface between the hardware and the user?
Who developed Unix?
Enumerate three features of Unix.
Unix structure and Commands14
-
5/20/2018 Unix Structure
15/122
IBM Global Business Services
Copyright IBM Corporation 2010
Topic 2: UNIX File System
IBM
-
5/20/2018 Unix Structure
16/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
Sub-topics covered
The sub-topics covered in this topic include:
File system
Inode table
Directory structure
Unix structure and Commands16
IBM
-
5/20/2018 Unix Structure
17/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
File system is a way of organizing files in the secondary storage device. The Unix filesystem is partitioned into four partitions:
Boot Block: Programs associated with booting of the system are stored in this block.
Super Block: This stores accounting information about the file system.
Inode Block: This stores inode tables.
Data Block: This stores data.
File system
Unix structure and Commands17
IBM
-
5/20/2018 Unix Structure
18/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
Every file in Unix file system has an inode table, which stores meta data of the file.The Inode table stores the following information about the file:
Inode table
File
Type
Permission
Mode
User
Id
Group
Id
File
SizeBytes
Modification
Time
Access
Time
Inode
UpdationTime
Ptrs
ToDataBlock
Ord. 644 Tom Ken 85 14-Jun-09
09:11:45
14-Jun-09
09:11:45
18-Jun-09
15:12
Unix structure and Commands18
IBM
-
5/20/2018 Unix Structure
19/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
Inode table: Block addressing scheme
Inode table consists of 13 fields, each containing addresses to a block each.The first 10 address fields contains addresses of a data block each. This is direct addressing.Number of data blocks addressed: 10.
The 11th field contains address of an address block, the addresses of which point to a data
block each. This is single indirect addressing. Number of data blocks addressed: 128.
12th field contains address of an address block, the addresses of which point to an address
block each, each of the addresses of which point to data blocks; Number of data blocksaddressed: 128 * 128.
13th field contains address of an address block, the addresses of which points to an
address block each; each of the address of these address blocks point to an address blockeach, through which, data blocks are pointed. Number of data blocks addressed.128 * 128 * 128.
Unix structure and Commands19
IBM
-
5/20/2018 Unix Structure
20/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
Directory structure
Directory structure in Unix is an inverted tree structure, wherein the root (/) directory is atthe top of the hierarchy and the subdirectories are placed as children of the root.
/
etc dev sbinbin usr tmp
Unix structure and Commands20
IBM
-
5/20/2018 Unix Structure
21/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
There are two means of referencing of files, including:
Absolute referencing: The referencing of a file done with reference to the root directory, /
Examples:$ cd /usr/include # changes to /usr/include directory$ ls /dev/tty # lists the file /dev/tty
Relative referencing: The referencing of a file done relative to the working directory.
Current directory and parent to current directory are referred by shortcuts . and ..respectively.
Examples:$ cd cppdir # changes to subdirectory cppdir
Directory structure: Referencing of files
Unix structure and Commands21
IBM
-
5/20/2018 Unix Structure
22/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
Questions and answers
Unix structure and Commands22
IBM
-
5/20/2018 Unix Structure
23/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
Knowledge check
What are the four partitions of the Unix file system?
Enumerate three types of information stored in the Inode table.
What is the referencing of a file done relative to the working directory?
Unix structure and Commands23
-
5/20/2018 Unix Structure
24/122
IBM Global Business Services
Copyright IBM Corporation 2010
Topic 2: UNIX file system
IBM Gl b l B i S i IBM
-
5/20/2018 Unix Structure
25/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
Sub-topics covered
The sub-topics covered in this topic include:
File system
Inode table
Directory structure
Unix structure and Commands25
IBM Gl b l B i S i IBM
-
5/20/2018 Unix Structure
26/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
pwd: Knowing your present working directory
$ pwd
/h ome/sac h in
Unix structure and Commands26
IBM Gl b l B i S i IBM
-
5/20/2018 Unix Structure
27/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
ls: Listing files and subdirectories in any directory
$ ls
abc.txt s.sh test.c dir1
$ ls /home/sach in
pmode.sh f ibonacci .c prof i le. tx t
$ lsl
-rwxr-xr-x 1 sach in foo tbal l 512 feb 23 12:37 abc .txt
-rwxr-xr-x 1 sach in foo tbal l 20 mar 12 14:16 s.sh
-rwxr-xr-x 1 sachin f ootb al l 134 apr 4 09:11 test.c
Unix structure and Commands27
IBM Global B siness Ser ices IBM
-
5/20/2018 Unix Structure
28/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
ls: Listing files and subdirectories in any directory
$ ls l /hom e/sachin
-rwxr-xr-x 1 sachin foo tbal l 36 mar 3 17:25 pm ode.sh
-rwxr-xr-x 1 sach in foo tbal l 114 jan 24 20:12 fibo nacci.c
-rwxr-xr-x 1 sachin footb al l 50 apr 5 10:20 pro f i le.txt
$ ls -a
$ ls -a /usr /sachin
$ ls -a | mor e
$ ls -lrt
Unix structure and Commands28
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
29/122
Copyright IBM Corporation 2011
IBM Global Business Services IBM
$ cd
$ pwd
/h ome/sach in
$ cd /usr/ l ib
$ pwd
/u sr/l ib
$ cd ..
$ pwd
/u sr
cd: Changing Directory
Unix structure and Commands29
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
30/122
Copyright IBM Corporation 2011
IBM Global Business Services
$ cd bin
$ pwd
/u sr/b in
$ cd ../..
$ pwd
/
$ cd -
$ pwd
(Takes you to the p revious directory)
cd: Changing Directory (continued)
Unix structure and Commands30
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
31/122
Copyright IBM Corporation 2011
IBM Global Business Services
$ cd /home/sachin
$ mkdir playdt
$ cd playdt
$ pwd
/h ome/sac h in /p lay d t
$ mkdir /home/sachin/playdt /mi l lnm
$ cd mar
$ pwd
/h ome/sac h in /p lay d t/m il lnm
mkdir: Creating new directory or subdirectory
Unix structure and Commands31
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
32/122
Copyright IBM Corporation 2011
IBM Global Business Services
$ mkdi r p thisyr /mar/f i rs tprd
$ cd thisyr /mar/f i rs tprd
$ pwd
/h ome/sac h in /p lay d t/m il lnm / th isy r /m ar /f irs tprd
mkdir: Creating new directory or subdirectory (continued)
Unix structure and Commands32
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
33/122
Copyright IBM Corporation 2011
IBM Global Business Services
The removing criteria for the directory or subdirectory to be removed is as follows:
It should not be the current directory
It should be vacant
$ cd /hom e/sachin/playdt /mi l lnm /th isyr /mar
$ pwd
/h ome/sach in /p lay d t/m il lnm /th isyr /m ar
$ rmdir f i rs tprd
$ cd ..
$pwd
/h ome/sach in /p lay d t/m il lnm /th isyr
rmdir: Removing existing directory or subdirectory
Unix structure and Commands33
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
34/122
Copyright IBM Corporation 2011
IBM Global Business Services
$ mk dir /hom e/sachin/playdt /mi l lnm /th isyr /mar
$ cd ..
$ pwd
/h ome/sach in /p lay d t/m il lnm
$ cd ../..
$ pwd
/home/sach in
$ rmd ir p /playdt /mi l lnm
rmdir: Removing existing directory or subdirectory (continued)
Unix structure and Commands34
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
35/122
Copyright IBM Corporation 2011
IBM Global Business Services
Some file-oriented commands include:
cat
mv
cp
rm
chmod
Some file oriented commands
Unix structure and Commands35
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
36/122
Copyright IBM Corporation 2011
IBM Global Business Services
$ cat pro f i le.txt
My com pany name is IBM.
My wo rk locat ion is Kolkata.
$ cat /us er/sach in/pmode.sh
echo The working mode is Learning.
$ cat pro f i le.txt pm ode.sh
My com pany name is IBM.
My wo rk locat ion is Kolkata.
echo The working mode is Learning.
cat: Displaying the content of one or more file
Unix structure and Commands36
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
37/122
Copyright IBM Corporation 2011
IBM Global Business Services
$ mv /home/sach in/f ibonacci.c /usr/bin
$ mv /home/sach in/f ibonacci.c /us r/bin/f ibonacc i.c
$ mv /home/sach in/f ibonacci.c /home/sach in/pmode.sh /us r/bin
$ mv /home/sach in/f ibonacci.c /home/sach in/f ibbo .c
$ cd /hom e/sach in
$ mv f ibo.c f ibonacci .c
mv: Moving file from one directory or subdirectory to another
Unix structure and Commands37
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
38/122
Copyright IBM Corporation 2011
IBM Global Business Services
$ cp /home/sach in/f ibonacci.c /home/sach in/fb.c
$ cd /hom e/sach in
$ cp pmo de.sh p.sh
cp: Copying of one file to another
Unix structure and Commands38
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
39/122
Copyright IBM Corporation 2011
IBM Global Business Services
$ cp /hom e/sach in/fb.c /hom e/sachin /p.sh /usr /bin
$ cd /hom e/sach in
$ cp f ibonacci .c pmode.sh /usr
cp: Copying files from source directory to another
Unix structure and Commands39
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
40/122
Copyright IBM Corporation 2011
G oba us ess Se ces
$ rm /home/sach in/fb.c
$ rm /hom e/sach in/f ibo.c /home/sach in/p.sh
$ cd /hom e/sach in
$ rm fib bo .c
$ rm f.c pm.sh
$ rm *
(Use the above command with great caution)
rm: Deleting one or more files
Unix structure and Commands40
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
41/122
Copyright IBM Corporation 2011
Interactive Mode:
$ rm i fb.c
fb.c: ? y
Force Mode:
$ rmf p.sh
Recursive Mode:
$ rm r /home/sachin
$ rm r * (Recursiv e remov al of f i les )
rm: Several options
Unix structure and Commands41
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
42/122
Copyright IBM Corporation 2011
$ date
Mon Ap r 23 11:23:34 IST 2007
date: Displaying system date
Unix structure and Commands42
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
43/122
Copyright IBM Corporation 2011
d => Day (for exam ple, 23)
m => Mont h Num ber (for examp le, 10)
y => Rightm ost Two Digits of Year (for examp le, 07 for 2007)
a => Sho rt Name o f Week Day (for examp le, Mon )
h => Short Name o f Month (for examp le, Apr)
H => The Hour in 24 Hou r Form at (for examp le, 20)
M => Minu te (for example, 34)
S => Second (for example, 12)
date: Several format specifiers
Unix structure and Commands43
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
44/122
Copyright IBM Corporation 2011
$ date +%a
Mon
$ date +%H %M
11 34
$ date +%H : %M 11 : 34
date: Use of several format specifies
Unix structure and Commands44
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
45/122
Copyright IBM Corporation 2011
$ cal 2007
$ cal
$ cal apr
$ cal 2007 | more
cal: Displaying calendar
Unix structure and Commands45
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
46/122
Copyright IBM Corporation 2011
$ banner IBM KOLKATA
IBM
KOLKATA
$ banner IBM KOL
IBM KOL
$ banner IBM KOL IT WORK
IBM KOL
IT WORK
banner: Displaying posters for any text
Output will bemuch bigger than
shown here.
Unix structure and Commands46
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
47/122
Copyright IBM Corporation 2011
Locking without time specification:
$ lock
passwo rd: ******
Reenter passwo rd: ******
Terminal locked by sachin 0 minu tes ago.
Locking with time specification:
$ lock -20
..
lock: Locking the system without logging out
Locking for 30Minutes, if time not
specified duringcommandinvocation
Unix structure and Commands47
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
48/122
Copyright IBM Corporation 2011
Waits for All:
$ wait
Waits for Some Specific One:
$ wait 232
( wait for pr oc ess id 232 )
wait: Waits for finishing of the system background process
Unix structure and Commands48
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
49/122
Copyright IBM Corporation 2011
$ echo Delay Invocation; sleep 20; echo Back Again After 20 Sec
Delay Invocat ion Back A gain After 20 Sec
Delay of 20 Second s
sleep: Invoking delay
Unix structure and Commands49
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
50/122
Copyright IBM Corporation 2011
$ who
roo t conso le apr 23 15:23
sach in tty01 apr 23 15:24
sh tty02 apr 23 15:25
who: Aiding in being acquainted with all logged-in users
Unix structure and Commands50
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
51/122
Copyright IBM Corporation 2011
$ who am I
sachin tty01 apr 23 15:24
who am I: To know current user, who has issued command
Unix structure and Commands51
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
52/122
Copyright IBM Corporation 2011
$ uname -n
sachdev
unamen: Knowing computers name in network
Unix structure and Commands52
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
53/122
Copyright IBM Corporation 2011
$ tty
/d ev /t ty01
tty: To be acquainted with the name of users terminal
Unix structure and Commands53
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
54/122
Copyright IBM Corporation 2011
$ tput cup 15 28
$-
tput cup: Cursor positioning at desired location
Unix structure and Commands54
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
55/122
Copyright IBM Corporation 2011
$ tput c lear
$-
tput clear: Monitor screen clearing
Unix structure and Commands55
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
56/122
Copyright IBM Corporation 2011
$ tput sm so
echo Tutorial For Highlighting Displayable Text.
$ tput rms o
tput smso and tput rmso: Highlighting displayable text
Tutorial for Highlighting Displayable Text
Unix structure and Commands56
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
57/122
Copyright IBM Corporation 2011
$ bc
7+17
24
12/7
1
99 ; 3 3 ; 4 * 5
0
27
20
$_
bc: Performing numeric calculation
Unix structure and Commands57
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
58/122
Copyright IBM Corporation 2011
$ bc
ibase =2
1001
9
obase=2
11
1011
scale=3
10/3 3.333
$_
bc: Performing numeric calculation (continued)
Unix structure and Commands58
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
59/122
Copyright IBM Corporation 2011
$ bc
r=9 ; s=4 ;
t=r-s
t
5
$_
$ x=3 $ y= echo $x + 4 | bc `
$ echo $y
7
bc: Performing numeric calculation (continued)
Unix structure and Commands59
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
60/122
Copyright IBM Corporation 2011
UNIX:
$ spell /home/sachin /prof i le.txt
$ cd /home/sach in
$ spell pro f i le.txt
LINUX:
$ ispell /home/sachin/profile.txt
$ cd /home/sachin
$ ispell profile.txt
spell / ispell: Checking of spellings
Unix structure and Commands60
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
61/122
Copyright IBM Corporation 2011
The wildcard characters basics include:
? => Must be One Character, Any character
* => Can be Zero or Any Number of Characters, AnyCharacter
[L] where L is a set of characters placed without separation => One character, anyone
character from L.
Wildcard characters basics
Unix structure and Commands61
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
62/122
Copyright IBM Corporation 2011
$ ls *
$ ls l p*.??
$ cat ???? ?.sh
$ ls p[rm ]*
Wildcard characters: Examples
Unix structure and Commands62
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
63/122
Copyright IBM Corporation 2011
$ chmod u+x pro f i le.tx t
$ chmo d g+rw prof i le.tx t
$ chmod o-r prof i le.tx t
$ chmod +wx p rof i le.tx t
$ chmod x pro f i le.txt
$ chmod go -w prof i le.tx t
$ chmo d u=rw x, go=rx prof i le.tx t
chmod: Altering file access permissions using symbols
Several Symbols
u => File Owner
g => Group Ownero => Other User
r => Read
w => Write
x => Execute
Unix structure and Commands63
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
64/122
Copyright IBM Corporation 2011
$ chmod 741 pro f i le.txt
$ chmod 777 pro f i le.txt
$ chmod 751 pro f i le.txt
$ chmod 000 pro f i le.txt
chmod: Altering file access permissions using numbers
Several Numbers
0 => No Permission Is Present
1 => Execute Only
2 => Write Only
3 (2+1) => Write & Execute
4 => Read Only
5 (4+1) => Read & Execute
6 (4+2) => Read & Write
7 (4+2+1) => Read, Write & Execute
Several Permission WeightsRead => 4
Write => 2
Execute => 1
Unix structure and Commands64
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
65/122
Copyright IBM Corporation 2011
Running processes related to current user at current terminal:
$ ps
PID TTY TIME CMD
234 tty 01 00:00:02 sh
684 tty 01 00:00:00 ps
All Running User and System Processes:
UNIX: $ ps -e
LINUX: $ ps -ax
ps: Knowing about running processes of system
Unix structure and Commands65
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
66/122
Copyright IBM Corporation 2011
Details of running processes related to current user at current terminal:
UNIX: $ ps -f
LINUX: $ ps -u
ps: Knowing about running processes of system (continued)
Unix structure and Commands66
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
67/122
Copyright IBM Corporation 2011
INPUT REDIRECTION:
$ cat < prof i le.txt
$ cat 0< prof i le.txt
OUTPUT REDIRECTION:
$ cat pro f i le.txt >pm ode.sh
$ cat pro f i le.txt >> fibb onac ci.c
ERROR REDIRECTION:
$ cat pl.txt 2> err.txt
Redirection: Altering input, output, or error files for a command
Unix structure and Commands67
-
5/20/2018 Unix Structure
68/122
IBM Global Business Services
Copyright IBM Corporation 2010
Topic 4: vi Editor Basics
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
69/122
Copyright IBM Corporation 2011
Sub-topics covered
The sub-topics covered in this topic include:
vi Editor Basics
Mode commands
Repeat factor
Unix structure and Commands69
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
70/122
Copyright IBM Corporation 2011
vi Editor is a powerful full-screen editor for UNIX. Texts, numbers, commands, and more
can be inserted to any file through this. For file creation, editing, and the like, this
is used extensively in UNIX.
vi Editor can be invoked with the following command: $ vi viexmpl.sh
Some of the important modes of vi Editor include:
Input Mode
Command Mode
vi Editor: Introduction
Unix structure and Commands70
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
71/122
Copyright IBM Corporation 2011
vi Editor: Toggling between important modes and Shell
Shell
Command
Mode
Input
Mode
Vi filename
i, a, I, A etc.
:wq, :q!, :x etc.
Unix structure and Commands71
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
72/122
Copyright IBM Corporation 2011
Several Input Mode Commands in vi Editor
Some input mode commands in vi Editor include:
i: Inputs text at left of current cursor position.
a:Appends text at right of current cursor position.
I:Inputs text at starting of current line.
A:Appends text at the end position of current line.
R:Replaces (overwrites) text from current cursor position to right direction.
S:Replaces the full line in which the cursor is.
Unix structure and Commands72
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
73/122
Copyright IBM Corporation 2011
Several essential command mode commands in vi Editor
Some input mode commands in vi Editor include:
:x (or :wq): Saves and Exits from vi Editor.
:q!: Quits from vi Editor without Save.
:w: Saves current file and dont exit the vi Editor.
h: Shifts the cursor to one character left.
J:Shifts the cursor to one line down.
K: Shifts the cursor to one line up.
l: Shifts the cursor to one character right.
4h:Shifts the cursor to four characters left.
5j:Shifts the cursor to five lines down.
6k:Shifts the cursor to six lines up.
7l:Shifts the cursor to seven characters right.
Unix structure and Commands73
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
74/122
Copyright IBM Corporation 2011
More essential command mode commands in vi Editor
Some input mode commands in vi Editor include:
|:Cursor reaches the beginning of current line.
$:Cursor reaches the beginning of current line.
:Scrolls forward one page.
:Scrolls backward one page.
20G:Cursor reaches 20th line of the current file.
15|:Cursor reaches 15th column of current line.
1G:Cursor reaches the Starting of the current file.
G:Cursor reaches the end of the current file.
x: Erases the current character.
4x: Erases current character and three next characters.
d15G: Erases all from current cursor position to 15th line.
Unix structure and Commands74
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
75/122
Copyright IBM Corporation 2011
Additional essential command mode commands in vi Editor
Some input mode commands in vi Editor include:
dd:Erases full current line.
7dd:Erases full current line and next six lines.
. :Applies the last editing command used.
u:Undoes the last editing operation performed.
J:Joins the current line with the next line.
7yy:Copies the current line and next six lines together.
p:Pastes the copied content after the current cursor position.
~ :Changes current character case to the reverse one.
15~:Changes current and next 14 character case to the reverse one.
1p:Restores latest erase work performed.
3p:Restores latest three erase work performed.
Unix structure and Commands75
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
76/122
Copyright IBM Corporation 2011
Repeat factor in vi Editor
In Command Mode:
10i^
or
10i^
Will enter consecutive 10 ^
that is, ^^^^^^^^^^
Unix structure and Commands76
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
77/122
Copyright IBM Corporation 2011
Some search commands include:
/IBM:Searches in forward direction for pattern IBM in current file.
?kolkata:Searches in backward direction for pattern kolkata in current file.
Some substitute commands include:
:1,25s/Calcutta/Kolkata/:Replaces first occurrence of Calcutta with Kolkata
between lines 1 to 25 in current file.
:1,25s/Calcutta/Kolkata/g:Replaces all occurrence of Calcutta with Kolkata
between lines 1 to 25 in current file.
:.,$s/Calcutta/Kolkata/gc:Replaces Calcutta with Kolkata interactively between
current line and end of current file.
Commands
Unix structure and Commands77
-
5/20/2018 Unix Structure
78/122
IBM Global Business Services
Copyright IBM Corporation 2010
Topic 5: Command Substitution, Filters, and Pipes
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
79/122
Copyright IBM Corporation 2011
Sub-topics covered
The sub-topics covered in this topic include:
Command substitution
Filters
Pipes
Unix structure and Commands79
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
80/122
Copyright IBM Corporation 2011
Command substitution is the technique of passing a commands output as argument to
another command. The command that passes output as argument can be called as innercommand. The command that receives the output of another command as its argument,
can be called as outer command. Here is an example:
$ echo "Today's date is `date`
Today's d ate is Tue Apr 7 15:14:41 IST 200
$ echo "No. of f i les in $PWD is $(ls|wc -l)
No. of f i les in /home/mural i is 45
Command substitution
Explanation:The meta-characters ` `(pair of backticks) and $() are known as command
substitution meta-characters. When a command is issued, shell first parses the command line
arguments; the shell first expands special meaning of these meta-characters; hence, shell first
executes the command specified within `` or $() and places Output of this command as
argument to the main command; then, Shell transfers control to the main command.
Unix structure and Commands80
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
81/122
Copyright IBM Corporation 2011
This refers to a command which, by default, expects input from standard input file and
sends processed output to standard output file.
Here is a list of some filters:
uniq
t r
tee pr
cut
paste
bc
Filters
Unix structure and Commands81
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
82/122
Copyright IBM Corporation 2011
It translates consecutive identical entries from the input file, into unique entries. These
include:
$ cat dom est ics
dog
goat
goat
goat
horse
$ uniq dom estics
dog
goat
horse
Filters: Unique
Unix structure and Commands82
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
83/122
Copyright IBM Corporation 2011
Here is an example of unique filters:
c opt ion
$ cat domest ics
dog
goat
goat
$ uniq -c domest ics
1 dog
2 goat
Filters: Unique (continued)
Explanation of code:
With c option, uniq command displays number
of consecutive occurrences of the identical
entries and also the unique entries.
Unix structure and Commands83
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
84/122
Copyright IBM Corporation 2011
It translates all occurrences of a set of characters in the first expression to corresponding
set of characters in second expression, from the input file.
Syntax:
tr exp1 exp2 < file1
Options:
S: This will squeeze multiple consecutive occurrences of a specified set ofcharacters to single occurrence.
D: This deletes the specified set of characters from the string. Cd: This complement deletes the specified set of characters from the string.
Filters: tr
Unix structure and Commands84
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
85/122
Copyright IBM Corporation 2011
Here is an example of tr filters:
$ cat > text1
a quick brown fox jump ed over the lazy dog
$ tr '[a-z]' '[A -Z]' < text1
A QUICK BROWN FOX JUMPED OVER THE LAZY DOG
$
Filters: tr (example)
Explanation of code:
The tr command translates all occurrences of
lower-case alphabets to corresponding
upper-case alphabets in the file text1;
it displays the output.
Unix structure and Commands85
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
86/122
Copyright IBM Corporation 2011
Here is another example of tr filters:
$ cat mydir info
tot al 12
-rw-r--r-- 1 murali murali 0 2009-03-25 12:48 ch ap1
-rwxr--r-- 1 mu ral i m ural i 133 2009-03-25 12:48 my fun ction s
$ tr -s ' ' < my dir info
tot al 12
-rw-r--r-- 1 mu rali m urali 0 2009-03-25 12:48 chap1
-rwxr--r-- 1 mu ral i m ural i 133 2009-03-25 12:48 my fun ction s
Filters: tr (example) (continued)
Explanation of code:
The tr command squeezes consecutive multiple occurrences of space character to single
occurrence in the input file mydirinfo, and displays output.
Unix structure and Commands86
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
87/122
Copyright IBM Corporation 2011
Here is another example of tr filters using d option:
$ cat > num fi le
78343hhdfdh434384
834893849&*&*&dfdhfhdj
4839483jhdjfhdjf
hsdjfhdjfhdj4738473
jd f jdh f384738
$ tr -d '[a-z&*] ' < num fi le
78343434384
834893849
4839483
4738473
384738
Filters: tr (example: d option)
Explanation of code:
The tr command removes lower-case
alphabets, the characters &and *
from the input file numfile, and
displays output.
Unix structure and Commands87
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
88/122
Copyright IBM Corporation 2011
Here is another example of tr filters using cd option:
$ cat > num fi le
78343hhdfdh434384
834893849&*&*&dfdhfhdj
4839483jhdjfhdjf
hsdjfhdjfhdj4738473
jd f jdh f384738
$ tr -cd '[0-9\n] ' < num fi le
78343434384
834893849
4839483
4738473
384738
Filters: tr (example: cd option)
Explanation of code:
The tr command removes all other
characters except digit characters and
newline characters from the input file
numfile, and displays output.
Unix structure and Commands88
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
89/122
Copyright IBM Corporation 2011
It examines and counts the number of arguments say, n issued to it, creates n+1 copies of
the input it receives, and writes n copies to the n files specified to it as arguments, and onecopy to the standard output file.
Syntax:
tee file1 file2 file3
Filters: tee
Unix structure and Commands89
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
90/122
Copyright IBM Corporation 2011
It paginates or columnates files for printing.
Syntax:
pr [ file1 file2 file3 ]
Options:
D: This will double space the output.
D: This will use format for the header date.
L : This sets the page length.
W: This sets the page width.
Filters: pr
Unix structure and Commands90
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
91/122
Copyright IBM Corporation 2011
It extracts specified characters or fields from the input file.
Syntax:
cu t [] [ ]
Options:
c: Used to specify the characters, that have to be extracted from every line of input file
d: Used to specify field delimiter, based on which, cut would understand the fields
f: Used to specify the number of the fields that have to be extracted
Filters: cut
Unix structure and Commands91
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
92/122
Copyright IBM Corporation 2011
It expects two files as input files, joins corresponding lines of the two files, and sends the
output to standard output file.
Syntax:
paste [] [] []
Filters: paste
Unix structure and Commands92
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
93/122
Copyright IBM Corporation 2011
Performs calculation on the input expression. Takes input for standard input and sends
processed output to standard output.
Syntax:
bc
bc c alcf i le # calcf i le contains expression
Examples:
$ bc # issues input interrupt
5^3 # f inds 5 raised to pow er of 3
125 # and displays outpu t on screen
qui t # terminates bc prog ram
$ # and returns con trol back to shel l
Filters: bc
Unix structure and Commands93
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
94/122
Copyright IBM Corporation 2011
A pipe is a communicating process. It receives output of one command and sends it as
input to another command.
comm and1 | comm and2
Example:
ls t | head -15
Output of ls command is passed on to head command as its input, through the pipe.
Pipes
Command1 Command2
Unix structure and Commands94
IBM Global Business Services IBM
-
5/20/2018 Unix Structure
95/122
Copyright IBM Corporation 2011
Questions and answers
Unix structure and Commands95
IBM Global Business Services IBM
K l d h k
-
5/20/2018 Unix Structure
96/122
Copyright IBM Corporation 2011
Knowledge check
What is the command that receives the output of another command as its argument?
What filter extracts specified characters or fields from the input file?
What receives output of one command and sends it as input to another command?
Unix structure and Commands96
-
5/20/2018 Unix Structure
97/122
IBM Global Business Services
Copyright IBM Corporation 2010
Topic 6: Locating Files withfind
IBM Global Business Services IBM
S b t i d
-
5/20/2018 Unix Structure
98/122
Copyright IBM Corporation 2011
Sub-topics covered
The sub-topics covered in this topic include:
find
find example
find example (2)
find example (3)
Unix structure and Commands98
IBM Global Business Services IBM
fi d
-
5/20/2018 Unix Structure
99/122
Copyright IBM Corporation 2011
It searches for the files with specified criteria, recursively starting from the specified
directory, and takes action on those files. The default action is print.
Syntax:
f ind
Examples:
$ find $HOME -name "*.kor n" -print
/h ome/mural i /k o rnd ir /n amechange.korn
/h ome/mural i /k o rnd ir /u n t i lDemo.ko rn
/h ome/mural i /k o rnd ir /cmdArgs .ko rn
/h ome/mural i /k o rnd ir /w h i le2Demo.ko rn
find
Unix structure and Commands99
IBM Global Business Services IBM
fi d
-
5/20/2018 Unix Structure
100/122
Copyright IBM Corporation 2011
List of operators and their purpose:
find
-name : search for files matching specific names; find supports wild cards
-mtime n : File was last modified n*24 hours ago
-atime n : File was last accessed n*24 hours ago-amin n : File was last accessed n minutes ago
-cmin n : Files status last changed n minutes ago
-mmin n : Files data was last modified n mins ago
-newer rfile : Files that are modified more recently than rfile
-inum n : File has inode number n.
-links n : File has n links
-user uname : File is owned by user uname (numeric user ID allowed).
Unix structure and Commands100
IBM Global Business Services IBM
fi d
-
5/20/2018 Unix Structure
101/122
Copyright IBM Corporation 2011
List of operators and their purpose:
find
-size n[cwbkMG]: File uses n units of space.
The following suffixes can be used:b : for 512 bytes blocks (this is default if no suffix is used)
C : for bytes
w : for two-byte words
k : for kilobytes (units of 1024 bytes)
M : for megabytes (units of 1048576 bytes)
G : for Gigabytes (units of 1073741824 bytes)
Unix structure and Commands101
IBM Global Business Services IBM
find
-
5/20/2018 Unix Structure
102/122
Copyright IBM Corporation 2011
List of operators and their purpose:
find
-type c : file is of type c
b : block special
c : character special
ddirectory
pnamed pipe (FIFO)
fregular file
lsymbolic link
ssocket
Unix structure and Commands102
IBM Global Business Services IBM
find: example (1)
-
5/20/2018 Unix Structure
103/122
Copyright IBM Corporation 2011
$ find /tmp -m time +30 -prin t
f ind: /tmp /orbi t -gdm : Permis sion denied
/tmp/m app ing-roo t
f ind: / tmp/gconfd-user1: Permis sion d enied
find: example (1)
Explanation of code:
find command recursively searches for files under the directory /tmp
which are not modified within past 30 days and prints them.
Unix structure and Commands103
IBM Global Business Services IBM
find
-
5/20/2018 Unix Structure
104/122
Copyright IBM Corporation 2011
List of operators and their purpose:
find
-print: print the full file name on the standard output, followed by
a newline.
-exec: executes a unix command placing files output by find,
one, for each execution
-ok: executes a unix command placing files output by find, one,
for each execution non-interactively
Unix structure and Commands104
IBM Global Business Services IBM
find: example (1)
-
5/20/2018 Unix Structure
105/122
Copyright IBM Corporation 2011
$ pwd
/h ome/m ural i
$ f ind . -l inks +1 -type f -print 2> /dev/nul l
. /cppdir/new.cpp
./cppdir/vector1.cpp
find: example (1)
Explanation of code:
find searches recursively under home directory of user, for regular
files whose link count is more than one, that is, hard linked files.
Unix structure and Commands105
IBM Global Business Services IBM
find: example (2)
-
5/20/2018 Unix Structure
106/122
Copyright IBM Corporation 2011
$ find / -size 0c -print 2> /dev/nu l l |xargs ls -l 2> /dev/nul l 1> ZeroBy teFiles &
[1] 30299
find: example (2)
Explanation of code:
find searches recursively under directory /, whose file size is zero bytes
and pipes output to xargs and xargs places. The list of files as arguments
to ls command; ls command redirects output to the file ZeroByteFiles and
errors are redirected to /dev/null; this command is run in background.
Unix structure and Commands106
IBM Global Business Services IBM
find: example (2) (continued)
-
5/20/2018 Unix Structure
107/122
Copyright IBM Corporation 2011
$ find / -perm 777 -type f 2> /dev/nul l | xargs ls -l
-rwx rw xrw x 1 user20 user20 11688 2009-06-25 15:41
/h ome/u ser20/.b as h_h is to ry
-rwx rw xrw x 1 user20 user20 33 2009-03-06 15:43
/h ome/u ser20 /.bas h_logou t
-rwx rw xrw x 1 user20 user20 181 2009-05-11 12:57
/h ome/u ser20 /.bas h_pro f i le-rwx rwxrwx 1 user20 user20 124 2009-03-06 15:43 /hom e/user20/.bashr c
find: example (2) (continued)
Explanation of code:
The find command recursively lists all regular files under the directory /whose
octal permission mode is 777; errors are discarded; xargs receives list of files
output by find through pipe and places this list as arguments to ls command, and
transfers control to ls command; ls command displays long-listing of these files.
Unix structure and Commands107
IBM Global Business Services IBM
find: example (3)
-
5/20/2018 Unix Structure
108/122
Copyright IBM Corporation 2011
$ tou ch -t 07070900 my reff i le
$ find $HOME -newer m yreff i le -prin t 2> /dev/nul l
/h ome/mural i
/h ome/mural i /.lesshs t
/h ome/mural i /ZeroBy teF iles
find: example (3)
Explanation of code:
The find command recursively lists all files under the home directory of user, files
whose modification timestamp is later than the file, myreffile; find displays output
and sends errors to /dev/null.
Unix structure and Commands108
IBM Global Business Services IBM
find: example (3) (continued)
-
5/20/2018 Unix Structure
109/122
Copyright IBM Corporation 2011
$ find . -m time +365 -type f -exec ls -l {} \; # -exec executes a shell cmd
# on the output of f ind command
-rw-r--r-- 1 murali m urali 658 2007-10-11 19:46 ./.zsh rc
-rw-r--r-- 1 mu rali m urali 33 2007-08-31 19:50 ./.bash_logou t
$ find . -size 0 -typ e f -ok rm {} \; # -ok interact ively confirm s from user< rm ... ./usr s_at_now > ? Y # before executin g shell comm and
< rm ... ./no_access_tmp > ? Y # on every argum ent
< rm ... ./mynewerrf i le > ? n
< rm ... ./users > ? y
< rm ... ./my perlsc ripts /scores_hash .pl > ? n
< rm ... ./my per lscr ipts/mysu bst i tute.p l > ? n
< rm ... ./cobo lpro gs > ? y
find: example (3) (continued)
Unix structure and Commands109
IBM Global Business Services IBM
find: example (3) (continued)
-
5/20/2018 Unix Structure
110/122
Copyright IBM Corporation 2011
find: example (3) (continued)
Explanation of code:
The find command recursively outputs all regular files under working
directory of user, which are not modified since the past 365 days;
exec action of find command places the files one by one as
argument to ls command and executes ls command.
Unix structure and Commands110
IBM Global Business Services IBM
Questions and answers
-
5/20/2018 Unix Structure
111/122
Copyright IBM Corporation 2011
Questions and answers
Unix structure and Commands111
IBM Global Business Services IBM
Knowledge check
-
5/20/2018 Unix Structure
112/122
Copyright IBM Corporation 2011
Knowledge check
find / -type fsize +32print
Output of the above command is:
a) Regular files that are greater than 32 bytes in sizeb) Regular files that are greater than 32 MB in sizec) Regular files that are greater than 32 blocks in sized) Regular files that are exactly 32 blocks in size
Unix structure and Commands112
-
5/20/2018 Unix Structure
113/122
IBM Global Business Services
Copyright IBM Corporation 2010
Topic 6: Thexargs command
IBM Global Business Services IBM
Sub-topics covered
-
5/20/2018 Unix Structure
114/122
Copyright IBM Corporation 2011
Sub topics covered
The sub-topics covered in this topic include:
xargs
xargs commands
Unix structure and Commands114
IBM Global Business ServicesIBM
xargs
-
5/20/2018 Unix Structure
115/122
Copyright IBM Corporation 2011
It reads arguments from standard input, place theses as arguments to the shell command,
and runs the command on these arguments.
Syntax:
xargs
Example:
$ xargs grep "cout"
autodemo.c++
mynewprog.c++
autodemo.c++: cout
-
5/20/2018 Unix Structure
116/122
Copyright IBM Corporation 2011
Here is another example:
Example:
$ ls *.c++ | xargs tar -cvf cppArchive.tar
autodemo.c++
inheritance.c++
methOverriding.c++
mynewprog.c++
scope.c++
test.c++
virtualFunction.c++
$ ls -l cppArchive.tar
-rw-rw-r-- 1 murali murali 10240 2009-07-07 12:34 cppArchive.tar
xargs
Unix structure and Commands116
IBM Global Business ServicesIBM
xargs
-
5/20/2018 Unix Structure
117/122
Copyright IBM Corporation 2011
Another example can be illustrated as follows:
Example:
$ find . -mtime +180 -type f -name "*.txt" | xargs -I {} -t mv {} {}.old
mv ./logFile.txt ./logFile.txt.old
mv ./links.txt ./links.txt.old
mv ./link.txt ./link.txt.old
xargs
Unix structure and Commands117
IBM Global Business ServicesIBM
xargs commands
-
5/20/2018 Unix Structure
118/122
Copyright IBM Corporation 2011
Some xargs commands include:
-I replace-str: It replaces occurrences of replace-str in the initial-arguments with
names read from standard input.
-t: It prints the command line on the standard error output before executing it.
- diff filename1.txt filename2.txt:It reports the difference between the files
- compress filename.txt: It compresses the filename filename.txt
tailf filename.txt:It lists the last entry of the file filenname.txt run time
mailx -s test email emailid < test.out:It sends an e-mail to emailid with thesubject test email with the content of the file test.out
passwd:It allows the user to change his or her password on the server man command:It shows the manual page for the command
xargs commands
Unix structure and Commands118
IBM Global Business ServicesIBM
Questions and answers
-
5/20/2018 Unix Structure
119/122
Copyright IBM Corporation 2011
Q
Unix structure and Commands119
IBM Global Business ServicesIBM
Knowledge check
-
5/20/2018 Unix Structure
120/122
Copyright IBM Corporation 2011
g
User types the command xargs at the $ prompt, and presses key. What is the
response?
User types the following command at the $ prompt; what is the response? $ find / 2>/dev/null | xargs grepl DISPLAY > outfiles 2> /dev/null & tar tf shellprogs.tar | xargs cat |more
Unix structure and Commands120
IBM Global Business ServicesIBM
Tying the ends
-
5/20/2018 Unix Structure
121/122
Copyright IBM Corporation 2011
y g
By the end of this module on Unix Structure and Commands, you are expected to:
Paraphrase the basics of Unix File System
List file handling commands
Summarize vi editor basics
Analyze command substitution, filters, and pipes
Illustrate locating files with find
Interpret xargs command
Unix structure and Commands121
IBM
-
5/20/2018 Unix Structure
122/122
IBM Global Business Services