Introduction to UNIX
2
Unix File SystemDirectory OrganizationHierarchy of Files & Directories
usr var
file1, file2,file3, file4
jan
file1, file2,file3, file4
feb
data m box, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
3
Unix File SystemIdentifying Files Using Full Path Names
usr var
file1, file2,file3 , file4
jan
file1, file2,file3, file4
feb
data m box, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
/home/rdefe/mbox
Full Path Names Always begin with /
4
Unix File System Identifying Files using Relative Path Names
Dependent on Your Location in the File System
usr var
file1, file2,file3 , file4
jan
file1, file2,file3, file4
feb
data m box, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
data/jan/file1
Current Working Directory
mbox
5
Directory Manipulation
mkdir [directory1] [directory2] … Create New Directories
$ mkdir mail$
file1, file2,file3, file4
jan
file1, file2,file3, file4
feb
data m ail m box, letter,prog1, prog2
rdefe
Directories created based on your current location in the File System
6
Directory Manipulationcd [DirectoryName]
Change Directory
usr var
file1, file2,file3, file4
jan
file1, file2,file3, file4
feb
data m box, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
$ cd data
$ cd /home/djones
7
Directory Manipulationpwd
Display Present Working Directory
usr var
file1, file2,file3, file4
jan
file1, file2,file3, file4
feb
data m box, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
$ pwd/home/rdefe$
8
Directory Manipulation
rmdir [directory1] [directory2] … Remove Empty Directories
file1, file2,file3, file4
jan
file1, file2,file3, file4
feb
data m ail m box, letter,prog1, prog2
rdefe$ rmdir mail$ X
9
Directory Manipulationrm [-ri] [directory1] [directory2] …
-r Remove Directories -i Interactive
file1, file2,file3, file4
jan
file1, file2,file3, file4
feb
data m ail m box, letter,prog1, prog2
rdefe
X$ rm -r data$
Caution: Will delete directories that have files & subdirectories
$ rm -i mboxmbox? y$
10
Unix Commandscp [-i] [SourceFile] [DestinationFile]
Copy A File -i Interactive
$ cp jan jan.copy$
$ cp /home/jsmith/data mydata$
$ cp /home/jsmith/d5 /tmp/a5$
11
Unix Commands
cp [-i] [File1] [File2] … [FileN] [Directory] Copy N number of files to a directory$ cp jan feb mar data
$
$ cp p1 /tmp/p2 p3 data$
$ cp jan data$Note: data is an existing
directory
12
Unix Commandsmv [-i] [SourceFile] [DestinationFile]
Rename a File Move a File to a Different Directory
$ mv jan feb$
$ mv jan /tmp/oldjan$
$ mv jan /tmp$
13
Unix Commands
mv [-i] [File1] [File2] … [FileN] [Directory] Move N number of files to a directory
$ mv jan feb mar data$
$ mv p1 /tmp/p2 p3 data$
$ mv jan data/oldjan$
Note: data is an existing directory
14
Unix Commandsln [-i] [SourceFile] [DestinationFile]
Create multiple names that all reference the same data
$ ls jan$ ln jan feb$ ls jan feb$
1 $ vi feb$
2
$ cat jan$ cat feb$
3
Changes made to feb are reflected in jan feb and jan both reference the same data Changes made to one will be reflected in the other
15
Unix CommandsHow Links Work
$ ls jan$ ln jan feb$ ln feb mar$ ls jan feb mar$
jan
feb
mar
$ rm jan Use the rm command to remove links
X
16
Unix Commands
feb
mar
1582
How Links Work ls -i to display inode numbers ls -l to display number of links
$ ls -l-rw-r--r-- 2 rdefe unix 53 Sep 12 21:46 feb-rw-r--r-- 2 rdefe unix 53 Sep 12 21:46 mar$
$ ls -i1582 feb1582 mar$
17
Unix Commandsln [-i] [SourceFile] [DestinationFile]
Create multiple names that all reference the same data
$ ls jan$ ln jan feb$ ls jan feb$
1 $ vi feb$2
$ cat jan$ cat feb$
3
18
Unix Commands
ln [-i] [File1] [File2] … [FileN] [Directory] Create N number of linked files in a
directory$ ln jan feb mar data$
$ ln p1 /tmp/p2 p3 data$
$ ln jan data/oldjan$
Note: data is an existing directory
19
Examplescp, mv, ln
$ ls -li1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1
$ cp letter1 letter1.cp$ ls -li1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter11509 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1.cp
$ mv letter1.cp doc$ ls -li1509 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 doc1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1
20
Examplescp, mv, ln
$ ls -li1509 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 doc1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1
$ ln doc a$ ln a b$ ls -li1509 -rw-r--r-- 3 rdefe unix 1696 Oct 29 21:39 a1509 -rw-r--r-- 3 rdefe unix 1696 Oct 29 21:39 b1509 -rw-r--r-- 3 rdefe unix 1696 Oct 29 21:39 doc1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1
21
Examplescp, mv, lncp Makes new copies of filesmv Moves and/or renames filesln Create additional file names
22
Unix Commandsgrep [string] [File1] [File2] …
Find a char string contained in a file Global Regular Expression Processor
$ cat datathis is a sample filethat we'll use to test theunix grep command.unix commands can some interestingnames
$ grep unix dataunix grep command.unix commands can some interesting
23
Unix Commands
grep [string] [File1] [File2] …
$ grep unix data wpdata:unix grep command.data:unix commands can some interestingwp:created with the unix command vi.wp:this unix editor can$$ grep "unix comm" data wpdata:unix commands can some interestingwp:created with the unix command vi.$
24
Meta CharactersSpecial Characters with Special Meaning
/ \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & #Used to Save time
$ ls a*z
$ ls a*9*z
$ cp a* data
$ more data*
$ rm junk*
$ ls a*
* Match zero or more characters
25
Meta CharactersSpecial Characters with Special Meaning
/ \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & #Used to Save time
$ ls a?z
$ ls a??
$ cp a? data
$ more data?
$ rm junk?
$ ls a?
? Match any single character
26
Meta CharactersSpecial Characters with Special Meaning
/ \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & #Used to Save time
[ ] Match any single character in the list
$ ls a[abc] aa, ab, ac
$ ls a[abc,0-9]z aaz, a9z, acz
$ ls a[adz,A-Z] aa, aZ, aA
27
Meta CharactersCombining Meta Charaters
$ ls a?[123] aa1, au3, az2
$ ls data*[89][12] data81, data91, data82
$ ls mail?[xy]* mailxx1234, mail8y
28
Meta Characters . Current Directory .. Parent Directory ~ Home Directory
usr var
file1, file2 ,file3, file4
jan
file1, file2 ,file3, file4
feb
data m box, le tter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
Current DirectoryParent Directory
29
Meta CharactersCopy /home/djones/info to rdefe
usr var
file1, file2,file3, file4
jan
file1, file2,file3, file4
feb
data m box, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
$ cp /home/djones/info .$$
pwd
$ cp ../djones/info .$
30
Meta CharactersCopy /home/rdefe/mbox to data
usr var
file1 , file2,file3 , file4
jan
file1 , file2,file3, file4
feb
data m box, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
$ cp ../mbox tempmbox$$
pwd
$ cp ../mbox .$
31
Meta CharactersOther Examples...
usr var
file1 , file2,file3 , file4
jan
file1 , file2,file3, file4
feb
data m box, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/$ cp ../*.f .$
pwd
$ cp ../../rdefe/mbox .$
32
…Even More RedirectionOutput Normally Displayed to the
Screen Can Be Redirected to a File
$ pr prog1 >> prog1.pr$
If the file prog1.pr exists append the output of the pr command to prog1.pr
Note: Nothing is displayed to the screen
If the file prog1.pr does not exist send the output of the pr command to a new file prog1.pr
33
…Even More RedirectionUnixCommand | tee FileName
Display Screen Output and Redirect to a File
$ who | tee whoisonrdefe ttyp0 Aug 23 9:07 (dana)jsmith ttyp2 Aug 23 22:30 (dana)rdefe ttyp3 Aug 23 13:53 (dana)$
Output saved to the file whoison
Output displayed to the screen
34
Shell ScriptsText File that Contains Unix CommandsCorrect Command Syntax
Options Arguments
One Command per Line$ cat myshellwhodate$
$ rm * -i
$ cat myshell2who;date$
x