CONTENTS UNIX What is UNIX? What is OS?

CONTENTS
UNIX
• History
• UNIX File System
• Commands to manipulate Files
• Shell
• Editor
Jusub Kim
Art & Technology
1
2
What is UNIX?
What is OS?
• A Computer Operating System
• Developed in early ‘70s by Ken Thompson
• Unix, Linux, Mac OS X, Windows, iOS,
Android...
• Software that manages computer hardware
and Dennis Ritchie
resources and provide common services for
application software
• Originally written in assembler, later
rewritten in C (for greater portability) which
is invented by Ritchie
• Tasks:
- scheduling of multiple programs
- memory management
- access to hardware
- etc.
3
source: wikipedia
4
Unix Philosophy
File System
• Make each program do one thing well
• The output of every program become the
•
•
•
“Files have places and processes have life” - Kaare Christian
Root directory : “/”
• Everything is seen
•
•
•
•
Shortcuts:
“.” : current directory, “..” : parent directory, “~” : home directory
ex) > cd ..
<= move to the parent directory
input of another program to combine simple
tools to perform complex tasks
as a file
All files are flat ( just a sequence of bytes), but file system is hierarchical
Organized as a tree, each node is a directory where each directory can
contain other files or another directories or both.
Case sensitive
Files can be referenced either by relative reference or by absolute
reference.
ex) > cd /usr/jusub <= change directory to /usr/jusub
> cd jusub
<= change directory to jusub ( jusub directory has
to be under current directory)
source: www.math.yorku.ca/~milver
5
6
Shell
Shell
• When you log in, your shell (a process) starts in
• A Shell is a Command Interpreter (a
order to allow you to communicate with OS
program) that turns text to actions
- a shell reads a line that you type in, finds
the program, and runs it
ex) > ls => the shell reads the line ‘ls’,
finds the ‘ls’ program, and runs it.
• Shell provides you with a bunch of commands
that let you access the computer system
- ls : list file names
- pwd : print working directory name
- cd : change working directory
- top : display sorted information about processes
- man : print manual on a command (eg., man ls)
• Popular shells:
- sh : Bourne Shell
- csh : C Shell
- bash : Bourne-Again Shell
7
8
ls command
cd command
• display the names of files
• options
• change the current working directory
• ex) > cd /usr/jusub (absolute path)
> ls -l : list in a long format with more info
> ls -a : list also hidden files (ones of which
filename starting with “.”)
> ls -t : list files sorted by modification time
> ls -r : list files in a reverse order
> cd jusub
> cd ..
> cd
(relative path)
( move to parent directory)
( move to home directory)
9
cp commands
•
10
rm command
• delete files
Copy files (make clones)
usage: cp [options] source dest
- source: the name of the file you want to copy
- dest: the name of the new file
Examples)
cp /usr/jusub/my.pdf /usr/tiara/my.pdf
cp /usr/jusub/my.pdf /usr/tiara/mm.pdf
cp /usr/jusub/my.pdf ./mm.pdf (copy to current directory)
cp /usr/jusub/my.pdf ../mm.pdf (copy to parent directory)
cp /usr/jusub/my.pdf /usr/tiara/ (copy with the same name)
cp my.pdf ../ (copy to parent directory with the same name)
cp -r /usr/jusub/* /usr/tiara/ ( recursively copy everything
under jusub directory into /usr/tiara/, r: recursively, *: all)
ex) >rm my.pdf
>rm -r /usr/jusub ( recursively remove
everything under jusub directory)
11
12
mv command
•
•
•
•
•
•
• move files
ex) > mv /usr/jusub/my.pdf /usr/tiara
(original one is moved to a new place)
> mv /usr/jusub/my.pdf ./ (move to the
current working directory)
> mv my.pdf your.pdf (rename it if same
directory)
mkdir : make directory
rmdir : remove directory
touch : change file timestamp
sort : sort the input file
cat : display the contents of files
wc : count the number of lines, words, letters
* wildcards : matches anything
ex) > ls tiara* => it will print all files of which
names start with tiara
13
File attributes
14
Input/Output Redirection
• Each file has some attributes
- permissions, owners, size, access time
a file or
the output of another program
standard input (STDIN)
ex) keyboard
Program
• Permission bits (user, group, others) :
rwx rwx rwx (r: read, w:write, x: execute)
ex) rw- : possible to read and write
r-- : possible to read only
rwx : possible to read, write, and execute
rw-r--r-- : user can read and write, a member of group can
only read, others can only read
standard output
(STDOUT)
ex) screen
15
=> Or you can
redirect them to
a file or
another program
standard error
(STDERR)
ex) screen
Program
a file or
the input of another program
16
Redirection to a file
Redirection to another program
• ‘>‘
• ‘|’
: redirect output to a file
ex) ls > f (store the output of ‘ls’ into a file
‘f’)
redirect output to another program
ex) cat f.txt | wc (redirect the output of the
program ‘cat’ to another program ‘wc’)
cat f.txt | grep “snsd” > out ( redirect
the output of ‘grep‘ to a file ‘out’)
• ‘<‘ : redirect input to a file
ex) sort < f (feed the file ‘f’ to the ‘sort’
program)
• A Pipe ‘|‘ is a holder for a stream of data and
can be used to hold the output of one
program and feed it to the input of another
Program
stdout
Program
stdin
17
18
Shell variables
•
ex) HOME : home directory of user
PATH : list of places to look for commands
•
•
echo $VAR : displaying shell variables
•
Job Control
export PATH=/usr/bin:/usr/jusub/bin/
(if your shell is bash)
or setenv PATH /usr/bin/:/usr/jusub/bin/
(if your shell is csh)
•
kill : Kill a job
Ctrl-C : kill the foreground job
ex) kill 1876 (1876: process id)
•
Ctrl-Z : suspend the foreground job (stop the job, not kill
it)
fg : resume the suspended foreground jobs
•
‘&’ : run the job in the background ( don’t need to wait for
the job to complete, also can run multiple jobs in the
background)
‘jobs’ program shows background processes
ex) sort my.txt & (run sorting in background so that you
can do other things at the same time)
•
ps or top : check the status of current processes
You can set the variables in the startup file
( ~/.bashrc (in bash) or ~/.cshrc (in csh) ) so that you
don’t have to do it every time you log in.
19
20
Editor
Vi editor
• vi : text editor created for Unix
• Text editor: enter and modify text
• emacs : another text editor created for
• Text Editor Features
cf) Word processor: enter, modify, and format
text
- still most widely used editor in Unix world
- vim : improved version of vi
- Open a file (new or existing)
- Enter text
- Search & Replace
- Copy, Cut, and Paste
- Undo and Redo
- Save text
Unix
- more extensible
21
22
Vi
• Has three modes
Command
mode
:
Vi
• Lastline mode (‘:’)
i, o
ESC
- used for file-level commands (open, save,
quit, help, execute other program, etc)
Input
mode
ex)
:q =>quit
:w <filename> => write to a file
:wq => write and quit
:e => open
:help <command>
:12 => go to the line number 12
Return
Last-line
mode
23
24
Vi
•
Vi
Command mode
- Used for editing commands such as insertion, deletion,
copy, paste, undo, etc
- Essential commands
i : convert to insert mode
o : open line below cursor and convert to insert mode
dd : delete a line
3dd : delete 3 lines
u : undo a command
Ctrl-r : redo a command
v : start visual mode
y : copy (yank) to the clipboard
p : paste in the clipboard (deleted or copied ones)
gg : go to the first line of file
shift-g : go to the last line of file
•
25
Homework #1
•
•
•
•
•
•
•
Install Linux
Make a new directory
Run vim
Type in page 1 of the textbook
Copy the first paragraph and Paste it back to the bottom
Replace ‘ C ’ with ‘Awesome-C’
Save it and Send it to TA by email attachment (use
firefox)
27
Search & Replace
- Search (command mode)
/snsd : search snsd
n : repeat search
N : repeat search in the other direction
% : search for matching parenthesis on ‘()‘, ‘[]‘,or ‘{}‘
- Search & Replace (lastline mode)
:%s/jusub/taeyeon => change only first occurrence in the
file
:%s/jusub/taeyeon/g => change all occurrences in the file
:%s/jusub/taeyeon/gc => change all occurrences in the
file with confirmation
26