105

Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

  • Upload
    others

  • View
    5

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Part 2Unix Introduction

Page 2: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

More Unix Commandswc

touch

Page 3: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

cp - ‘copy review’Let's copy a directory tree with recursion. Remember that in order to use the -r option when copying directory hierarchies.

Make sure your current directory is Unix_class:

$ cp -r dir2 dir3

Page 4: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

wc - ‘count’

This command will let you count the number of characters, words, and lines in a file.

To display the number of lines in a file:

$ wc -l filename

Page 5: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

wc - ‘count’

To display the number of words in a file:

$ wc -w filename

This command will let you count the number of characters, words, and lines in a file.

Page 6: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

wc - ‘count’

To display the number of characters in a file:

$ wc -m filename

This command will let you count the number of characters, words, and lines in a file.

Page 7: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

wc PracticeHow many lines are in the shrodinger poem? (in dir1)

$ wc -l shrodinger

$ wc -w final.paper

57 shrodinger

118 final.paper

How many words are in final.paper? (in dir2)

Page 8: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

touch

Use touch to create an empty file.

$ touch newfilename

Using the touch command on an existing file will update the last modify time.

Page 9: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

touch Practice

Create a new file in dir3 called my.stuff

$ cd ../dir3 $ touch my.stuff

Page 10: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

date

Typing date will give you the current day, date, time, and year and timezone.

$ date

Using the -u option will give you Greenwich Mean Time (universal time).

Page 11: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

who

This command will display information about who is currently logged onto the system.

$ who

Page 12: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find

In the very simplest form we can use this command to find the location of a file.This command has four parts to it.

The Swiss Army Knife of Unix Utilities

Page 13: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find

$ find

Start with the command_name find

Page 14: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find

• Specify the directory in which to start your search (such as /home/userid). The find command will start there and go down recursively.

• If you want to start your search in the current directory, simply type a period (.) here. More on this later.

• Don’t start with the root directory (/)-You would not be able to search through some Unix directories.

$ find [directory]

Page 15: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find$ find [directory] -name

-name specifies that you are looking for a filename.

• The find command can search for files by criteria other than the file name, such as modify time, size, or the file access permissions and many more attributes. I devote much more time to this command in S603 Unix Intermediate.

Page 16: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find

$ find [directory] -name [filename]

• Now you type the name of the file you are looking for.

Page 17: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

$ find . -name grp.txt (or…)

$ find ~/Unix_class -name grp.txt

• Make Unix_class your current directory • You are looking for your file called grp.txt,

starting in the current directory (Unix_class) and searching in all of its subdirectories.

• What subdirectory is grp.txt in?

dir1

find Practice

Page 18: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find PracticeWhat directory is catalyst in?

$ find . -name catalyst

$ find . -name address.list

./dir2/cats/catalyst (and)

./dir3/cats/catalyst

./dir1/address.list

(remember the . means the current working directory so it is assumed that your current working directory is Unix_class)

What directory is address.list in?

Page 19: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find

$ find /home/jsmith -name rec*

You can also use wildcards with find, (you can’t remember if the name is records, recordings, or just recs).

(It is best to surround the wildcard phrase with single or double quotes.)

Page 20: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find practiceFind all the files that begin with sh in Unix_class.

$ find . -name "sh*"./dir1/shrodinger ./Wildcards/shell.script.113 ./Wildcards/shell.script.276

Page 21: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

find

If the program has already found the file you wanted, but is still searching for more, then press Ctrl+C to stop the search.

Page 22: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Review Even More Commands

wc – count the characters, words, or lines in a file touch – create an empty file, modify the file's time date – tells you the current day, date, time, timezone, and year who – see who is logged onto the system find – find the location of a file

Page 23: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Even More Commands

Remember to check the man pages for each of the new

commands you've learned so far.

Page 24: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Command Options

Page 25: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Options/Flags• ls

• cp

• mv

• rm

Page 26: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Review

• Unix Commands generally have three parts:

command_name [options] [operands]

Page 27: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Command Options

So far we have discussed commands and operands.

Now we'll see some options that are available for our commands.

All commands come with default behavior and command options will allow us to modify this default behavior.

Page 28: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Examples

$ ls temp

$ cp test1 test2

$ mv pict /home/jsmith/family_pics

Page 29: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Examples$ ls -l temp

$ cp -r test1 test2

$ mv -i pict /home/jsmith/family_pics

Page 30: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

ls

-a

-l

-p

-t

Page 31: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

ls -a

“all” • ls -a will list all files

• Just typing ls will not list entries whose names begin with a period (.) “hidden files”

Page 32: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

ls -a

Example:

$ ls dir2

address_list final.paper picts cats history.txt

Page 33: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

ls -a

Example:

$ ls -a dir2

. .DS_Store cats history.txt

