0% found this document useful (0 votes)
58 views41 pages

Intro Unix 02

This document provides an introduction to the Unix file system and common Unix commands. It discusses how the Unix file system stores and organizes files in a hierarchical structure with directories and subdirectories. It also summarizes several important Unix commands for listing, viewing, printing and redirecting files, such as ls, cat, more, pr, lpr, and redirection operators like | (pipe) and >>. The commands can be used with various options to filter, sort and format their output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
58 views41 pages

Intro Unix 02

This document provides an introduction to the Unix file system and common Unix commands. It discusses how the Unix file system stores and organizes files in a hierarchical structure with directories and subdirectories. It also summarizes several important Unix commands for listing, viewing, printing and redirecting files, such as ls, cat, more, pr, lpr, and redirection operators like | (pipe) and >>. The commands can be used with various options to filter, sort and format their output.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Introduction to UNIX

Unix File System


Stores Files
Text, Programs, Images, etc.

Made up of Many Files


User, Unix System, Unix Commands

Unix File System


Top most
directory in Unix

ro o t
usr

Home
Directory

var

hom e

e tc

r d e fe

js m ith

d jo n e s

d a ta

m b o x , le tte r,
p ro g 1 , p ro g 2

ja n

fe b

file 1 , file 2 ,
file 3 , file 4

file 1 , file 2 ,
file 3 , file 4

save

lib

m b o x , ja n d a t
in fo

Directory
containing all
home
directories

x1, x2, x3, x4

Unix File System


Home Directory
Personal Directory to store files
Usually the Directory Name = login ID
Current Directory after successful login
ro o t
usr

Home directories for:


rdefe, jsmith and
djones

var

hom e

e tc

rd e fe

js m ith

d jo n e s

d a ta

m b o x , le tte r ,
p ro g 1 , p ro g 2

ja n

fe b

file 1 , file 2 ,
file 3 , file 4

file 1 , file 2 ,
file 3 , file 4

save

lib

m b o x , ja n d a t
in fo

x1, x2, x3, x4

Unix File System


What Can You Do?
Create New Files & Directories
Organize Files
Locate Files Based on a Directory
Structure
Change to a New Directory

Unix File System


