October 11th, 2018

Connecting to the server

Did you got your username and password?

I sent an email to every person who answered the survey before October 10, 15:45

That email contains your account on the server. It has two parts:

  • username
  • password

The password is confidential. Only for your eyes

You are legally responsible for whatever happens with your account

Connecting to the server from Windows™

To connect to the server we use a protocol called ssh

In Microsoft Windows™ we can use PuTTY

(you have to download and install it)

The server name, or hostname is

rstudio.iu.edu.tr

Alternatives

You can have an ssh client as an extension in Google Chrome

Look for it on Chrome Web Store

You can also install the Official Microsoft SSH client

The SSH client is a part of Windows 10, but it’s an “optional feature” that is not installed by default.

It look like this

You use it from PowerShell or any other shell.
The command is sshusername@server

Connecting from Linux

  • If you already have Linux
    • or you are using the computers in the astronomy lab
  • Then you use a normal terminal and write the command
    sshusername@server
  • Is the same as in Windows PowerShell

First connection

The first time you connect, there will be a warning

Just say “yes”. This is to verify that you are connecting to the correct server

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

Removing files and directories

rm (remove file)

To delete (remove) a file, use the rm command. As an example, we are going to create a copy of the science.txt file then delete it.

Inside your unixstuff directory, type

$ cp science.txt tempfile.txt
$ ls
$ rm tempfile.txt
$ ls

rmdir (remove directory)

You can use the rmdir command to remove a directory

(make sure it is empty first)

Try to remove the backups directory

You will not be able to since UNIX will not let you remove a non-empty directory

Exercise

Create a directory called tempstuff using mkdir , then remove it using the rmdir command.

Displaying the contents of a file on the screen

clear (clear screen)

Before you start the next section, you may like to clear the window

At the prompt, type

$ clear

This will clear all text and leave you with the $ prompt at the top of the window.

cat (concatenate)

The command cat can be used to display the contents of a file on the screen. Type:

$ cat science.txt

As you can see, the file is longer than than the size of the window, so it scrolls past making it unreadable.

less

The command less writes the contents of a file onto the screen one page at a time. Type

$ less science.txt
  • Press the [space-bar] if you want to see another page
  • Type [q] if you want to quit reading

As you can see, less is better than cat for long files

head

tail

The tail command writes the last ten lines of a file to the screen.

Clear the screen and type

$ tail science.txt

Question. How can you view the last 15 lines of the file?

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
rm file remove a file
rmdir directory remove a directory
cat file display a file
less file display a file a page at a time
head file display the first few lines of a file
tail file display the last few lines of a file
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 form >> 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.

Concatenating files

We will now use the cat command to join (concatenate) list1 and list2 into a new file called biglist. Write

$ cat list1 list2 > biglist

What this is doing is reading the contents of list1 and list2 in turn, then outputing the text to the file biglist

See the result

To read the contents of the new file, type

$ cat biglist

Redirecting the Input

We use the < symbol to redirect the input of a command.

The command sort alphabetically or numerically sorts a list. Type

$ sort

Write the names of some animals. Press [Return] after each one.

dog
cat
bird
ape
^D

(control d to stop)

Output of sort

The output will be

ape
bird
cat
dog

Replacing the keyboard by a file

Using < you can redirect the input to come from a file rather than the keyboard. For example, to sort the list of fruit, type

$ sort < biglist

and the sorted list will be output to the screen.

To output the sorted list to a file, type,

$ sort < biglist > slist

Use cat to read the contents of the file slist

Pipes

To see who is on the system with you, type

$ who

One method to get a sorted list of names is to type,

$ who > names.txt
$ sort < names.txt

This is a bit slow and you have to remember to remove the temporary file names.txt when you have finished

There is a better way

What you really want to do is connect the output of the who command directly to the input of the sort command

This is exactly what pipes do

The symbol for a pipe is the vertical bar |

Example

For example, typing

$ who | sort

will give the same result as above, but quicker and cleaner.

To find out how many users are logged on, type

$ who | wc -l

Exercise

Using pipes, display all lines of list1 and list2 containing the letter ‘p’, and sort the result.

Summary

Command Meaning
command > file redirect standard output to a file
command >> file append standard output to a file
command < file redirect standard input from a file
command1 | command2 pipe the output of command1 to the input of command2
cat file1 file2 > file0 concatenate file1 and file2 to file0
sort sort data
who list users currently logged in

References