.. address_list final.paper picts

The . stands for the directory itself, and the .. stands for the parent directory. These files are created with the mkdir command.

Page 34: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

ls -l

“long format”

ls -l will list files in long format

This will give information about each file, such as who has permission to read it, when it was made, how large it is, etc. (we will cover more of this later in class).

Page 35: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Example:

$ ls -l dir2

-rw-r--r-- 1 name name 455 Jun 30 22:33 address_list

drwxrwxr-x 5 name name 512 Jun 30 22:33 cats

-rw-r--r-- 1 name name 716 Jun 30 22:33 final.paper

-rw-r--r-- 1 name name 7091 Jun 30 22:33 history.txt

drwxrwxr-x 2 name name 512 Jun 30 22:33 picts

ls -l

Page 36: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

ls -p puts a slash (/) after each filename if the type of file is a directory.

Remember that in Unix, everything is a file. Some are regular files and some are directory files. There are several other types of files in Unix (i.e. devices, links, etc.) but all you need to know is how to differentiate between regular files and directories.

ls -p

Page 37: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Example:

$ ls -p dir2

address_list final.paper picts/

cats/ history.txt

ls -p

Page 38: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

ls -t

“time”

• ls -t will list the files and directories, sorted by time modified (most recently modified first).

Page 39: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Combining OptionsOptions can easily be combined.

If you want to list all files in long format:

$ ls -al

This would also give you information about the current directory and the parent directory, because the . and .. are listed with the -a.

Page 40: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

How many files and directories are in dir2, including hidden files?

$ ls -a . .DS_Store cats history.txt .. address_list final.paper picts

(8 files and directories)

ls Practice

Page 41: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

List dir2 in long format. Two entries begin with a ‘d’ - why

do you think this is?

$ ls -l (they are directories)

ls Practice

Page 42: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

cp

-i

-r

Page 43: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

cp -i

“interactive”

When you are copying a file or directory, when using cp -i, the system will ask you before you overwrite a copied file over an existing file.

Page 44: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

cp -r

“recursive”

cp -r specifies that when you copy a directory, you also copy all the subdirectories and files within that directory.

Page 45: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

m v

-i

Page 46: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

mv -i

“interactive”

When you are moving a file or directory, when using mv -i, the system will ask you before you overwrite a moved file over an existing file.

Page 47: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

r m

-i

-r

Page 48: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

rm -i

“interactive”

When you are removing a file or directory, when using rm -i (or rmdir -i), the system will ask you before you delete any file.

The -i option in cp, mv and rm are safety features for you.

Page 49: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

rm -r

Using rm -r will remove a directory and all of the files and directories under it!

Like fire, it can be very useful or catastrophic so be careful when using this command option.

Page 50: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

$ pwd (verify you are in Unix_class)

$ rm -r dir3

$ ls

Delete the directory dir3 - deleting all the nested files and

subdirectories at the same time.

rm Practice

Page 51: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

More Flags & Options

Check man for a list of available options for each command.

These options will differ slightly with different versions of Unix.

Page 52: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grepGlobal Regular

Expression and Print

Page 53: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep

Looks inside files and searches for a series of characters (a phrase, a group of numbers, etc.). This is also referred to as contextual searching.

Page 54: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep

What if ?

...you want to look in all the files in the current directory (but not in its subdirectories) for a string of characters (also referred to as a “pattern list”)

?

Page 55: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep

What if ? You want to search for the phrase “tiny

fish” in one of your files in the current directory.

?

Page 56: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep in-class practice

Make dir1 your current working directory.

$ cd Unix_class/dir1

Page 57: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep 'tiny fish' *|

command

Type the following:

| space

| phrase you are

looking for

‘search string’

|

space | asterisk

* - means grep will look in all the files in the current directory

grep in-class practice

Page 58: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep

The program will then list the name of the file and the entire line in which the search string was found.

Page 59: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep

If you know what file to look in, you can substitute the filename for the asterisk (*).

Page 60: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep PracticeExample:

You want to find Sally’s information in address.list

Type: $ grep 'Sally Smith' address.list

Sally Smith 128 Hickory Lane 555-2974

Page 61: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep

You will always need single quotes (not any other kind of quote mark) around your search string if it has any spaces:

'class notes' or 'new ideas'

If your search string is only one word, you can still use the single quotes, but you don’t need them:

'notes' or 'ideas'

notes or ideas

Page 62: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep Practice

While you are in the dir1 subdirectory, search for:

Ash

How many results did you find?

What files were they in?

Remember - Unix is case sensitive

$ grep Ash *

Page 63: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep PracticeWhile you are in the dir1 subdirectory, search

for:

Ash

$ grep Ash *

address.list:Hon Chin 211 Ash Blvd. 555-5922

