Download presentation
Presentation is loading. Please wait.
Published byMoses Hampton Modified over 9 years ago
1
LIN 69321 Unix Lecture 3 Hana Filip
2
LIN 69322 UNIX Resources http://www.unix.org/ UNIX Tutorials http://www.unixtools.com/tutorials.html UNIX help for Users (developed at the University of Edinburgh) http://blackduck.union.edu/UNIXhelp1.3.2/ “Grep for Linguists” (by Stuart Robinson): http://arts.anu.edu.au/linguistics/misc/comp_resources/grep.html
3
LIN 69323 Overview File Management with Shell Commands Simple Script Grep and Egrep
4
LIN 69324 File Management with Shell Commands How to name a file –File = a collection of characters –The name must be all one ‘word’ or ‘string of characters’ –No spaces are allowed, because spaces are used by UNIX (and other operating systems) to identify the discrete ‘pieces’ of a command line in the same way they are used in regular expressions –14 characters or less (less is better)
5
LIN 69325 File Management with Shell Commands % vi letter to mom “edit three separate files named letter, to and mom”
6
LIN 69326 File Management with Shell Commands Don’t begin a file with a character that is not a letter or number NEVER use the characters ! “ * > < | $ @ ?
7
LIN 69327 File Management with Shell Commands ACCEPTABLE NOT ACCEPTABLE ozzieozzie nelson why-mewhy-me? bonzo-4bonzo 4 English190Enlgish 190 greatgreat! prog1*prog1 fido.nerfball fido nerfball
8
LIN 69328 File Management with Shell Commands % script [RETURN] % who % date % whoami % [CTRL-D] % exit Script done, output file is typescript
9
LIN 69329 File Management with Shell Commands To see what’s in your typescript file: % more typescript or (depending on the UNIX version) % page typescript 1. To see the next screenful of text press the SPACEBAR 2. If you wish to return to the Shell, press the DELETE key % less typescript % cat typescript
10
LIN 693210 File Management with Shell Commands To see what’s in your typescript file: % head typescript displays the first 10 lines of a file % tail typescript displays the last 10 lines of a file
11
LIN 693211 File Management with Shell Commands % ls [RETURN] % typescript you should see typescript listed among your files % cp typescript [NEW.FILENAME] [RETURN] cp = copy % ls % typescript [NEW.FILENAME] you should see typescript and whatever name you gave to the copied file listed among your files
12
LIN 693212 File Management with Shell Commands
13
LIN 693213 File Management with Shell Commands Renaming a file % mv file1 file2 [RETURN] % ls % file2
14
LIN 693214 File Management with Shell Commands Removing a file % rm typescript [RETURN] % ls –The file typescript should no longer be listed –There is no UNDO command for actions performed in the Shell Mode –You can remove more than one file at once: % rm file1 file2 … filen [RETURN]
15
LIN 693215 File Management with Shell Commands Options as arguments –Specify how a command ‘does its thing’ % rm -i typescript [RETURN] remove typescript ? The -i option informs the Shell to question your attempt to remove the specified file(s).
16
LIN 693216 File Management with Shell Commands Commands have online documentation, called man (manual) pages. For more information about any of these, just type % man [COMMAND] [RETURN] % man pwd
17
LIN 693217 File Management with Shell Commands Options as arguments –Specify how a command ‘does its thing’ –In manual instructions, optional arguments are shown in brackets following the command: % command [options]
18
LIN 693218 File Management with Shell Commands ls [-options] [name] -a list all files, including those starting with a "." -d list directories like other files, rather than displaying their contents -k list file sizes in kilobytes -l long (verbose) format — show permissions, ownership, size, and modification date -t sort the listing according to modification time (most recently modified files first) -X sort the files according to file extension -1 display the listing in 1 column
19
LIN 693219 File Management with Shell Commands files and directories beginning with a period (. ) typically hold settings for programs. Here are brief descriptions of many common dot files, generally stored in the root directory of your account:.cshrc Initialization for csh and tcsh shells.emacs Initialization and key-mappings for Emacs editor.login Startup commands for login shell.logout Commands to execute upon exiting shell.netscape/ Configuration directory for Netscape web browser.newsrc Newsgroup and article information for newsreaders.rhosts Users not requiring password to log in.signature Brief file appended to email and news postings.tcshrc Initialization for tcsh shell
20
LIN 693220 File Management with Shell Commands Options can be combined –a verbose listing of files by last modification date: % ls -lt
21
LIN 693221 File Management with Shell Commands The verbose listing shows the file permissions of a given file: -rwxr-xr-x directories have a "d" in the first column regular files have a "-". the remaining 9 characters indicate owner, group, and world permissions of the file An "r" indicates it's readable "w" is writable, "x" is executable A dash in the column instead of a letter means that particular permission is turned off.
22
LIN 693222 File Management with Shell Commands r readable w writable x executable - permission is turned off -rwxr-xr-x a plain file that is read-write-execute by the owner, and read- execute by group and world. drwx------ a directory that is read-write-execute by owner, and group and world have no permissions at all.
23
LIN 693223 File Management with Shell Commands % chmod [permissions] [file] Changes the permissions of the named file. You can use numbers: % chmod 755 index.html The first number translates to permissions by the owner. The second is permissions for the group. The third is permissions for everyone. Number Perms 0 --- no permissions 1 --x executable only 2 -w- writable only 3 -wx writable and executable 4 r--- readable only 5 r-x readable and executable 6 rw- readable and writable 7 rwx readable, writable, and executable
24
LIN 693224 File Management with Shell Commands A second way of setting permissions is with letters: % chmod u+rwx index.html % chmod go+rx index.html u is the owner's ("user's") permissions g is the group permissions o is "other" or world permissions. The + sign turns the stated permissions on; the — sign turns them off If you want to change a file so that it's group writable, but not readable or executable, you'd do: % chmod g+w,g-rx index.html
25
LIN 693225 Example of a simple shell script # This script displays the date, time, # username and current directory. echo "Date and time is:" date echo "Your username is: `whoami`" echo "Your current directory is:" pwd
26
LIN 693226 Example of a simple shell script # This script displays the date, time, # username and current directory. echo "Date and time is:" date echo "Your username is: `whoami`" echo "Your current directory is:" pwd
27
LIN 693227 Example of a simple shell script # This script displays the date, time, # username and current directory. lines beginning with a hash (#) are comments and are not interpreted by the Shell.
28
LIN 693228 Example of a simple shell script # This script displays the date, time, # username and current directory. echo "Date and time is:" When used as a Shell command echo echo prints its argument When echo ing multiple words, they must be placed within quotes (single or double)
29
LIN 693229 Example of a simple shell script # This script displays the date, time, # username and current directory. echo "Date and time is:" date echo "Your username is: `whoami`" The backquotes (`) around the command whoami illustrate the use of COMMAND SUBSTITUTION: To include the output from one command within the command line for another command, enclose the command whose output is to be included within `backquotes`.
30
LIN 693230 Executing the shell script Before using a file as a shell script you must change its access permissions so that you have execute permission on the file, otherwise the error message Permission deniedis displayed. To give yourself execute permission for the file containing the script use the command: % chmod u+rwx display To run the shell script, simply type its name at the prompt. The commands in the script will then execute one at a time as though you were typing them in at the terminal.
31
LIN 693231 Executing the shell script % chmod u-x display % display display: Permission denied.
32
LIN 693232 Searching for something in a file % grep [options] pattern filenames % fgrep [options] string filenames fgrep (or "fast grep") only searches for strings grep is a full-blown regular-expression matcher
33
LIN 693233 File Management with Shell Commands Changing to another directory % cd.. [RETURN] go up a directory tree % cd [DIRECTORY] [RETURN] change to a subdirectory % cd /tmp to change to some other directory on the system, you must type the full path name
34
LIN 693234 File Management with Shell Commands Create a directory % mkdir [DIRECTORY.NAME] [RETURN] Remove a directory % rmdir [DIRECTORY.NAME] [RETURN]
35
LIN 693235 Searching for something in a file > cd.. > cd c6932aab > ls display shakespeare > cp shakespeare ~ c6932aad > cd > ls shakespeare
36
LIN 693236 Searching for something in a file % grep [options] pattern filenames % fgrep [options] string filenames fgrep (or "fast grep") only searches for strings grep is a full-blown regular-expression matcher Some of the valid options are: -i case-insensitive search -n show the line# along with the matched line -v invert match, e.g. find all lines that do NOT match -w match entire words, rather than substrings
37
LIN 693237 Searching for something in a file with GREP % grep -inw ”thou" shakespeare find all instances of the word ”though" in the file “shakespeare”, case- insensitive but whole words and display the line numbers
38
LIN 693238 Grep grep '^smug' files {'smug' at the start of a line} grep 'smug$' files {'smug' at the end of a line} grep '^smug$' files {lines containing only 'smug'} grep '\^s' files {lines starting with '^s'} grep '[Ss]mug' files {search for 'Smug' or 'smug'} grep 'B[oO][bB]' files {search for BOB, Bob, BOb or BoB } grep '^$' files {search for blank lines} grep '[0-9][0-9]' file {search for pairs of numeric digits}
39
LIN 693239 Grep grep '[^a-zA-Z0-9] {anything not a letter or number} grep '[0-9]\{3\}-[0-9]\{4\}' {999-9999, like phone numbers} grep '^.$' {lines with exactly one character} grep '"smug"' {'smug' within double quotes} grep '"*smug"*' {'smug', with or without quotes} grep '^\.' {any line that starts with "."} grep '^\.[a-z][a-z]' {line start with "." and 2 lc letters}
40
LIN 693240 Egrep The version of grep that supports the full set of operators mentioned above is generally called egrep (for extended grep) % egrep '(mine|my)' shakespeare
41
LIN 693241 Grep % vi /class/lin6932/c6932aab/shakespeare
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.