Intro to Unix
Agenda – Activity 1
Introduction to the Unix File System
Unix file system
File Types
Directory File Paths
Access Permissions
Demonstration of file system
Agenda – Activity 2
UNIX Commands
Navigating the File System:
pwd, ls, touch, cd
Demonstration of pwd, ls, touch, and cd
Working With Files:
cp, mv, rm, mkdir, rmdir
Demonstration of cp, mv, rm, mkdir, rmdir
Agenda – Activity 2 Continued
More UNIX Commands
File Permissions:
id, umask, chmod
Demonstration of id, umask, chmod
In class assignment
Break (10 minutes)
Agenda – Activity 3
vi Editor
Introduction to the vi editor
Practice with the editor
Preview of next week
Activity 1
The Unix File System
Everything is
a File in Unix
Types of Unix Files
There are Three Types of Files:
Ordinary / Regular Files
Directories
Special Files – Internal representation of a
physical device (keyboard, printer,
terminal)
The Tree-Structured File System
root (/)
bin dev etc lib lost+found sys tmp usr
bin games lib local spool
Another Example
The following directory tree, and files are located under /export/home/smith/comp110
|_[assignment1]
| \_assign1-1.doc
|
|_[assignment2]
| \_assign2-1.doc
|
|_[lab1]
\_[doc]
| \_bubblesort.man
|
|_[report]
| \_lab1.report
|
|_[source]
\_sort.cpp
sort.o
Common Unix Directories
/bin stores basic Unix programs
/dev contains files that represent devices
/etc files for managing the system
/lib contains libraries of programs
/lost+found contains ‘misplaced’ files
/sys contains system source files
/tmp temporary storage
/usr important directory – contains many
things
Unix Directories
Root Directory /
Your Home Directory
/export/home/{userid}
$HOME variable
Shows your current home directory
print $HOME - display variable setting
Unix Directories
Present Working Directory
Your current location
-or-
Current Directory
Unix Commands: pwd
Use pwd to display the name of your
current working directory
/export/home/morris07/> pwd
Present Working Directory
Absolute Path
Absolute Path
/export/home/morris07/labs
NOTE: These always start with a “/” from
root.
Unix Commands: cd
Use cd to change your working
directory
/export/home/morris07/>
cd {directory name}
Absolute Path
Absolute Path
Relative Path
If your pwd was
/export/home/morris07/
You could do:
cd examples
To move into the examples subdirectory
Relative Path
Relative Path (Shorthand)
Single dot . Your current directory
Double dot .. Your parent directory
cd . Takes you to where already are!
cd .. Takes you to the pwd’s parent
directory.
cd ~ Takes you to your home directory
cd - Takes you to the previous directory
Relative Path
Relative Path
Relative Path
Unix Commands: ls
Use ls to list the contents of a directory
/export/home/morris07/> ls
/export/home/morris07/> ls –l
* (long format)
/export/home/morris07/> ls –la
* (long format, and list all entries including those that
begin with a “.”
Unix Commands: ls
/export/home/morris07/> ls –F
* Flags directories with a “/” and executables with a “*”
Using Wildcards:
* Any string of characters
? Any one character (not space)
[] Match any character in the brackets
Unix Commands: ls
Examples
ls *.c Lists all files ending with ‘.c’
ls file? Lists any file with file and one
character at the end
ls v[6,7]file Lists v6file and v7file
Unix Commands: ls
Unix Commands: ls
Unix Commands: ls
Unix Commands: ls
Using Relative Path in ls
ls -al ..
Lists your parent directory
ls –al ~
Lists your home directory
Activity 2
Unix Commands: touch
Use touch to change a file’s access time and
modification time to the current date
/export/home/morris07/> touch {file name}
NOTE: If the file does not exist, touch will create a new file
Unix Commands: id
Use id to display your userid and groupid
/export/home/morris07/> id
Unix Commands: id
Unix Security
Login name and a password
Encryption on important files
Access permission
Encryption of files
Text page 334
crypt
Description will make more sense after
next week
Requires a key – do not forget the key
Access Permissions
Ordinary File
Read: you can read from the file
Write: you can write to the file
Execute: you can execute the file key
Directory Permissions
Directory
Read
You can read the directory
Write
You can create, move, copy or remove
directory contents
Execute:
You can search the directory
How Permissions are Managed
There are three Permission Groups:
Owner:
Owner’s Group:
Everyone Else/Other:
Permissions
-rwxrwxrwx 1 morris07 student 512 Jan 12 14:07 file.exe
-rw-rw- rw- 1 morris07 student 812 Jan 12 14:22 file.name
drw-rw-rw- 1 morris07 student 812 Jan 12 14:22 labs
-----------------------------------------------------------------------------------------
r read permission
w write permission
x execute permission
- permission not granted
-----------------------------------------------------------------------------------------
owner group everybody At the far left, 1’st character
rwx rwx rwx shows type of file. “-” ordinary
“d” is directory
Unix Commands: chmod
Use chmod to change the file-access
permissions on an existing file
> chmod {mode} {file}
> chmod 777 file.name
Numeric Value of Permissions
FILE MODE, or MODE
read permission = 4
write permission = 2
execute permission = 1
no permission = 0
To calculate the proper permissions you want to assign, simply add
the numbers together:
read + write + execute = 4 + 2 + 1 = 7
read + write = 4+2=6
…
chmod: Calculating the Mode
Number Meaning
400 Owner has read permission
200 Owner has write permission
100 Owner has execute permission
040 Group has read permission
020 Group has write permission
010 Group has execute permission
004 Everyone else has read permission
002 Everyone else has write permission
001 Everyone else has execute permission
--------
777
Numeric Value of Permissions
chmod 777 lab1
Allows rwx to everyone!
chmod 755 lab1
Allows rwx to user, and rx to group/others.
Or to deny group and others rwx permissions:
chmod 700 lab1
Symbolic-Mode File Permissions
Letters represent the groups and permissions:
u = User, g = Group, o = Others
+ or – To add or remove a permission from:
r = Read, w = Write, x = Execute
chmod ugo+rwx lab1
Allows rwx to everyone!
Or to deny group and others rwx permissions:
chmod go-rwx lab1
Unix Commands: chmod
Unix Commands: chmod
Unix Commands: chmod
Unix Commands: umask
Use umask to display or set the current
value of the file-creation mask (default
permissions, set in .profile)
/export/home/morris07/> umask
/export/home/morris07/> umask 022
Unix Commands: umask
umask: Calculating the umask
File Type Beginning File Mode
Non-executable files 666
Executable files 777
Directories 777
From this initial mode, Unix subtracts the value of the
umask.
For example, if I want a file permission of 644 on a
regular file, the umask would need to be 022.
Unix Commands: umask
Unix Commands: umask
Unix Commands: cp
Use cp to copy the contents of one file to
another file
cp {source file} {destination file}
> cp file1 file2
Copies the file to another file name
cp file1 ~/newdir/junk1
Copies the file1 to your home directory in the
directory newdir and renames the file to junk1
(newdir must already exist)
Unix Commands: mv
Use mv to move files to another directory
or to a new name in the current
directory
> mv {source file} {destination file}
> mv file1 file2
* Moves the file to another file name
> mv file1 newdir
* Moves the file to another directory
Unix Commands: rm
Use rm to remove files
> rm {file(s)}
> rm file1 file2
> rm –i file1 - Prompts for confirmation before
removing the file
NOTE: You have to either be the owner of the file or have
write permissions to the directory containing the file!
Unix Commands: mkdir
Use mkdir to make a directory
> mkdir {directory}
> mkdir newdir
> mkdir –p newdir1/newdir2/newdir3
NOTE: You have to either be the owner of the file or
have write permissions to the directory containing the
new directory!
Unix Commands: rmdir
Use rmdir to remove a directory
> rmdir {directory}
> rmdir newdir
> rmdir –p newdir1/newdir2/newdir3
only works if the directories become empty
Activity 3
Unix File Editors
vi (pronounced “vee-eye”) is a visual
editor that was created by Bill Joy.
vi is a “right-handed” editor
Other Unix editors: pico, emacs
The vi Editor
Use vi to edit files
> vi {file}
NOTE: If the file does not already exist, vi will create it
for you.
The vi Editor: Modes
vi has two different modes:
Command Mode
In Command Mode, the characters you type are
interpreted as commands.
Input Mode
In Input Mode, everything you type is inserted
into the editing buffer.
The vi Editor: Modes
vi starts in Command Mode by default
Type <Esc> to change from Input Mode to Command
Mode
Hint: If you forget which mode you are in, hit the
<Esc> key twice to get to Command Mode.
Hint: :set showmode will display the input mode in the
lower right hand corner of the screen.
The vi Editor: Inserting Data
From Command Mode:
i changes to Input Mode: insert
before current position
a changes to Input Mode: insert
after current position
I changes to Input Mode: insert at
start of current line
The vi Editor: Inserting Data
From Command Mode:
A changes to Input Mode: insert at
end of current line
o changes to Input Mode: open
below current line
O changes to Input Mode: open
above current line
The vi Editor: Saving and Exiting
In Command Mode (colon commands):
:w writes data to the file (saves changes)
:wq writes data to the file and quits
:w filename writes buffer to the named file
:q quits
:q! quits without saving
ZZ quits and saves
The vi Editor: Moving the Cursor
In Command Mode:
h, move cursor one position to the left
j , move cursor one position down
k , move cursor one position up
l , move cursor one position to the
right
The vi Editor: Moving the Cursor
:set number
:set nonumber
:n <Return> Jump to line number n
nG Jump to line number n
1G Jump to first line
G Jump to the last line
The vi Editor: Moving the Cursor
w move cursor forward to first
character of next word
<Return> Move cursor to beginning of
next line
^F Move down one screenful
^B Move up one screenful
The vi Editor: Moving the Cursor
0 move cursor to beginning of current line
$ move cursor to end of current line
^ move cursor to first non-space/tab in the
current line
- move cursor to beginning of previous line
+ move cursor to beginning of next line
The vi Editor: Deleting Data
x delete character at cursor
X delete character to left of cursor
D delete from cursor to end of line
dw delete one word
dd delete the entire current line
The vi Editor: Replacing Data
r replace a single character at the cursor
ra
* Replaces the current character with “a”
R replace characters by typing over
Rnew stuff
* Replaces the text at cursor with “new stuff”
cw replace the word by typing over
The vi Editor: Copy and Paste
yy copies (yanks) the whole line to the buffer
p pastes data, insert before/above cursor
P pastes data, insert after/below cursor
3yy
* Copies (yanks) 3 lines
p
* would paste the previously yanked lines at the current
cursor position!
The vi Editor: Cut and Paste
dd deletes a line (puts it in the buffer)
p pastes data, after/below cursor
dd
*Cuts
p
*Pastes
The vi Editor: Searching
/{pattern} searches forwards for pattern in file
/<Return> repeats forward search for pattern
?{pattern} searches backwards for pattern in file
?<Return> repeats backward search for pattern
n repeats search in same direction
N repeats search in opposite direction
The vi Editor: Replacing
:s/{pattern}/{replace} Replaces a pattern
*Only works on the first occurrence
:s/{pattern}/{replace}/g
*Works on all occurrences in the current line
:%s/{pattern}/{replace}/g
*Works on all occurrences in the file
:line#1,line#2,s/{pattern}/{replace}
*Works on all occurrences between the line numbers
The vi Editor: Undo and Repeat
u undo last command
U restores the current line
. Repeat last command
The vi Editor: Bonus
J Join Lines
:!{command}<return> Pause vi, execute
specified shell command.
:r !{command}<return> Insert output of
command after current line
^L Redisplay the current screen.
The vi Editor: Bonus
Here is a helpful hint on controlling the length of lines in “vi”
One-way to do this is to press <Enter> at the end of each line.
Pressing <Enter> will insert a newline character, which marks the
end of the line.
You can automatically tell “vi” to let it break a line into two when it
gets within “n” characters of the right margin. To have “vi” break
your lines automatically when they get within 6 characters of the
right margin, use the following command in “vi” (command mode):
:set wrapmargin=6
or
:set wm=6
The vi Editor: Bonus
This is especially helpful when you are typing a
long paragraphs and don’t want it to be one
continuous line.
If you don’t want to have to enter this “vi”
command every time you enter “vi”, you can
put it in the .exrc file. This file, created by you,
belongs in your home directory. “vi” will read
and execute any commands that it finds in this
file upon startup.
In the .exrc file you don’t need to start any
commands with a colon (:).
The vi Editor: Bonus
~ (tilde) changes the case of the
current position