address.list:Ashley McNara 608 Redbud Ave. 555-8058

grp.txt:grep will also find parts of words, like bAshful.

Page 64: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep optionsWhile we are learning about grep it might be

useful to know some useful options: $ man grep

-i Ignores upper/lower case distinction during comparisons.

-l Prints only the names of files with matching lines, separated by NEWLINE characters. Does not repeat the names of files when the pattern is found more than once.

-v Prints all lines except those that contain the pattern.

Page 65: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Using “regular expressions” with grep gives you more flexibility. These special metacharacters mean something specific to the program:

. * [ ] ^ $

grep - Regular Expressions

Regular expressions are sets of symbols and syntactic elements used to match patterns of text.

Page 66: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Using “regular expressions” with grep gives you more flexibility. These special metacharacters mean something specific to the program:

. * [ ] ^ $

grep - Regular Expressions

When you use regular expressions, enclose your search string in quotes.

Page 67: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep - Regular Expressions

.(period)

Matches any single character

Example: $ grep 't.ck' filename This finds all examples of “tack,” “tick,”

“tock,” or “tuck” in the file, but not “thick”

Page 68: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep - Regular Expressions

*(asterisk)

Matches multiples of the preceding character

Example: $ grep 'ex3*7' filename This would find “ex37,” “ex337,” “ex333337,”

but not “ex347”

Page 69: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep - Regular Expressions

^(caret)

Matches only instances of the search string at the beginning of a line

Example: $ grep '^Once upon' filename This would find all lines beginning with the

phrase “Once upon”

Page 70: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep - Regular Expressions

$(dollar sign)

Matches only instances of the search string at the end of a line

Example: $ grep 'ever after$' filename This would find all lines ending with the phrase

“ever after”

Page 71: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

grep - Regular Expressions

[](brackets)

Matches any member of the set; use the brackets to enclose a set of options

Example: $ grep '[12,24,48] apples' filename

This would find “12 apples,” “24 apples,” or “48 apples” in the file

Page 72: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

How many lines begin with “No” in the shrodinger file (in dir1)?

grep - Practice

$ grep '^No' shrodinger

$ grep '8$' address.list

4 lines

5 numbers

• How many phone numbers in address.list end with an 8 (in dir1)?

Page 73: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

In dir1, type:

$ grep 'r...ing' *

What are the matches in each line?

grep - Practice

Breeding

fairy ring

rhyming

resting

Page 74: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

But what if you wanted to search for a phrase which contains a regular expression?

Example: you are looking for “$250” in the directory.

grep - Regular Expressions

If you typed:

$ grep $250 *

Because $ is a regular expression, you would not get the results you were looking for.

Page 75: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

To do this you need to use a backslash (\)

This negates the special properties of the following character.

This is often referred to as an 'escape' character. It is used to 'escape' the special meaning of a symbol.

Example: To look for $250, you would type:

$ grep '\$250' *

grep - Regular Expressions

Page 76: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Who has a phone number where the last four digits begin with a 9 and end with a 9?

grep - Practice

$ grep '9..9' address.list

$ grep '\$3.50' *What costs $3.50 in dir1?

A bag of gerbil food (the $3750 appears because the . can stand for any character)

Jordon Davis

Page 77: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

In the Shakespeare directory, type:

$ grep 'ade.$' *

What results do you find? Why is the period necessary?

grep - Practice

They live unwoo'd and unrespected fade, Of their sweet deaths are sweetest odours made:

Without the period, the comma or colon would tell the system that 'ade' does not come at the end of the line (the $). So be careful of punctuation.

Page 78: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Wildcards and Regular Expressions

? *

Page 79: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

WildcardsThere are two metacharacters which can act as a substitution for unknown letters or numbers in a filename or directory name.

These characters are not the same as they are in regular expressions using grep.

Page 80: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Wildcard Uses

You can use wildcards with commands other than ls.

Wildcards apply to all commands including grep and are used in place of or in combination with operands. Regular Expressions only apply to grep and a few other Unix commands.

Page 81: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Regular Expressions

Wildcards are different from the regular expressions used in grep (although they may look similar at times).

Page 82: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Regular Expressions

The regular expressions used in grep should always be enclosed in the single quotes:

$ grep 'model vx9*0' *|

Here the asterisk is a

regular expression

| Here the

asterisk is a wildcard

Page 83: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Regular Expressions

When you further your study with Unix you will appreciate what you have learned about regular expressions using grep.

Other Unix utilities such as sed, awk, and perl, python, php, and text editors such as vi and emacs will also use similar types of regular expressions.

Page 84: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Regular Expressions

