system administration introduction to unix session 2 – fri 02 nov 2007 reference: chapter 1, the...

Post on 01-Jan-2016

222 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

System AdministrationIntroduction to Unix

Session 2 – Fri 02 Nov 2007

Reference: chapter 1,

The Unix Programming Environment, Kernighan & Pike, ISBN 0-13-937681-X

Albert Lingelbach, Jr.alingelb@yahoo.com

History of Unix

Begun in 1969 at Bell Labs Timesharing / Multiuser

One computer, many terminals Programming Typesetting Group Communication

Architecture

Kernel Windowing System

Gnome Shell Programs

The shell environment

tty (teletype) / terminal text only 24 lines, 80 columns

default shell: Bourne shell to open a tty:

right-click on desktop, choose “Open Terminal” echo

print text to the tty try: echo Hello

what shell is running ?echo $0

Simple commands

bash compatible with sh, added features:

command history with up arrow editable command line backspace (vs. delete)

date system date and time

who what users are on the system

Flow Control characters

ctrl-C stop a running command try: troff

ctrl-S / ctrl-Q pause/unpause output try: countdown 1000

end input ctrl-D

Command Arguments / Parameters

mis-typed command gold: not found

command arguments / parameters try: who am i try: gold coins

User Communication

news get local system news (written by the system

administrator) try: news

write write messages to another user on the system;

like IM but local try:

pair off using who, find userid of your partner write userid

(and your partner does same)exchange messages

Manual Pages

You have seen the following commands: date who news write sh bash

How to find more about them ? command -?

often returns a short command synopsis man command

returns the complete manual page reference for the command

Files

As in any computer system, data is stored in files

Most Unix files are (ASCII) text Many Unix commands manipulate text files

File Naming

case sensitive spaces are problematic dot and underscore are useful separators

File Commands – 1

current (“working”) directory pwd

print the working directory ls

list the files in the working directory cd path

change the working directory

File Paths 1

filenames without a path default to the current directory example: myfile.txt

/ is the root or top of the file system tree a file path contains directories

separated by “/”(not “\” as in DOS/windows) example: /home/export/staff/alingelb/file.txt

File Paths 2

relative paths (not starting with “/”) start at the current directory

current directory (.) parent directory (..)

relative to any preceding directory examples:

./file.txt (the same as file.txt)

../../student/kate/file.txt in general, any command that takes a file as

an argument, will accept a path + file (path/file)

File Commands - 2

touch filename create an empty file example: touch myfile.txt

rm filename delete a file (forever – be careful!) example: rm myfile.txt

cp original_file new_file copy a file example: cp myfile.txt copy_of_myfile.txt

File Commands - 3

mv original_file new_filename mv original_file new_location

new_location must already exist file keeps same name

mv original_file new_location/new_filename move a file examples: mv myfile.txt newname.txtmv myfile.txt ..mv myfile.txt /usr/bin/students.txt

File Commands - 4

cat file cat file1 file2 ...

display the contents of the file(s) on the screen more file

display the contents of the file on the screen, on screenful at a time (press space for the next screen, return to advance one line)

gedit file edit the file in the gnome graphical editor

Directory Commands

mkdir dirname mkdir path/dirname

create the directory rmdir dirname

remove the directory (it must be empty)

Shell filename wildcards 1

it can be useful to match filenames by pattern: * matches any set of characters (or no

characters) ? matches one character

examples: *file*

matches any filename containing “file” *.txt

matches any filename that ends in “.txt” this*

matches any filename that starts with “this”

Shell filename 2 wildcards

More examples:

Sample file names

pattern file1.txt file10.txt file.txt file.tmp.txt file.bmp oldfile.txtfile*.txt * = 1 * = 10 * = [nothing] * = .tmp no match no matchfile?.txt ? = 1 no match no match no match no match no match*file.txt no match no match no match no match no match * = old

Shell filename wildcards 3

More examples: cat chapter*.txt

will output to the screen all files that start in chapter and end in .txt

ls *.txtwill list all files that end in .txt

mv *.txt /export/home/alingelbwill move all files ending in .txt to the directory /export/home/alingelb

More useful (text) file commands 1

grep pattern filesearch for the pattern in the file, and output the line(s) containing the pattern

sort filesort the lines of the file in alphabetical order

head filedisplay the first 10 lines of the file

More useful (text) file commands 2

tail filedisplay the last 10 lines of the file

wc filecount the number of lines, words, and characters in the file

diff file1 file2display the differences between the two files

File permissions 1

Every file has read, write, and execute permissions (RWX)

These are set by the owner, for the owner, the group, and everyone else; so there are 9 permissions total (plus a few special ones to be discussed later).

File permissions 2

ls -lwill display file permissions, along with the group and owner example:

$ ls -ltotal 3-rwxr-xr-x 2 alingelb staff 512 Nov 2 10:38 Desktopdrwxr-xr-x 3 alingelb staff 512 Oct 16 11:15 Documents-rw------- 1 alingelb staff 40 Nov 2 12:13 foonly.txt

File permissions 3

chmod ### fileis used to change file permissions the first # is owner permissions the second # is group permissions the third # is everyone else permissions the # is composed of

(add the numbers): 4 for read 2 for write 1 for execute

File permissions 4

chmod example: chmod 744 file

sets owner to read/write/execute sets group to read sets everyone else to read

Shell theory

The shell is a command interpreter It interfaces between the user and programs

and the kernel It has its own syntax In addition to providing access to programs

and to the kernel, the shell has some powerful features of its own, including wildcards I/O redirection scripting

I/O management 1

Every process has 3 channels of information: “standard” input “standard” output error output

These can be controlled by the shell: command > file

sends the standard output of the command to a file command < file

sends the contents of a file to the standard input of the command

command >> file appends the standard output of the command to the

end of a file

std I/O management

Examples ls > listing.txt echo “this is a test” > test.txt echo “the test continues” >> test.txt echo “6 + 6” > math.txt bc < math.txt

more I/O management

More I/O redirection There is a special empty file/dev/null

it discards all output command 2> /dev/null

discards all error output example:touch testfilechmod 000 testfilecat testfile 2> /dev/null

command1 | command2 send the output of command1 as the input of

command 2 example: ls | wc

I/O management: pipe

Advanced example of pipe wanted: list of users on system. build it

incrementally using pipe. who

gives list of users, but there are duplicates (because user appears once for each session);also, who gives too much information

using cut to remove extraneous information:who | cut -d\ -f1gives list of users, still with duplicates

who | cut -d\ -f1 | sortsorts list, putting duplicates together

who | cut -d\ -f1 | sort | uniqremoves duplicates; this is the desired list

Review

Concepts Unix history, multiuser/timesharing, kernel, shell,

man pages, file permissions Flow control

ctrl-C, ctrl-S, ctrl-Q, ctrl-D Commands:

echo, bash, date, who, pwd, ls, cd, touch, rm, cp, mv, cat, more, gedit, mkdir, rmdir, grep, sort, head, tail, wc, diff, chmod, bc

File paths & wildcards *, ?

I/O management >, >>, <, |, 2>

top related