wecome to unix
TRANSCRIPT
-
8/11/2019 Wecome to UNIX
1/40
-
8/11/2019 Wecome to UNIX
2/40
Course Outline Introduction Logging On and Off
Unix File System Structure
Dealing with Directories Handling Files
Shell Basics
Shell Programming
Customisation
-
8/11/2019 Wecome to UNIX
3/40
Introduction Unix operating system first Version was developedin 1969 by Ken Thompson in assembly language. In1973 UNIX was written in C language.
Unix is more powerful, more reliable, can do muchmore work, much faster and you do not need toreboot your computer everyday.
Best part about UNIX in C was portability
-
8/11/2019 Wecome to UNIX
4/40
Features of Unix
A multitasking and multiuser system Each user has his/her own usernameor id
Many users can log in simultaneously
Access to files and programs is based on username
Each user may run multiple tasks
-
8/11/2019 Wecome to UNIX
5/40
Logging On Users must identify themselves to the system
this is called logging on
At the login:prompt enter your idthen pressEnter(or return)
Atthepassword:prompt enter your passwordnote that you cannot see what you are typing
Change your password with thepasswd
command
-
8/11/2019 Wecome to UNIX
6/40
Logging Off Disconnecting from the system is called logging off
Varies from system to system
exit logout
^D
-
8/11/2019 Wecome to UNIX
7/40
Unix File System Organised as an inverted treeof directories
Begins at theroot /
Directories contain files and other directories Every user is allocated a home directory
-
8/11/2019 Wecome to UNIX
8/40
Paths and PathnamesApathname(orpath)is a route from the root to the
file or directory in question
Directory names are separated by /s
A path beginning with a / is an absolutepath
A path beginning with anything but a / is a relativepath
-
8/11/2019 Wecome to UNIX
9/40
Working Directories and Pathnames Relative paths are based upon the current working
directory
Change the working directory with the cdcommand
Show the working directory with thepwdcommand
List the contents of the working directory with thelscommand
-
8/11/2019 Wecome to UNIX
10/40
-
8/11/2019 Wecome to UNIX
11/40
Inodes Data structure to store administrative information
about a regular file, directory, or other filesystem object
Permission
Time stamps last modified, last used & last changed(inode)
Disk location Represented by a number called I-Number(ls i)
-
8/11/2019 Wecome to UNIX
12/40
Creating and Removing DirectoriesA new directory can be created with the mkdir
command
Note that this cannot be shortened to md
A directory can be removed using the rmdircommand
The directory must be empty (no files or subdirectories)
The name cannot be shortened to rd
-
8/11/2019 Wecome to UNIX
13/40
Manipulating Files Files are usually created by editors, compilers etc. rmremoves files (and directories)
cpcopies files (and directories)
mvmoves (renames) files (and directories) lslists files (not the contents of files)
cat displays
-
8/11/2019 Wecome to UNIX
14/40
Wildcard ExpressionsWe often wish to apply a command to a selection of
files or directories
The shell wildcardfeature allows this ls *.c
lists all files whose names end in .c
-
8/11/2019 Wecome to UNIX
15/40
Wildcard ExpressionsWhen we type in a wildcard expression it isexpanded by the shellbefore the command runs
If we type is ls *.cand the working directory
containsprog1.c, abc.candxyz.c then the actualcommand is
ls abc.c prog1.c xyz.c
The wildcard expression has been expanded in place
-
8/11/2019 Wecome to UNIX
16/40
Wildcard Characters ? matches any character
* matches any number of characters
[abc] matches any of the characters in the
brackets [!abc] matches anything exceptthe characters in
the brackets
[a-z] matches the range of characters in the
brackets
-
8/11/2019 Wecome to UNIX
17/40
Wildcard Examples abc?def
matches abcXdef, abc.def, abc3def but notabcdef
abc*
matches abc. abcd, abcdef, abc.another.part *.*
matches any name with a .
-
8/11/2019 Wecome to UNIX
18/40
Wildcard Examples *
matches any name (except a name beginning with a.)
[abc]def matches adef, bdef and cdef
[a-z]*
matches any name beginning with a lower-case letter
-
8/11/2019 Wecome to UNIX
19/40
Wildcard Examples
[a-zA-Z]*
any name beginning with an alphabetic character
[!a-zA-Z]*
any name beginning with a non-alphabetic character
[a-z]*[!A-Z]
any name beginning with a lower-case letter andending with anything except an uppercase letter
-
8/11/2019 Wecome to UNIX
20/40
Avoiding Wildcard Expansion
Sometimes it is necessary to enter a wildcardcharacter on a command line, but not have itexpanded
Quote the argument containing the character ls abc*def
ls the file whose name is abc*def (if the system allows sucha name)
ls abc*def the same
-
8/11/2019 Wecome to UNIX
21/40
Avoiding Wildcard Expansion
Escape the character
ls abc\*def
again lists the file whose name is abc*def
These can be used in combination echo Im a * and \* matches *
* inside the double quotes is not expanded
* after the \ is not expanded
only the final asterisk is expanded
-
8/11/2019 Wecome to UNIX
22/40
File Permissions
Unix provides three distinct permissions for files:
read permission to examine the contents of a file
write permission to alter the contents of a file
execute permission to run a file as a program or shellscript
-
8/11/2019 Wecome to UNIX
23/40
Directory Permissions
The same permissions apply to directories, but withdifferent meanings:
read permission to look at the contents of
the directory, i.e. to see file names write permission to create or delete files
within the directory
execute permission to execute (permission to list)
-
8/11/2019 Wecome to UNIX
24/40
Listing Permissions
the -l(long) argument to the lscommand listspermissions
-rw-rw-rw- a file with read and write for the owner, group and
world
drwxr-xr-x a directory with read/write/execute for the owner,
read/execute for group and world
-
8/11/2019 Wecome to UNIX
25/40
Setting Permissionschmod
The chmod (change mode) command sets and unsetsfile and directory permissions
chmod mode filename(s)
wheremodecan be:
who op permission op2 permission2 ...
octalnumber
-
8/11/2019 Wecome to UNIX
26/40
Permissions the Quick Way
chmodalso allows octal representation
chmod 755 directory
Set rwx for owner, rx for group and world
chmod 644 file
Set rw for owner, r for group and world
chmod 711 file
Set rwx for owner, x for group and world
-
8/11/2019 Wecome to UNIX
27/40
Creating Files
Most human-readable files on Unix are created with atext editor
Unix has many, many different editors
ed a very old line-oriented editor
ex an enhanced version of ed
vi the most ``popular editor a
Visual Interface to ex
-
8/11/2019 Wecome to UNIX
28/40
The vi Editor Created by Bill Joy at UCB
Holds file being edited as an internal buffer
Screen acts as a windowonto the buffer
Operates in two basic modes Command mode
Insert mode
-
8/11/2019 Wecome to UNIX
29/40
Starting vi vi
Starts viwith an empty buffer
vi file
Starts vi opensfilefor editing vi +n file
Starts viopensfileat line n
vi +/pattern file
Starts viopensfileat the first line matchingpattern vialways starts in command mode
-
8/11/2019 Wecome to UNIX
30/40
vicommand mode
In command mode every charactertyped is acommand
You can move about in the buffer
Characters/lines can be deleted or moved
To enter text shift to insert mode
-
8/11/2019 Wecome to UNIX
31/40
viinsert mode
In insert modecharacters typed are entered into thebuffer
Shift from command to insert move using:
i insert textbefore cursor a append text after cursor
Shift from insert back to command mode by typingESCape
-
8/11/2019 Wecome to UNIX
32/40
vimoving around the file
Only part of the buffer is usually visible onscreen
In command mode can navigate through the file
Can choose to move: to a position within the buffer
to a position on the screen
to a particular text string
-
8/11/2019 Wecome to UNIX
33/40
vipositioning the cursor
Examples:
H Go to top line on screen
L Go to last line on screen
M Go to middle line on screen h Move cursor left
j Move cursor down
k Move cursor up
l Move cursor right
Or you could use the arrow keys....
-
8/11/2019 Wecome to UNIX
34/40
vimore movement
n Go to line n
/textCR Find text(forward search)
?textCR Find text(backward search)
n Find next occurrence of text(same direction)
N Find next occurrence of text(opposite direction)
-
8/11/2019 Wecome to UNIX
35/40
vicomplex commands
Typing the :character in command mode causes aprompt to appear at the bottom of the screen
excommands can be typed here
:q
quit vi
will complain if there are unsaved changes
:q!orZZ quit and lose any unsaved changes
-
8/11/2019 Wecome to UNIX
36/40
vireading and writing files
:rfilename
read the named file into the buffer
:w filename write the buffer to named file
:w
rewrite the buffer to an already named file
-
8/11/2019 Wecome to UNIX
37/40
Useful Commands
Among the many Unix commands a few stand outas being particularly essential
These include
grep
-
8/11/2019 Wecome to UNIX
38/40
Searching Files with grep
Thegrepcommand is actually a family
grep
All search files for strings which match specifiedpatterns
grep Failure *
-
8/11/2019 Wecome to UNIX
39/40
More on grep
greps behaviour is modifiable via command lineoptions
-c display only count of matching
lines-i ignore casewhenmatching
-l display only names of files withmatching lines
-n display line numbers-v display only non-matching lines
-
8/11/2019 Wecome to UNIX
40/40
The find Command Thefindcommand walks down a subtree of the file
system looking for files which match certaincreteria, such as:
wildcards file type (e.g. file, directory, special, link)
files owner
access time
modification time