File & Directory Naming Guidelines
Dont Use Meta Characters
/\`*;?{}()[]~!$<>|&#

Do Use
a-z A-Z
0-9
._-

Remember Unix is Case Sensitive!


6

Unix File System

File & Directory Name Examples


feb1998, 178987, my_data,
my.data.new
mbox, Mbox, mboX, MBOX, mBox
prog1..2
.profile
7

Unix Commands
ls
List Files & Directories
$$lsls
198
198
AA
aa.bb.cc.dd
aa.bb.cc.dd
bin
bin
datefile
datefile
etc
etc
mail
mail
mbox
mbox
mbox.old
mbox.old
unix
unix
$$

Unix Commands
ls -p
-p Show Directories
$$lsls-p
-p
198
198
AA
aa.bb.cc.dd
aa.bb.cc.dd
bin/
bin/
datefile
datefile
etc/
etc/
mail/
mail/
mbox
mbox
mbox.old
mbox.old
unix/
unix/
$$

Unix Commands
ls [directory1] [directory2] ...
List the contents of a directory
$$lsls-p
-p
198
198
AA
aa.bb.cc.dd
aa.bb.cc.dd
bin/
bin/
datefile
datefile
etc/
etc/
mail/
mail/
mbox
mbox
mbox.old
mbox.old
unix/
unix/
$$

$$ls
lsunix
unix
awk.data
awk.data
awk.prg
awk.prg
bgraph
bgraph
cpy
cpy
homework2
homework2
homework3
homework3
unix2test
unix2test
$$

10

Unix Commands
ls -s
-s Show File Size in Blocks
Block Sizes
$$ls
ls-s
-s
512 bytes
1024 bytes
2048 bytes

total
total19
19
11198
198
11AA
11aa.bb.cc.dd
aa.bb.cc.dd
11bin
bin
11datefile
datefile
11etc
etc
11mail
mail
22mbox
mbox
11unix
unix

11

Unix Commands
ls -l
-l Long Listing
$$ ls
ls -l
-l
total
total 19
19
-rw-r--r--rw-r--r--rw-r--r--rw-r--r-drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
-rw-------rw------drwxr-xr-x
drwxr-xr-x
$$

11 rdefe
rdefe
11 rdefe
rdefe
22 rdefe
rdefe
33 rdefe
rdefe
22 rdefe
rdefe
22 rdefe
rdefe
11 rdefe
rdefe
22 rdefe
rdefe

unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix

53
53 Sep
Sep 12
12 21:46
21:46 198
198
62
62 Sep
Sep 12
12 21:47
21:47 AA
96
96 Sep
Sep 44 20:58
20:58 bin
bin
80
Aug
29
20:54
db
80 Aug 29 20:54 db
64
64 Aug
Aug 29
29 20:54
20:54 etc
etc
32
32 Sep
Sep 22 1992
1992 mail
mail
984
984 Sep
Sep 12
12 21:44
21:44 mbox
mbox
144
144 Aug
Aug 29
29 20:54
20:54 unix
unix

12

Unix Commands
ls -a
-a List All Files

Include Files That


Begin with a .
Files that begin with a . are usually
configuration files

$$ls
ls-a
-a

..
....
.profile
.profile
.sh_history
.sh_history
198
198
AA
bin
bin
datefile
datefile
db
db
etc
etc
mbox
mbox
unix
unix
$$

13

Unix Commands
ls -r
-r Reverse Sort

ls -R
-R List Contents
of Subdirectories

List files in each subdirectory.


Subdirectories with Subdirectories
will also be listedand so on...

$$ls
ls-r-r
unix
unix
mbox
mbox
mail
mail
etc
etc
db
db
datefile
datefile
bin
bin
AA
198
198
$$

14

Unix Commands
ls -l -t or

ls -lt

-t Sort On Modification Time


$$ ls
ls -lt
-lt
total
total 19
19
-rw-r--r--rw-r--r--rw-r--r--rw-r--r--rw-------rw------drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
$$

11 rdefe
rdefe
11 rdefe
rdefe
11 rdefe
rdefe
22 rdefe
rdefe
22 rdefe
rdefe
22 rdefe
rdefe
33 rdefe
rdefe
22 rdefe
rdefe

unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix

62
62 Sep
Sep 12
12 21:47
21:47 AA
53
53 Sep
Sep 12
12 21:46
21:46 198
198
984
984 Sep
Sep 12
12 21:44
21:44 mbox
mbox
96
Sep
4
20:58
bin
96 Sep 4 20:58 bin
64
64 Aug
Aug 29
29 20:54
20:54 etc
etc
144
Aug
29
20:54
unix
144 Aug 29 20:54 unix
80
80 Aug
Aug 29
29 20:54
20:54 db
db
32
32 Sep
Sep 22 1992
1992 mail
mail

15

Unix Commands
ls -l -t -r

or

ls -ltr

-tr Reverse Sort On Modification Time


$$ ls
ls -ltr
-ltr
total
total 19
19
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
drwxr-xr-x
-rw-------rw-------rw-r--r--rw-r--r--rw-r--r--rw-r--r-$$

22 rdefe
rdefe
33 rdefe
rdefe
22 rdefe
rdefe
22 rdefe
rdefe
22 rdefe
rdefe
11 rdefe
rdefe
11 rdefe
rdefe
11 rdefe
rdefe

unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix
unix

32
32 Sep
Sep 22 1992
1992 mail
mail
80
Aug
29
20:54
db
80 Aug 29 20:54 db
144
144 Aug
Aug 29
29 20:54
20:54 unix
unix
64
Aug
29
20:54
etc
64 Aug 29 20:54 etc
96
96 Sep
Sep 44 20:58
20:58 bin
bin
984
Sep
12
21:44
mbox
984 Sep 12 21:44 mbox
53
53 Sep
Sep 12
12 21:46
21:46 198
198
62
62 Sep
Sep 12
12 21:47
21:47 AA

16

Unix Commands
ls -[ltrRa] [Filename1] [Filename2]
UnixCommand Options Arguments
$$ ls
ls -lt
-lt unix
unix
$$

17

Unix Commands
cat [Filename1] [Filename2]

Concatenate Files
Display Files to the Screen
Ctrl-s Pause Screen
Ctrl-q Unpause Screen

$$ cat
cat sample.file
sample.file
This
This is
is aa sample
sample file
file that
that i'll
i'll use
use to
to demo
demo how
how
the
the pr
pr command
command is
is used.
used.
The
The pr
pr command
command is
is useful
useful in
in formatting
formatting
various
various types
types of
of text
text files.
files.
$$

18

Unix Commands
more [Filename1] [Filename2]
Display Files to the screen one page at a time
$$ more
more largefile
largefile
This
This is
is aa sample
sample file
file that
that i'll
i'll use
use to
to demo
demo how
how
the
the pr
pr command
command is
is used.
used.
The
The pr
pr command
command is
is useful
useful in
in formatting
formatting
various
various types
types of
of text
text files.
files.
--More--(16%)
--More--(16%)
Spacebar (next page) q (Quit & return to unix)
/string (search for string)
b (previous page)
Enter Key (next line) :n (next file)
:p (previous file)

19

Unix Commands
pr [Filename1] [Filename2]
Format & Display Files to the Screen
$$ pr
pr sample.file
sample.file
98-05-22
98-05-22 23:07
23:07

sample.file
sample.file

Page
Page 11

This
This is
is aa sample
sample file
file that
that i'll
i'll use
use to
to demo
demo how
how
the
the pr
pr command
command is
is used.
used.
The
The pr
pr command
command is
is useful
useful in
in formatting
formatting
various
various types
types of
of text
text files.
files.
$$

20

Unix Commands
pr -[dn] [Filename1] [Filename2]
-d Double Space Output
-n Number Lines
$$ pr
pr -d
-d sample.file
sample.file
98-05-22
98-05-22 23:07
23:07

sample.file
Page
sample.file
Page 11
$$ pr
sample.file
pr -n
-nthat
sample.file
This
is
a
sample
file
This is a sample file that i'll
i'll use
use to
to demo
demo how
how
98-05-22
23:07
Page
98-05-22
23:07 sample.file
sample.file
Page 11
the
pr
command
is
used.
the pr command is used.
11 This
is
This
is aa sample
sample file
file that
that i'll
i'll use
use to
to demo
demo how
how the
the
The
pr
command
is
useful
in
formatting
The pr command is useful
in
formatting
22 pr
command
is
used.
pr command is used.
33 The
The pr
pr command
command is
is useful
useful in
in formatting
formatting
various
types
of
text
files.
various types of text
44 files.
various
types
of
text
files.
various types of text files.
$$
$$

21

Unix Commands
lpr [Filename1] [Filename2]
Send Files to a Unix Printer
No Formatting (page numbers, etc.)
$$ lpr
lpr prog1
prog1 mbox
mbox
$$
Ideal solution would
be to combine the
lpr & pr to achieve
formatted printed
output

$$ pr
pr sample.file
sample.file
98-05-22
98-05-22 23:07
23:07 sample.file
sample.file Page
Page 11
This
This is
is aa sample
sample file
file that
that i'll
i'll use
use
to
demo
how
the
pr
command
is
used.
to demo how the pr command is used.

...
...

22

Unix Redirection
Output Normally Displayed to the Screen
is Redirected and Becomes the Input to
Another Command

$$ pr
pr prog1
prog1 || lpr
lpr
$$
23

Unix Redirection
The Output of One Command can be
used as the Input to Another
Command that
generates screen
output

$$ pr
pr prog1
prog1 || lpr
lpr
$$

Command | Command
Command that
requires input

24

Unix Redirection

Command | Command | Command ...


$$ ls
ls || pr
pr || lpr
lpr
$$
Directory Listing Formatted
Using pr, Printed on Paper
25

Unix Redirection
Output Normally Displayed to the
Screen Can Be Redirected to a File

$$ pr
pr prog1
prog1 >> prog1.pr
prog1.pr
$$
Note: Nothing is displayed to
the screen

The file prog1.pr contains


the output of the pr command
26

Unix Redirection
Redirecting Output
The Output of Command can be saved to a
File
$$ pr
pr prog1
prog1 >> prog1.pr
prog1.pr
$$

Command that
generates screen
output

Command > File


File used to capture
(save) screen output
Caution: Output File will
be erased if it already
exists.

27

Unix Redirection
Redirecting Input
A Command Requiring Input Can Receive its
Input From a File

$$ mail
mail rdefe
rdefe << mesg
mesg
$$
28

Unix Redirection
Redirecting Input
$$ mail
mail rdefe
rdefe << mesg
mesg
$$
Command that
requires input

Command < File


File used to provide input
(instead of typing it!)

29

Redirection Examples
1

$$
$$

pr
pr data
data >> data.pr
data.pr
lpr
lpr data.pr
data.pr

$$ cal
cal 33 1993
1993 >> year
year

Using arguments with redirection

$$ ls
ls -l
-l data
data >> stuff
stuff

Using arguments & options with


redirection

Is the same as

$$ pr
pr data
data || lpr
lpr

30

Redirection Examples
4

$$ pr
pr data
data >> lpr
lpr

$$ cat
cat text
text let
let >> mesg
mesg

$$ pr
pr f1
f1 f2
f2 >> f1
f1

Create the file f1


Erases f1 if it already
exists

pr f1 and f2, redirect to f1

31

Unix Commands
rm [Filename1] [Filename2]
remove files
$$ rm
rm prog1
prog1 mbox
mbox
$$

* is a Meta Char that


matches anything

$$ rm
rm p*
p*
$$

Caution

$$ rm
rm **
$$

Unix Assumes You Know What Your Doing


Be Sure You Know What Your Deleting
No Undelete!

32

Unix Commands
vi [Filename]

Full Screen, Visual Editor


Changes Displayed As They Are Made
Edits A Copy of the File
Changes Must Be Saved

$$ vi
vi mesg
mesg

emacs
http://www.gnu.org/software/emacs/emacs.html

33

Unix Commands
Vi Modes
Command Mode
Start in command mode
Every Key Performs an Editing Command

Insert Mode
Perform Text Entry/Input
Insert Mode Command
Command Mode

Insert Mode

ESC Key
34

Sample vi Edit Session


$$ vi
vi mesg
mesg

Start in Command Mode


Press a key for Append After Cursor Command
Enter text (Input Mode)
Press ESC key (Command Mode)
Press :wq followed by Enter key to Save & Exit

Welcome
Welcometo
toUNIX
UNIX
Wnewtextelcome
to
Wnewtextelcome
toUNIX
UNIX
This
is
an
example
of
This is an example of
aatext
file
in
vi
text
fileis
inan
vi example of
This
This
is
an
example of
~~
aatext
textfile
filein
invi
vi
~~
~~
~~
~~
:wq
:wq
$$

35

Cursor Positioning
Moving the Cursor in Command Mode

k
h

Key

Command

w Forward One Word


b
Backward One Word
Cntrl-d
Scroll Down
Cntrl-u
Scroll Up
Enter Down One Line
G Go To Last Line
5G
Go To Line 5

j
36

Text Input Mode


vi Text Input Mode Commands
Wnewtextelcome
Wnewtextelcometo
toUNIX
UNIX

a Append After Cursor

Welcome
to
This
is
of
Welcome
toUNIXnewtext
UNIXnewtext
This
isan
anexample
example
of
A Append at End of Line
aatext
textfile
filein
invivi
newtextWelcome
is
~~This
newtextWelcome
toUNIX
UNIX
This
isan
anexample
exampleof
ofto
i Insert Before Cursor
~~aatext
textfile
filein
invivi
isisan
~~ This
ThisnewtextWelcome
anexample
exampleof
ofto
I Insert at Beginning of Line
newtextWelcome
toUNIX
UNIX
~~ aatext
textfile
filein
invivi
~~ This is an example of
O Open New Line Above Cursor
Thisnewtext
is an example of
~~ a textnewtext
o Open New Line Below Cursor
file
Welcome
a text
filein
invivito
Welcome
toUNIX
UNIX
~~
~~ This is an example of
This is an example of
R Replace Text (typeover)
r Replace One Char
aatext
file
in
vi
text file in vi
~~
~~

37

Deleting Text
Welcome
Welcometo
toUNIX
UNIX
This
Thisis
isan
anexample
exampleof
of
aatext
textfile
filein
invivi
~~ This
Thisis
isan
anexample
exampleof
of
~~ aatext
textfile
filein
invivi
~~
~~
Proceed
4x
5dd
3dw

x Delete One Char


X Delete Char Before Cursor

dd

Delete One Line

Any Command with a Number to multiply effect


Delete 4 chars
Delete 5 lines
Delete 3 words

38

Undo Changes
Welcome
Welcometo
toUIX
UIX
Welcome
to
This
isisan
of
Welcome
toUNIX
UNIX
This
anexample
example
of
aatext
textfile
filein
invivi
~~ This
Thisis
isan
anexample
exampleof
of
WELcome
to
UIX
123
file
~~ aatext
WELcome
text
filein
invivi to UIX 123
~~
an
of
~~ This
Welcome
to
Thisis
is
anexample
example
of
Welcome
toUNIX
UNIX
aatext
textfile
filein
invivi
~~
This
Thisisisan
anexample
exampleof
of
~~
aatext
textfile
filein
invivi
~~
~~

x Delete One Char


u Undo Last Change

U Undo All Changes on Line

39

Saving Your Changes


Command Mode

ZZ
:wq
:w data
:q!

Save & Exit vi


Save & Exit vi
Save to a file data
Quit Without Saving Changes

Vi Reference

http://www.eng.hawaii.edu/Tutor/vi.html

40

Other Goodies...
/string Forward Search for `string`
?string Backward Search for `string`
n
N

Repeat Search
Repeat Search in reverse direction

Ctrl-G Display Current Location in File


J
Join two lines together
http://www.thomer.com/thomer/vi/vi.html
41

You might also like