340 likes | 469 Views
Introduction to UNIX. Unix File System. Directory Organization Hierarchy of Files & Directories. Unix File System. Identifying Files Using Full Path Names. /home/rdefe/mbox. Full Path Names Always begin with /. mbox. data/jan/file1. Unix File System.
E N D
Unix File System • Directory Organization • Hierarchy of Files & Directories
Unix File System • Identifying Files Using Full Path Names /home/rdefe/mbox Full Path Names Always begin with /
mbox data/jan/file1 Unix File System • Identifying Files using Relative Path Names • Dependent on Your Location in the File System Current Working Directory
Directory Manipulation • mkdir [directory1] [directory2] … • Create New Directories $ mkdir mail $ Directories created based on your current location in the File System
$ cd /home/djones Directory Manipulation • cd [DirectoryName] • Change Directory $ cd data
Directory Manipulation • pwd • Display Present Working Directory $ pwd /home/rdefe $
Directory Manipulation • rmdir [directory1] [directory2] … • Remove Empty Directories $ rmdir mail $ X
Directory Manipulation • rm [-ri] [directory1] [directory2] … • -rRemove Directories • -i Interactive X $ rm -r data $ Caution: Will delete directories that have files & subdirectories $ rm -i mbox mbox? y $
Unix Commands • cp [-i] [SourceFile] [DestinationFile] • Copy A File • -i Interactive $ cp jan jan.copy $ $ cp /home/jsmith/data mydata $ $ cp /home/jsmith/d5 /tmp/a5 $
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
Unix Commands • mv [-i] [SourceFile] [DestinationFile] • Rename a File • Move a File to a Different Directory $ mv jan feb $ $ mv jan /tmp/oldjan $ $ mv jan /tmp $
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
1 $ ls jan $ ln jan feb $ ls jan feb $ 2 $ vi feb $ 3 $ cat jan $ cat feb $ • 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 Unix Commands • ln [-i] [SourceFile] [DestinationFile] • Create multiple names that all reference the same data
X $ rm jan Use the rm command to remove links Unix Commands • How Links Work $ ls jan $ ln jan feb $ ln feb mar $ ls jan feb mar $ jan feb mar
Unix Commands • How Links Work • ls -ito display inode numbers • ls -lto display number of links 1582 feb $ ls -i 1582 feb 1582 mar $ 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 $
1 $ ls jan $ ln jan feb $ ls jan feb $ 2 $ vi feb $ 3 $ cat jan $ cat feb $ Unix Commands • ln [-i] [SourceFile] [DestinationFile] • Create multiple names that all reference the same data
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
Examplescp, mv, ln $ ls -li 1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1 $ cp letter1 letter1.cp $ ls -li 1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1 1509 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1.cp $ mv letter1.cp doc $ ls -li 1509 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 doc 1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1
Examplescp, mv, ln $ ls -li 1509 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 doc 1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1 $ ln doc a $ ln a b $ ls -li 1509 -rw-r--r-- 3 rdefe unix 1696 Oct 29 21:39 a 1509 -rw-r--r-- 3 rdefe unix 1696 Oct 29 21:39 b 1509 -rw-r--r-- 3 rdefe unix 1696 Oct 29 21:39 doc 1407 -rw-r--r-- 1 rdefe unix 1696 Oct 29 21:39 letter1
Examplescp, mv, ln • cpMakes new copies of files • mvMoves and/or renames files • lnCreate additional file names
Unix Commands • grep [string] [File1] [File2] … • Find a char string contained in a file • Global Regular Expression Processor $ cat data this is a sample file that we'll use to test the unix grep command. unix commands can some interesting names $ grep unix data unix grep command. unix commands can some interesting
Unix Commands • grep [string] [File1] [File2] … $ grep unix data wp data:unix grep command. data:unix commands can some interesting wp:created with the unix command vi. wp:this unix editor can $ $ grep "unix comm" data wp data:unix commands can some interesting wp:created with the unix command vi. $
* Match zero or more characters $ ls a* Meta Characters • Special Characters with Special Meaning • / \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & # • Used to Save time $ more data* $ ls a*z $ cp a* data $ ls a*9*z $ rm junk*
? Match any single character $ ls a? Meta Characters • Special Characters with Special Meaning • / \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & # • Used to Save time $ more data? $ ls a?z $ cp a? data $ ls a?? $ rm junk?
[ ] 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 Meta Characters • Special Characters with Special Meaning • / \ “ ` * ; ? { } ( ) [ ] ~ ! $ < > | & # • Used to Save time
$ ls a?[123] aa1, au3, az2 $ ls data*[89][12] data81, data91, data82 $ ls mail?[xy]* mailxx1234, mail8y Meta Characters • Combining Meta Charaters
Meta Characters • . Current Directory • .. Parent Directory • ~ Home Directory Parent Directory Current Directory
Meta Characters • Copy /home/djones/info to rdefe pwd $ cp /home/djones/info . $ $ $ cp ../djones/info . $
Meta Characters • Copy /home/rdefe/mbox to data pwd $ cp ../mbox tempmbox $ $ $ cp ../mbox . $
Meta Characters • Other Examples... $ cp ../*.f . $ pwd $ cp ../../rdefe/mbox . $
…Even More Redirection • Output Normally Displayed to the Screen Can Be Redirected to a File If the file prog1.prdoes not exist send the output of the pr command to a new file prog1.pr $ pr prog1 >> prog1.pr $ If the file prog1.prexists append the output of the pr command to prog1.pr Note: Nothing is displayed to the screen
…Even More Redirection • UnixCommand | tee FileName • Display Screen Output and Redirect to a File Output saved to the file whoison $ who | tee whoison rdefe ttyp0 Aug 23 9:07 (dana) jsmith ttyp2 Aug 23 22:30 (dana) rdefe ttyp3 Aug 23 13:53 (dana) $ Output displayed to the screen
Shell Scripts • Text File that Contains Unix Commands • Correct Command Syntax • Options • Arguments • One Command per Line x $ rm * -i $ cat myshell who date $ $ cat myshell2 who;date $