introduction to unix. 2 unix file system zdirectory organization zhierarchy of files &...
TRANSCRIPT
Introduction to UNIX
2
Unix File System
Directory 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 System
Identifying 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$
$ 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 Manipulation
cd [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 data
$ cd /home/djones$ cd /home/djones
7
Directory Manipulation
pwd 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$
$ 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$
$ rmdir mail$
X
9
Directory Manipulation
rm [-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$
$ rm -r data$
Caution: Will delete directories that have files & subdirectories
$ rm -i mboxmbox? y$
$ rm -i mboxmbox? y$
10
Unix Commands
cp [-i] [SourceFile] [DestinationFile] Copy A File -i Interactive
$ cp jan jan.copy$
$ cp jan jan.copy$
$ cp /home/jsmith/data mydata$
$ cp /home/jsmith/data mydata$
$ cp /home/jsmith/d5 /tmp/a5$
$ 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 jan feb mar data$
$ cp p1 /tmp/p2 p3 data$
$ cp p1 /tmp/p2 p3 data$
$ cp jan data$
$ cp jan data$
Note: data is an existing directory
12
Unix Commands
mv [-i] [SourceFile] [DestinationFile] Rename a File Move a File to a Different Directory
$ mv jan feb$
$ mv jan feb$
$ mv jan /tmp/oldjan$
$ mv jan /tmp/oldjan$
$ mv jan /tmp$
$ 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 jan feb mar data$
$ mv p1 /tmp/p2 p3 data$
$ mv p1 /tmp/p2 p3 data$
$ mv jan data/oldjan$
$ mv jan data/oldjan$
Note: data is an existing directory
14
Unix Commands
ln [-i] [SourceFile] [DestinationFile] Create multiple names that all reference
the same data
$ ls jan$ ln jan feb$ ls jan feb$
$ ls jan$ ln jan feb$ ls jan feb$
1 $ vi feb$
$ vi feb$
2
$ cat jan$ cat feb$
$ 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 Commands
How Links Work
$ ls jan$ ln jan feb$ ln feb mar$ ls jan feb mar$
$ ls jan$ ln jan feb$ ln feb mar$ ls jan feb mar$
jan
feb
mar
$ rm jan$ rm janUse 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 -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$
$ ls -i1582 feb1582 mar$
17
Unix Commands
ln [-i] [SourceFile] [DestinationFile] Create multiple names that all reference
the same data
$ ls jan$ ln jan feb$ ls jan feb$
$ ls jan$ ln jan feb$ ls jan feb$
1 $ vi feb$
$ vi feb$2
$ cat jan$ cat feb$
$ 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 jan feb mar data$
$ ln p1 /tmp/p2 p3 data$
$ ln p1 /tmp/p2 p3 data$
$ ln jan data/oldjan$
$ 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
$ 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
$ 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, ln
cp Makes new copies of filesmv Moves and/or renames filesln Create additional file names
22
Unix Commands
grep [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
$ 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.$
$ 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 Characters
Special Characters with Special Meaning / \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & #
Used to Save time
$ ls a*z$ ls a*z
$ ls a*9*z$ ls a*9*z
$ cp a* data$ cp a* data
$ more data*$ more data*
$ rm junk*$ rm junk*
$ ls a*$ ls a*
* Match zero or more characters
25
Meta Characters
Special Characters with Special Meaning / \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & #
Used to Save time
$ ls a?z$ ls a?z
$ ls a??$ ls a??
$ cp a? data$ cp a? data
$ more data?$ more data?
$ rm junk?$ rm junk?
$ ls a?$ ls a?
? Match any single character
26
Meta Characters
Special Characters with Special Meaning / \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & #
Used to Save time
[ ] Match any single character in the list
$ ls a[abc]$ ls a[abc] aa, ab, ac
$ ls a[abc,0-9]z$ ls a[abc,0-9]z aaz, a9z, acz
$ ls a[adz,A-Z]$ ls a[adz,A-Z] aa, aZ, aA
27
Meta Characters
Combining Meta Charaters
$ ls a?[123]$ ls a?[123] aa1, au3, az2
$ ls data*[89][12] $ ls data*[89][12] data81, data91, data82
$ ls mail?[xy]*$ 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, letter,prog1, prog2
rdefe jsm ith
x1, x2, x3, x4
save m box, jandatinfo
djones
hom e etc lib
/
Current Directory
Parent Directory
29
Meta Characters
Copy /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 .$$
$ cp /home/djones/info .$$
pwd
$ cp ../djones/info .$
$ cp ../djones/info .$
30
Meta Characters
Copy /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$$
$ cp ../mbox tempmbox$$
pwd
$ cp ../mbox .$
$ cp ../mbox .$
31
Meta Characters
Other 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 .$
$ cp ../*.f .$
pwd
$ cp ../../rdefe/mbox .$
$ cp ../../rdefe/mbox .$
32
…Even More Redirection
Output Normally Displayed to the Screen Can Be Redirected to a File
$ pr prog1 >> prog1.pr$
$ 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 Redirection
UnixCommand | 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)$
$ 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 screenOutput displayed to the screen
34
Shell Scripts
Text File that Contains Unix CommandsCorrect Command Syntax
Options Arguments
One Command per Line
$ cat myshellwhodate$
$ cat myshellwhodate$
$ rm * -i$ rm * -i
$ cat myshell2who;date$
$ cat myshell2who;date$
x