UNIX BOOT CAMP Intermediate - University of Saskatchewan

Download Report

Transcript UNIX BOOT CAMP Intermediate - University of Saskatchewan

UNIX BOOT CAMP
Intermediate
Department of Computer Science
University of Saskatchewan
Autumn 2005
Why are we here?
Intended Audience
 UNIX beginners
 Command-line beginners
 Those with some, but limited experience.
Desired outcomes
 Take the edge off of something new.
 Make your computer time more productive.
 Understand more about under-the-covers stuff
that goes on when using a computer
 Extra confidence in 214, 332 and other courses.
Why are the others here?
There's always someone who knows more
than you.
 Share in community knowledge building
 Teamwork
 No one knows everything.
 Discover what's worth knowing and what will
help you succeed.
 Read the rest on a need to know basis.
 Know where to look.
Introduction
UNIX, Linux, *BSD form a family of Operating
Systems that conform to an interface known as
POSIX.
 Now, instead of writing for 4 different systems a
person can write for POSIX.
 Can be assured that ls on BSD has the same
functionality as ls on Linux, improving portability.
 Sometimes people add extra functionality.
Most modern operating systems use ideas
from UNIX.
 OSX uses many BSD tools.
 Windows uses the BSD TCP/IP stack.
Some bits of UNIX Philosophy
Programs should be small and specific.
 A well written program will do one or two things
and do them very well. It’s common to connect
many programs together at once to get complex
behaviour.
Everything is a file.
 Files, sockets, pipes and hardware devices can all
be accessed in a similar way.
Keep It Simple
 Binary file formats are avoided because they
require tools to modify. Text files are preferred.
Logging In/Bash Shell
Prompt screen
 Username (NSID) and password
 Starts a Window manager or shell
KDE is the default window manager
 Has all of the nice GUI things people like.
bash is the default shell for Linux.
 Sometimes csh ( C-Shell ) is installed.
 Interprets commands, executes them, and returns
to the prompt.
 Has all sorts of nice features. Job control, support
for piping, I/O redirection, etc.
Commands
Should look something like this.
 [abc123@stealth10:~]
General Format of Commands
 ssh stealth10.usask.ca
 ssh is the command name. This program is
used for getting a shell on remote hosts.
 stealth10.usask.ca is the destination host.
 cp foo newfoo
 cp is the command name.
 foo is the source file.
 newfoo is the destination file.
More on Commands
Options
 Some command options have arguments
associated with them, but not always.
 ssh -l abc123 -C stealth10.usask.ca
 ssh is the command name.
 -l specifies a username that’s used to log in.
 The argument to -l is “abc123”
 The -C option compresses network traffic. It has
no argument associated with it.
Which commands have which options?
 No rhyme or reason.
 This is why the man pages are so important.
File Systems and File System
Interaction
Contents of the disk are files. Files are stored
in directories.
 Files are named sequences of bytes, often (but
not always) stored on a disk device
Directory structure has a root and forms a
hierarchical structure (tree)
 Children nodes off the root can be files or other
directories (sometimes called folders)
 System files, device directories, user files
 /usr, /var, /home, /student, /faculty, /dev
cd - moves between directories
Manipulating Files
Listing contents of a directory
 ls
Copying
 cp
Moving
 mv
Deleting
 rm
Linking
 ln
Links and More
Symbolic links
 More than one way to get to a file
Pathnames
 Absolute path '/'
 Relative path './'
 Parent directory '..', current '.'
Making directories
 mkdir
Removing directories
 rmdir, rm -rf
Patterns in Files and Automatic
Completion
Wildcards in file names
 ? matches a single character
 * matches 0 or more characters
 [ch] matches a 'c' or and 'h'
Completion
 many shells allow the use of a commandcompletion character to complete the
remaining unique part of a command or
filename.
 Saves typing
Everything is a File
File operation metaphor for the entire
system
 File descriptors
 A directory is a file
 A disk is a file
 A console window/terminal is a file
 Standard file descriptors for regular
programs
 stdin, stdout, stderr (more about this later)
Permissions and Groups
 User Groups
 User, group, other
 Allows a specific set of users access to a specific set of
files/directories
 Access permissions
 Access granted according to type of access
 Read, write, execute (for directory, execute is traverse) (Octal
number)
 ls -l (-rw-rw-r-- 1 abc123 222 Sep 3 8:18 file.c)
 permissions for that file are 664
 Super user
 not subject to access restrictions
Other things in Filesystem
These are by convention and can vary
widely.
 /dev - Devices
 /etc - Configuration Files
 /bin - System binary files.
 /sbin - Superuser binary files
 /tmp - Temporary files
 /proc - Kernel information files.
 /lib - Standard programming libraries.