With regular expressions one can easily search through a text file (or a hundred text files) searching for words that have a certain suffix (but only if the word begins with a capital letter and occurs at the beginning of the line), replacing the suffix with another suffix, and then change all the lower case characters to upper case. With the right tools, a series of regular expressions would do just that.

However, this is well beyond the scope of this class.

Page 85: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the Internet

Page 86: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the Internet

The internet and the world wide web were built with Unix servers.

A server is just a computer that stores files and “serves” them whenever requested.

Page 87: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the Internet

A web server is a computer that stores many html files and various programs and then waits for other computers to request certain files usually via the browser. When a request is received it finds the file and serves it to the requesting computer.

Page 88: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the InternetAn IP number (internet protocol) is the numeric address for a computer on the internet. This address identifies your computer much like your street address identifies your home.

Similarly, your login name (userid) is also translated to a unique number that identifies you to the system.

Page 89: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the InternetA name can simply be the name of the server (computer/host), i.e. ella. This is referred to as a “relative” hostname. This is just a name we give the computer when Unix is installed. It may be all we need for a local network but there could be a lot of computers named “ella” out there. We need a way to further identify this computer in order to make it more unique.

Page 90: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the InternetTo create a more unique computer (host) name we use a “fully qualified” hostname. This is similar to identifying your home address further by adding your city, state, and zipcode. The fully qualified hostname of the student Unix web server at SLIS is “ella.ils.indiana.edu”.

The domain, ils.indiana.edu further identifies our server from the “ella” server on the ucs network (ucs.indiana.edu).

Page 91: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the InternetWhen you visit a site, you are visiting a computer. You may type in a name such as:

www.ils.indiana.edu

This is called a Domain Name.

This name gets translated into a number, the I.P. number. In this case:

129.79.36.75

Page 92: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the Internet

Most people can remember names better than a string of numbers so a system was setup to make life easier for Internet users. This system is called DNS (Domain Name System).

DNS does the work of translating the name, i.e. www.ils.indiana.edu, to a number the system can use to correctly identify which server you want to connect to.

Page 93: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the Internet

Domain names are organized in a hierarchical way, similar to the file system in Unix. This hierarchy includes your company or server name, and a country code (for example, .uk or .ca) or a top-level domain (for example, .com or .edu).

A Web site on the server is created with one IP address, one host name and one domain name that together establish the identity of that Web site on the Internet.

Page 94: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the Internet

When you attempt to access a server via http (normal web browser access) you may have encountered an error from time to time relating to DNS. It may give you an error saying that you had a “DNS lookup failure”.

All this means is that the DNS server (most often running on Unix servers) cannot match the domain name you provided to an IP number.

Page 95: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Unix and the Internet

If you were so inclined, you could look the IP number up yourself using the command, nslookup, or the dig command.

nslookup manually converts a domain name to the matching IP number.

$ nslookup [fullyqualifiedhostname]

Page 96: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Internet Practice

$ nslookup www.ils.indiana.edu

Server: 10.79.1.1 Address: 10.70.1.1#53 www.ils.indiana.edu canonical name = ils.indiana.edu. Name: ils.indiana.edu Address: 129.79.247.195

Page 97: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Names

Canonical names refer to the real hostname of a computer or network server.

A CNAME record in the DNS database allow a server to be known by more than one hostname.

Page 98: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Protection with FirewallsMost companies use ‘firewalls’ as a means to protect their internal (private) network resources. A firewall is usually a set of related programs running on servers that sit at the gateway to a private network. Their function is to protect a private network from the outside world by limiting access to the network.

A firewall also includes or works with a proxy server that makes network requests on behalf of computer users. Basically, a firewall, working closely with a router program, filters

Page 99: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Protection with Firewalls

Firewalls

Firewalls are frequently used to prevent unauthorized Internet users from accessing private networks connected to the Internet, especially intranets. All messages entering or leaving the intranet pass through the firewall, which examines each message and blocks those that do not meet the specified security criteria.

Page 100: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

100

Text Editing

Page 101: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

What is a text editor?A text editor is not a word processor like Microsoft Word.

It will not do boldface, headers, footers, italics, changing fonts, or other things associated with a word processor.

Page 102: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

What is a text editor?

A text editor does let you create a file of text, edit that text, and save the file.

Page 103: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

What is a text editor?

The two most common text editors that were mostly there from the beginning in the Unix world are:

vi

emacs

Page 104: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Vi/Vim

I will cover vi/vim in depth in the Unix Intermediate class. For some there can be a steep learning curve and it would be helpful to have a working understanding of regex to make full use of this editor.

Some people fell in love with and are “wired” for emacs… but I am not one of them.

Page 105: Intro to Unix-2-2014 - Indiana University Bloomingtoninfo.ils.indiana.edu/~stevecox/unix/unix_intro/intro_unix-2.pdf · not be able to search through some Unix directories. $ find

Ready for more?