October 10, 2019

Connecting to the server

Try this using Chrome or Firefox. Microsoft browsers do not work well

Please open the page http://rstudio.iu.edu.tr:4200/

If it works, you will connect to the server from the browser

Change your password

To be sure that nobody else uses your account

change the password I gave you for a good password

What is a good password?

You can use that idea

Change your password

The command to change your password is

passwd

You need to write the old password once, and the new passwords twice

Searching the contents of a file

Simple searching using less

Using less, you can search though a text file for a keyword (pattern).

For example, to search through science.txt for the word 'science', type

$ less science.txt

then, still inside less, type a / (forward slash) followed by the word to search

/science

As you can see, less finds and highlights the keyword. Type n to search for the next occurrence of the word.

grep

grep searches files for specified words or patterns. Type

$ grep 'science' science.txt

As you can see, grep has printed out each line containing the word science.

Is this all?

“This” is not the same as “this”

Try typing

$ grep 'Science' science.txt

The grep command is case sensitive; it distinguishes between Science and science.

To ignore upper/lower case distinctions, use the -i option. Type

$ grep -i 'science' science.txt

Be careful with spaces

To search for a phrase or pattern, you must enclose it in single quotes

(this symbol: ')

For example to search for spinning top, type

$ grep -i 'spinning top' science.txt

Some of the other options of grep are:

-v display those lines that do NOT match
-n precede each matching line with the line number
-c print only the total count of matched lines

Try some of them and see the different results

You can use several options at a time

For example, the number of lines without the words science or Science is

$ grep -ivc science science.txt

wc (word count)

A handy little utility is the wc command, short for word count. To do a word count on science.txt, type

$ wc -w science.txt

To find out how many lines the file has, type

$ wc -l science.txt

Summary

Command Meaning
grep 'keyword' file search a file for keywords
wc file count number of lines/words/characters in file

Redirection  

  • Most UNIX commands write to the standard output
    • that is, they write to the terminal screen
  • and many take their input from the standard input
    • that is, they read it from the keyboard
  • There is also the standard error, where processes write their error messages
    • by default, to the terminal screen.

Redirection

We have already seen one use of the cat command to write the contents of a file to the screen.

Now type cat without specifying a file to read

$ cat

Then type a few words on the keyboard and press the [Return] key.

Finally hold the [Ctrl] key down and press d (written as ^D for short) to end the input.

What has happened?

If you run the cat command without specifying a file to read, it reads the standard input (the keyboard), and on receiving the ‘end of file’ (^D), copies it to the standard output (the screen).

In UNIX, we can redirect both the input and the output of commands.

Redirecting the Output  

We use the > symbol to redirect the output of a command. For example, to create a file called list1 containing a list of fruit, type  

$ cat > list1

Then type in the names of some fruit. Press [Return] after each one.

pear
banana
apple
^D 

(this means press [Ctrl] and [d] to stop)

What happened?

The cat command reads the standard input (the keyboard) and the > redirects the output, which normally goes to the screen, into a file called list1

To read the contents of the file, type

$ cat list1

Exercise

Using this method, create another file called list2 containing the following fruit:

orange
plum
mango
grapefruit

Read the contents of list2

Appending to a file

The code >> appends standard output to a file. So to add more items to the file list1, type

$ cat >> list1

Then type in the names of more fruit

peach
grape
orange
^D

(Ctrl-D to stop)

Checking what happened

To read the contents of the file, type

$ cat list1

You should now have two files. One contains six fruit, the other contains four fruit.

Summary

Command Meaning
command > file redirect standard output to a file
command >> file append standard output to a file
who list users currently logged in

References