LIN Unix Lecture 3 Hana Filip
LIN UNIX Resources UNIX Tutorials UNIX help for Users (developed at the University of Edinburgh) “Grep for Linguists” (by Stuart Robinson):
LIN Overview File Management with Shell Commands Simple Script Grep and Egrep
LIN 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)
LIN File Management with Shell Commands % vi letter to mom “edit three separate files named letter, to and mom”
LIN File Management with Shell Commands Don’t begin a file with a character that is not a letter or number NEVER use the characters ! “ * > < | ?
LIN 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
LIN File Management with Shell Commands % script [RETURN] % who % date % whoami % [CTRL-D] % exit Script done, output file is typescript
LIN 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
LIN 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
LIN 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
LIN File Management with Shell Commands
LIN File Management with Shell Commands Renaming a file % mv file1 file2 [RETURN] % ls % file2
LIN 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]
LIN 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).
LIN 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
LIN 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]
LIN 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
LIN 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 and news postings.tcshrc Initialization for tcsh shell
LIN File Management with Shell Commands Options can be combined –a verbose listing of files by last modification date: % ls -lt
LIN 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.
LIN 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.
LIN 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 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
LIN 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
LIN 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
LIN 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
LIN 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.
LIN 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)
LIN 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`.
LIN 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.
LIN Executing the shell script % chmod u-x display % display display: Permission denied.
LIN 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
LIN 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
LIN File Management with Shell Commands Create a directory % mkdir [DIRECTORY.NAME] [RETURN] Remove a directory % rmdir [DIRECTORY.NAME] [RETURN]
LIN Searching for something in a file > cd.. > cd c6932aab > ls display shakespeare > cp shakespeare ~ c6932aad > cd > ls shakespeare
LIN 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
LIN 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
LIN 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}
LIN Grep grep '[^a-zA-Z0-9] {anything not a letter or number} grep '[0-9]\{3\}-[0-9]\{4\}' { , 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}
LIN 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
LIN Grep % vi /class/lin6932/c6932aab/shakespeare