Where to find Files
 /usr - User added files.
 /usr/bin/ - User added commands.
 /usr/include - Standard system header files
 /usr/lib/ - User added libraries.
Help and Details for
commands
 man, apropos
 All commands under unix have a “man” page that
details what the command does and what its
command line options are.
 info
 An alternative to man pages that have links to
related commands.
 --help
 A common command line option to get more
information. It could be also -? or -h.
 Google
 There are excellent pages available on the web.
UNIX gurus love to write.
Locating commands
which
 Used to find which version of a command
will be used when run. Very useful for
figuring out path problems.
locate
 Searches for files locally using a database
that’s updated nightly.
The VI text editor
Two modes, insert and command.
Very lightweight, but powerful.
 Good for editing remotely.
 Available on almost all UNIX systems.
 Extensible and customizable.
Insert mode
 Used for entering text, like you would in
most editors.
 Enter insert mode (i, a, I, A, Insert)
 Exit insert mode (ESC)
VI again
Command mode
 Used for doing powerful text modifications.
 Look for a pattern in the file.
 /pattern
 Do a global search and replace.
 :%s/search/replace/g
 To delete a line, hit “dd”
 To yank (copy) a line, hit “yy”
 To paste, hit “p” or “P”
 Save and Exit
 :wq, :q! (quit without save), :w filename
The VIM text editor
Syntax highlighting
Code completion
Visual selection mode
Split Window Editing using -o or -O
Vimdiff advanced diff viewer/editor
Many neat time saving features
 Edit compressed files in place
 vim file.gz file.bz2
The Emacs text editor
Modeless editor
Always in insert mode
Commands are combinations of ctrl, alt,
or esc (Meta) plus series of keys
 e.g. save is Ctrl-x Ctrl-s ( ^x^s in shorthand)
 quit ^x^c
 M->x goto-line
Has a command window version and a
GUI version
Emacs, continued
A GUI Modeless editor
Keeps multiple files open at one time via
multiple buffers, multiple menus
Has a command window for running
commands within the editor
 search for pattern in files
 automatic compile
Works from menus, can learn keystrokes
from the menu
Emacs, one more time
Programmable set of keys
 .emacs defines personalized keystroke
bindings
 examples (ask a guru, a.k.a. Greg Oster)
 (define-key ctl-x-map "\^E" 'compile)
 (define-key ctl-x-map "\^N" 'next-error)
 (define-key ctl-x-map "\^I" 'isearch-forward)
 (define-key ctl-x-map "\^L" 'goto-line)
 (define-key esc-map "s" 'shell)
 (define-key esc-map "t" 'auto-fill-mode)
 (define-key esc-map "q" 'query-replace)
 (define-key esc-map "r" 'replace-string)
Remote Access
ssh (ssh -X)
 Used for remotely logging in to another UNIX
box. The -X flag lets you run graphical
programs on the remote machine and have
them show up on the local machine.
 scp provides secure copying over a network.
 sftp is a secure ftp implementation using ssh.
rdesktop
 Linux program that accesses Windows
machines that are accepting remote logins.
 The discworld.usask.ca server is available for
student use.
Job Control
Foreground
 When a command that is running in the
foreground control will be returned to the user
when it finishes.
Background
 When a command is running the background,
the user has control and command rolls along
by itself.
^Z (CTRL-Z)
 If a command is running, and you hit ^Z, the
command will be stopped. From here, you
can use fg to bring it to the foreground again.
Job Control II
fg
 Brings the command last put in the
background into the foreground.
bg
 When a command is stopped, you may use
bg to put it in the background.
jobs
 Shows a list of all of the jobs currently
running. Each job has a number associated
with it that you can supply as an argument to
bg and fg.
Job Control III
How to do this in bash.
 cmd
 Runs the command cmd in the foreground.
 cmd1; cmd2
 Runs cmd1 in the foreground and then starts
cmd2 in the foreground when cmd1 is finished.
 cmd1 & cmd2 &
 Runs cmd1 and cmd2 in the background.
Pipes and Redirection
 The really elegant part of UNIX.
 stdin defaults to the keyboard, but if redirection is
used, we can read stdin from elsewhere.
 cmd1 | cmd2
 Runs cmd1 and supplies the output of cmd1 as
input to cmd2.
 This is very useful for chaining commands.
 cmd1 > file
 Writes the output of cmd1 to a file. It will overwrite
the original contents of the file.
 cmd1 >> file
 Appends the output of cmd1 to a file.
 cmd1 < file
 Runs cmd1, the input will be read from a file.