Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Part 2Unix Introduction
More Unix Commandswc
touch
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
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
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.
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.
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)
touch
Use touch to create an empty file.
$ touch newfilename
Using the touch command on an existing file will update the last modify time.
touch Practice
Create a new file in dir3 called my.stuff
$ cd ../dir3 $ touch my.stuff
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).
who
This command will display information about who is currently logged onto the system.
$ who
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
find
$ find
Start with the command_name 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]
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.
find
$ find [directory] -name [filename]
• Now you type the name of the file you are looking for.
$ 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
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?
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.)
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
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.
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
Even More Commands
Remember to check the man pages for each of the new
commands you've learned so far.
Command Options
Options/Flags• ls
• cp
• mv
• rm
Review
• Unix Commands generally have three parts:
command_name [options] [operands]
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.
Examples
$ ls temp
$ cp test1 test2
$ mv pict /home/jsmith/family_pics
Examples$ ls -l temp
$ cp -r test1 test2
$ mv -i pict /home/jsmith/family_pics
ls
-a
-l
-p
-t
ls -a
“all” • ls -a will list all files
• Just typing ls will not list entries whose names begin with a period (.) “hidden files”
ls -a
Example:
$ ls dir2
address_list final.paper picts cats history.txt
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.
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).
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
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
Example:
$ ls -p dir2
address_list final.paper picts/
cats/ history.txt
ls -p
ls -t
“time”
• ls -t will list the files and directories, sorted by time modified (most recently modified first).
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.
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
List dir2 in long format. Two entries begin with a ‘d’ - why
do you think this is?
$ ls -l (they are directories)
ls Practice
cp
-i
-r
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.
cp -r
“recursive”
cp -r specifies that when you copy a directory, you also copy all the subdirectories and files within that directory.
m v
-i
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.
r m
-i
-r
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.
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.
$ 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
More Flags & Options
Check man for a list of available options for each command.
These options will differ slightly with different versions of Unix.
grepGlobal Regular
Expression and Print
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.
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”)
?
grep
What if ? You want to search for the phrase “tiny
fish” in one of your files in the current directory.
?
grep in-class practice
Make dir1 your current working directory.
$ cd Unix_class/dir1
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
grep
The program will then list the name of the file and the entire line in which the search string was found.
grep
If you know what file to look in, you can substitute the filename for the asterisk (*).
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
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
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 *
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.
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.
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.
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.
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”
grep - Regular Expressions
*(asterisk)
Matches multiples of the preceding character
Example: $ grep 'ex3*7' filename This would find “ex37,” “ex337,” “ex333337,”
but not “ex347”
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”
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”
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
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)?
In dir1, type:
$ grep 'r...ing' *
What are the matches in each line?
grep - Practice
Breeding
fairy ring
rhyming
resting
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.
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
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
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.
Wildcards and Regular Expressions
? *
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.
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.
Regular Expressions
Wildcards are different from the regular expressions used in grep (although they may look similar at times).
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
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.
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.
Unix and the Internet
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.
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.
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.
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.
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).
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
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.
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.
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.
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]
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
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.
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
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.
100
Text Editing
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.
What is a text editor?
A text editor does let you create a file of text, edit that text, and save the file.
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
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.
Ready for more?