www.redoubt.com

Download Report

Transcript www.redoubt.com

System Administration
Introduction 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.
[email protected]
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



default shell: Bourne shell


to open a tty:
right-click on desktop, choose “Open Terminal”
echo



text only
24 lines, 80 columns
print text to the tty
try: echo Hello
what shell is running ?
echo $0
Simple commands

bash

compatible with sh, added features:




date


command history with up arrow
editable command line
backspace (vs. delete)
system date and time
who

what users are on the system
Flow Control characters

ctrl-C



ctrl-S / ctrl-Q



stop a running command
try: troff
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


ls


print the working directory
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 (..)


examples:



relative to any preceding directory
./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



rm filename



create an empty file
example: touch myfile.txt
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.txt
mv myfile.txt ..
mv myfile.txt /usr/bin/students.txt
File Commands - 4


cat file
cat file1 file2 ...


more file


display the contents of the file(s) on the screen
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
file*.txt
file?.txt
*file.txt
file1.txt
*=1
?=1
no match
file10.txt
* = 10
no match
no match
file.txt
* = [nothing]
no match
no match
file.tmp.txt
* = .tmp
no match
no match
file.bmp
no match
no match
no match
oldfile.txt
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 *.txt
will list all files that end in .txt
mv *.txt /export/home/alingelb
will move all files ending in .txt to the directory
/export/home/alingelb
More useful (text) file
commands 1



grep pattern file
search for the pattern in the file, and output
the line(s) containing the pattern
sort file
sort the lines of the file in alphabetical order
head file
display the first 10 lines of the file
More useful (text) file
commands 2



tail file
display the last 10 lines of the file
wc file
count the number of lines, words, and
characters in the file
diff file1 file2
display 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 -l
will display file permissions, along with the
group and owner

example:
$ ls -l
total 3
-rwxr-xr-x
drwxr-xr-x
-rw-------
2 alingelb staff
3 alingelb staff
1 alingelb staff
512 Nov 2 10:38 Desktop
512 Oct 16 11:15 Documents
40 Nov 2 12:13 foonly.txt
File permissions 3

chmod ### file
is 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


command < file


sends the standard output of the command to a 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


command 2> /dev/null



it discards all output
discards all error output
example:
touch testfile
chmod 000 testfile
cat 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\ -f1
gives list of users, still with duplicates
who | cut -d\ -f1 | sort
sorts list, putting duplicates together
who | cut -d\ -f1 | sort | uniq
removes duplicates; this is the desired list
Review

Concepts


Flow control


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


ctrl-C, ctrl-S, ctrl-Q, ctrl-D
Commands:


Unix history, multiuser/timesharing, kernel, shell,
man pages, file permissions
*, ?
I/O management

>, >>, <, |, 2>