Transcript Lecture 2

Lecture 2
The Structure of Directories
Filename Metacharacters
Handling
Special Characters
Input / Output Redirection
Building Complex Commands
Shell Aliases
The Structure of Directories
 Tree-like
Structure
 The top level is called root and is
represented by /
 The representation of directory is
represented as /aa/bb/cc
 A file in /aa/bb/cc can be represented as
/aa/bb/cc/file
Several Important Directories
/
: root file system
 /bin

Contains executable files
 /etc


Configuration files
Some executable shell files
Cont.
 /usr





Contains many sub-directories
/usr/inlcude and /usr/lib
/usr/home- the home directories of users
/usr/bin/ - contains more executables
/usr/local- softwares from the third-party
 /sbin

Contains executables which can only be
executed by the super user (root)
Cont.
 /home

Like /usr/home
 /dev

or /devices
Contains device files
 /var

Contains files the content of which will be
changed frequently. For example : mails , logs
and so on.
Cont.
 /proc

A special file system which contains runtime
information of the system
 /sys

A special file system which contains the
information of the system and its kernel
 /mnt

Can be used to mount other file sytems
The Home directory
 When
you log onto the system , you will be
assigned a home directory;
 Working directory – The directory which
you are currently in;

pwd- used to list your current working
directory
Two Necessary File System during Partition
/
- root file system
 /swap – Used to extend memory system.
The size should be double the amount of
memory
 Other file systems could be :

/opt; /home/ ; /var
Some Commands
 mkdir
-- create a directory
 ls ---- list the files in a directory
 df ---- display the number of the blocks of
a file system
 mount – mount a file system to a directory
Access Rights For A File

Three groups of Access rights




Owner
The members within a group
Others
Each group of rights is represented as rwx





r mean read
W means write
E means executable
Each kind of right can be presented by 0 or 1
The combination of 3 kinds of rights is represented by
a octet number (0 - 7).
Cont.
For example:
[gub@mu gub]$ ls -al sommer.pdf
-rw-r--r-- 1 gub
cisgrad 257688 Aug 20 15:09 sommer.pdf

The access right of the file is





rw -> 110 ->6
r->100->4
r->100->4
The access right is 644
Change the access right of a file:

chmod xxx filename
Cont.
The following is required access right for a
file (test1.txt):
1. Owner: rwx
2. Group: r—
3. Others: --How to use chmod to change the access
right of test1.txt?
The File Creating Mask
 Umask
uu
 The required access for files is:

Default access right – uu
For example:
 Default : 555; umask 22
 What is the required access right?
Other commands related to the
access right of a file
 chown:
change the owner of a file
 chgrp: change the group of a file
 chmod p + access right


P can be u (user), g (group) ; o(other); a(all)
Access right can be r , w x.
Wildcards / Metacharacters
for filename abbreviation
 When
you type in a command line the
shell treats some characters as special.
These special characters make it easy to
specify filenames.
 The shell processes what you give it,
using the special characters to replace
your command line with one that includes
a bunch of filenames.
 Metacharacters: * ? [ ] ~
Metacharacters
Matches anything: ls Test*.doc
 ? Matches any single character
*
ls Test?.doc

[abc…] Matches any of the enclosed
characters: ls T[eE][sS][tT].doc

[a-z] matches any character in a range
ls [a-zA-Z]*
{aa,de,a}: Matches for a character or
set of characters
Back Quota
 ``-
used for command substitution
 Example:
 Set d = `date`
 Echo $d
 Tue Jan 13 00:07:50 EST 2009
Tilde Expansion
 Tilde
(~) by itself expands to the full
pathname of the user’s home directory
 Tilde followed by pathname expands to
home directory + pathname
 Tilde followed by username expands to
that user’s home directory
Special Characters

We've already seen that some characters mean
something special when typed on the command
line:


* ? []
Turn off the special meanings by
quoting/escaping



“”: disable the special meaning for all chars except $ `
(back quota) and ! (bang)
‘ ’: disable the special meaning for everything but ! and
newline
\: escapes any immediately following char
Quoting – Turn Off Special Chars

What if we don't want the shell to treat these as
special. Example, we really mean *, not all the
files in the current directory:
echo here is a star *

To turn off special meaning - surround a string
with double quotes:
echo here is a star "*"
echo "here is a star *"
Cont..
 If
the directory name is /Program files,
How can we access it?
 cd ?
More Examples
echo isn\’t it interesting\?
or % echo “isn’t it interesting?”
 vi my_file!.txt
%
.txt: event not found
Programs and Standard I/O
Standard Input
(STDIN)
•Keyboard
•Output from another program
•A file
Program
Standard Output
(STDOUT)
•Screen
•Input to another program
•A file
Standard Error
(STDERR)
Input/Output Redirection

When a file descriptor is assigned to something
other than a terminal, it is called I/O redirection



A file (the contents of the file are fed to a program as if
you typed it): wc < text_file
A pipe (the output of another program is fed as input as
if you typed it)
The shell can attach things other than your screen
to standard output .


A file (the output of a program is stored in file):
ls > lsout (>> for append)
A pipe (the output of a program is fed as input to
another program)
Pipes

A pipe is a holder for a stream of data.
 A pipe can be used to hold the output of one
program and feed it to the input of another.
 Separate 2 commands with the “|” character.
prog1
prog2
STDOUT
STDIN
Redirection/Pipe Examples
 wc
< test1
 ls > lsout
 ls >> lsout
 ls | wc
Building Complex Commands

You can string together a series of unix
commands to do something new
 Complex Command Forms:







I/O Redirection
cmd &: launch cmd in background
cmd1; cmd2: run commands in sequence
(…): grouping commands
cmd1 `cmd2`: use cmd2 output as arguments to cmd1
(backquotes)
cmd1 && cmd2: AND, execute cmd2 only if cmd1
finishes with non-zero status, i.e. if comd1 succeeds
cmd1 || cmd2: OR, execute cmd2 only cmd1 finishes
with the status of zero, i.e. if cmd1 fails
Self-Test
%
wc < nums > wcNums
 % ls | wc > wcLs
 % echo `whoami` | grep –q john
&& echo “I’m Alex”
 % echo `whoami` | grep -q john
|| echo "I'm Alex"
I'm Alex
Shell Aliases
 alias
and unalias for maintaining frequently
used commands



Display all alias:
% alias
h
history | more
l
ls –l
d
date
Set an alias: % alias l ‘ls –l’
Release an alias: % unalias l
Recommended Reading
 Chapter
1, section 1.6 (excluding section
1.6.7)
 Chapter 9, sections 9.4, 9.7, 9.8, 9.11,
9.12
 Don’t worry if any examples use syntax we
haven’t seen yet (like for variable arrays).
Just make sure you understand the main
ideas.