Download - LIN 69321 Unix Lecture 3 Hana Filip. LIN 69322 UNIX Resources UNIX Tutorials UNIX help for
LIN 6932 1
Unix Lecture 3
Hana Filip
LIN 6932 2
UNIX Resources
• http://www.unix.org/
• UNIX Tutorialshttp://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
LIN 6932 3
Overview
• File Management with Shell Commands
• Simple Script
• Grep and Egrep
LIN 6932 4
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 6932 5
File Management with Shell Commands
% vi letter to mom
“edit three separate files named letter, to and mom”
LIN 6932 6
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 6932 7
File Management with Shell Commands
ACCEPTABLE NOT ACCEPTABLE
• ozzie ozzie nelson • why-me why-me?• bonzo-4 bonzo 4• English190 Enlgish 190• great great!• prog1 *prog1• fido.nerfball fido nerfball
LIN 6932 8
File Management with Shell Commands
% script [RETURN]
% who% date% whoami% [CTRL-D]
% exit
Script done, output file is typescript
LIN 6932 9
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 6932 10
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 6932 11
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 6932 12
File Management with Shell Commands
LIN 6932 13
File Management with Shell Commands
• Renaming a file
% mv file1 file2 [RETURN]
% ls% file2
LIN 6932 14
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 6932 15
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 6932 16
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 6932 17
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 6932 18
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 6932 19
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
LIN 6932 20
File Management with Shell Commands
• Options can be combined– a verbose listing of files by last modification date:
% ls -lt
LIN 6932 21
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 executableA dash in the column instead of a letter means that particular
permission is turned off.
LIN 6932 22
File Management with Shell Commands
r readablew 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 6932 23
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
LIN 6932 24
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") permissionsg is the group permissionso is "other" or world permissions.
The + sign turns the stated permissions on; the — sign turns them offIf 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 6932 25
Example of a simple shell script
# This script displays the date, time, # username and current directory.echo "Date and time is:"dateecho "Your username is: `whoami`"echo "Your current directory is:"pwd
LIN 6932 26
Example of a simple shell script
# This script displays the date, time, # username and current directory.echo "Date and time is:"dateecho "Your username is: `whoami`"echo "Your current directory is:"pwd
LIN 6932 27
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 6932 28
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 echoing multiple words, they must be placed within quotes (single or double)
LIN 6932 29
Example of a simple shell script
# This script displays the date, time, # username and current directory.echo "Date and time is:"dateecho "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 6932 30
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 6932 31
Executing the shell script
% chmod u-x display % displaydisplay: Permission denied.
LIN 6932 32
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 6932 33
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 6932 34
File Management with Shell Commands
• Create a directory% mkdir [DIRECTORY.NAME] [RETURN]
• Remove a directory% rmdir [DIRECTORY.NAME] [RETURN]
LIN 6932 35
Searching for something in a file
> cd ..> cd c6932aab> lsdisplay shakespeare
> cp shakespeare ~c6932aad
> cd> lsshakespeare
LIN 6932 36
Searching for something in a file
% grep [options] pattern filenames% fgrep [options] string filenames
fgrep (or "fast grep") only searches for stringsgrep 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 6932 37
Searching for something in a filewith 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 6932 38
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 6932 39
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}
LIN 6932 40
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 6932 41
Grep
% vi /class/lin6932/c6932aab/shakespeare