<<

Introduction to Studeren and Presenteren 15/09/15

At LIACS we will be using the Unix/ . It is important that you understand all the basic commands. These exercises will show you:

1. Basic commands for navigating and manipulating files in the directory structure

2. How to use and completion

3. How to things

4. How to simple shell scripts

To you do the following exercises, please use the documents found "Unix is a Four Letter Word... and is a Two Letter Abbreviation" (http://unix.t-a-y-l-o-r.com/index.html). There may be small differences between the commands in the document and the commands on our system. This is due to small variations between different Unix operating systems. You can get precise information about specific commands by looking at the man (manual) pages on the machine you are working on.

Unix Overview

Unix is case sensitive, so be careful when using lower and upper case letters

Unix command syntax is as follows:

command –[flags] argument1

–l t*

Where ls is the command, -l is an (optional) flag and t* is the argument.

Flags are generally preceded by a hyphen, to prevent them from being interpreted as arguments (or filenames).

Tab Completion The default shell on the LIACS system allows tab completion. When you part of a command or a name, and press tab, the computer will try and complete what you have typed.

History As you type in and use commands, the operating system will remember what you typed. If you would like to reuse a command, you can retrieve it by using the arrow keys to navigate back to it. The up-arrow key moves backwards in the list and the down-arrow key moves forward in the list.

Command Line Editing If you need to edit the current command on the command line, you can navigate through the text by using the left and right arrow keys.

Exercise 1 - Navigating the directory structure

Open a terminal window. When you first log in, you are in your home directory, will have the same name as your user-name. Execute each of the following commands by typing them in at the prompt > What happens in each case?

ls ls -l ls p* ls = list. This command lists the contents of the current directory.

Type man ls and find out how to list files by when they were modified or created.

The man pages are the command line manual pages for Unix. Man ls gives you a full description of the ls command on this Unix version.

Type to command:

ls -a

This lists all the files in your current directory, including those with a prefix (.). These are known as hidden files and should not be changed unless you know what you are doing.

Exercise 2 - Creating Directories

The command makes a new directory (folder). Type:

mkdir exercises move into the new exercises folder by typing

exercises cd (change directory) allows you to move around the file system. Typing cd directoryname, moves you into that directory.

Type ls to see what is already in the folder. As you can see, there is nothing in there at the moment. Move back to the directory above by typing

cd .. cd .. moves you to the directory above. If you just type cd or cd~ you will move to your home directory and if you type cd . you will remain in the current directory.

See what security permissions the new directory has by typing:

ls -l

Experiment further with navigating the directory structure. Move back into your home directory and then move to the directory above. What is in there?

Moving around using a series of cd .. commands can sometimes be confusing. If you forget where you are, you can type (for Print Working Directory). This will display the complete path to the directory you are in. What is the full path for the exercises directory?

Exercise 3 – Removing directories

Remove the directory you have just created by typing

exercises

Check this has happened by typing ls

Exercise 4 - Directory permissions

Recreate the directory so that only you have read//execute by typing

mkdir -m 0700 exercises

Investigate how you can change the permissions so that other people have read and write permissions, by reading man . Only the owner of a file can use the chmod command to change the permissions. What do the codes and the numbers mean?

Move into the new exercises directory and create another directory inside exercises (called example) that is also only accessible to you.

Check that the new directory is in the right place by moving into it and typing pwd. The full path should look like:

/homedir/exercises/example

Exercise 5 - Creating and Concatenating Files

Navigate to your home directory by using the cd command. We will now create some new text files. You can open a new blank document by typing the name of a text editor, followed by the name of the file you wish to create. There are several text editors to choose from (e.g. you can use emacs or vi, or vim).

emacs textFile1.txt

In the text editor type the following sentence:

Hello Unix world. This is a plain text document.

Create another text file called textFile2.txt and type the following text:

Hello world. Plain text documents are useful as well as plain.

Combine the two files into another file called final.txt using the command (concatenate). Use man cat to find out how to concatenate the files.

You can see what is in the new concatenated file in a number of different ways. You can open the new file in a text editor, or you can print the content to the screen by using the more or commands. Is the content as you expected? What is the difference between more and less?

Note: when creating a file in plain text, it is good practice to add the .txt extension.

Exercise 6 - Moving files

Move the file final.txt to the folder exercises by using the (move) command

Copy the file final.txt to a file called final_copy.txt using the command

Exercise 7 -

You can use grep to search plain text files for matches to regular expressions. The command stands for globally search a and print.

Have a look at the man pages for grep. What is the difference between grep, egrep and fgrep?

Use grep to find out how many times the word ‘world’ is used in the two original text files you created earlier. What do you get as an output on the command line?

Use the grep command to search for the word 'plain'. Modify the command to search for either 'plain' and 'Plain' - do you get the same result?

Often, grep is used with | (pipe). Execute the following command:

ls | grep text

What is the effect?

The | command passes the results of one command onto another, to allow for further processing.

Exercise 8 - Find

Use man find to investigate what the find command is for and how to use it.

What is the effect of the following command

find . –name final -print

What happens when you execute the following command:

find /home -name final.txt -type f

Find can be used to files with particular content. It is very useful when combined with other commands, like grep or mv, to perform actions on the files you find.

Advanced: Work out how you would find the file final.txt and move it to your home directory, using one combined command.

Exercise 9 - Creating Shell Scripts

Shell scripts are simple programs that are written in a shell programming language and are interpreted by the shell process. They are very useful for automating tasks in Unix. First, you need to find out which shell you are using on this system by typing:

$shell

Simple shell scripts can be executed from the command line. Once you have written your script, you need to give it execute permissions and put it somewhere where the shell can find it.

Using a text editor, type the following text into a new file named myscript

#!/bin/tcsh (or #!/bin/bash if you are in a bash shell!) # My first script

echo "Hello World!"

Change the permissions on the new file so that you can execute it.

Execute the script – what happens?

Edit the script so that it performs other functions after displaying “Hello World!”. For example, add the pwd command, so that you can find out which directory you are running the script from and add the command, so that you can discover what the disk space usage is like.

Execute the script again - what do you get as output this ?

Exercise 10 – Shell Scripts Continued

Create a shell script that finds the exercises directory and copies a file into that directory.