THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

Similar documents
Mills HPC Tutorial Series. Linux Basics I

Chapter-3. Introduction to Unix: Fundamental Commands

Introduction to Unix: Fundamental Commands

Unix File System. Class Meeting 2. * Notes adapted by Joy Mukherjee from previous work by other members of the CS faculty at Virginia Tech

Using LINUX a BCMB/CHEM 8190 Tutorial Updated (1/17/12)

Unix Filesystem. January 26 th, 2004 Class Meeting 2

Linux Essentials. Programming and Data Structures Lab M Tech CS First Year, First Semester

Introduction to UNIX. Logging in. Basic System Architecture 10/7/10. most systems have graphical login on Linux machines

h/w m/c Kernel shell Application s/w user

Physics REU Unix Tutorial

Lab 3a Using the vi editor

Outline. Structure of a UNIX command

Unix File System. Learning command-line navigation of the file system is essential for efficient system usage

INTRODUCTION TO LINUX

The Unix Shell & Shell Scripts

Unix/Linux Basics. Cpt S 223, Fall 2007 Copyright: Washington State University

Chapter Two. Lesson A. Objectives. Exploring the UNIX File System and File Security. Understanding Files and Directories

CS246 Spring14 Programming Paradigm Notes on Linux

CSCI 2132 Software Development. Lecture 4: Files and Directories

Week Overview. Unix file system File types and file naming Basic file system commands: pwd,cd,ls,mkdir,rmdir,mv,cp,rm man pages

Linux Operating System Environment Computadors Grau en Ciència i Enginyeria de Dades Q2

Std: XI CHAPTER-3 LINUX

Introduction to Linux

Appendix B WORKSHOP. SYS-ED/ Computer Education Techniques, Inc.

Perl and R Scripting for Biologists

CHE3935. Lecture 1. Introduction to Linux

Hands-on Keyboard: Cyber Experiments for Strategists and Policy Makers

UNIX File Hierarchy: Structure and Commands

Linux/Cygwin Practice Computer Architecture

Using the Zoo Workstations

Session 1: Accessing MUGrid and Command Line Basics

Introduction. File System. Note. Achtung!

Unix Tutorial Haverford Astronomy 2014/2015

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Mauro Ceccanti e Alberto Paoluzzi

Introduction to the UNIX command line

Introduction to Unix - Lab Exercise 0

FILE MAINTENANCE COMMANDS

Welcome to getting started with Ubuntu Server. This System Administrator Manual. guide to be simple to follow, with step by step instructions

Introduction to Linux Environment. Yun-Wen Chen

CENG393 Computer Networks Labwork 1

History. Terminology. Opening a Terminal. Introduction to the Unix command line GNOME

Course 144 Supplementary Materials. UNIX Fundamentals

Introduction: What is Unix?

Overview LEARN. History of Linux Linux Architecture Linux File System Linux Access Linux Commands File Permission Editors Conclusion and Questions

Lezione 8. Shell command language Introduction. Sommario. Bioinformatica. Esercitazione Introduzione al linguaggio di shell

Introduction to Linux. Woo-Yeong Jeong Computer Systems Laboratory Sungkyunkwan University

Embedded Linux Systems. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Contents. Note: pay attention to where you are. Note: Plaintext version. Note: pay attention to where you are... 1 Note: Plaintext version...

Introduction of Linux

This lab exercise is to be submitted at the end of the lab session! passwd [That is the command to change your current password to a new one]

Linux Systems Administration Getting Started with Linux

Unix System Architecture, File System, and Shell Commands

Oxford University Computing Services. Getting Started with Unix

CS4350 Unix Programming. Outline

commandname flags arguments

EECS2301. Lab 1 Winter 2016

Introduction to Linux

CENG 334 Computer Networks. Laboratory I Linux Tutorial

Commands are in black

Tutorial 1: Unix Basics

Overview of the UNIX File System

Introduction to Linux Workshop 1

Using UNIX. -rwxr--r-- 1 root sys Sep 5 14:15 good_program

Getting started with Hugs on Linux

Basic Linux Command Line Interface Guide

Unix Introduction to UNIX

When talking about how to launch commands and other things that is to be typed into the terminal, the following syntax is used:

Lab Working with Linux Command Line

Getting Started With UNIX Lab Exercises

Introduction to Unix and Linux. Workshop 1: Directories and Files

Getting your department account

CSE 303 Lecture 2. Introduction to bash shell. read Linux Pocket Guide pp , 58-59, 60, 65-70, 71-72, 77-80

Essential Linux Shell Commands

Working with Basic Linux. Daniel Balagué

*nix Crash Course. Presented by: Virginia Tech Linux / Unix Users Group VTLUUG

Unix tutorial. Thanks to Michael Wood-Vasey (UPitt) and Beth Willman (Haverford) for providing Unix tutorials on which this is based.

Overview of the UNIX File System. Navigating and Viewing Directories

Read the relevant material in Sobell! If you want to follow along with the examples that follow, and you do, open a Linux terminal.

Getting started with Hugs on Linux

Introduction to Linux. Roman Cheplyaka

Linux Command Line Primer. By: Scott Marshall

INSE Lab 1 Introduction to UNIX Fall 2017

CSC209. Software Tools and Systems Programming.

Lecture # 2 Introduction to UNIX (Part 2)

First of all, these notes will cover only a small subset of the available commands and utilities, and will cover most of those in a shallow fashion.

INTRODUCTION TO BIOINFORMATICS

Hitchhiker s Guide to VLSI Design with Cadence & Synopsys

A Brief Introduction to the Linux Shell for Data Science

Introduction p. 1 Who Should Read This Book? p. 1 What You Need to Know Before Reading This Book p. 2 How This Book Is Organized p.

1 Installation (briefly)

Unix Handouts. Shantanu N Kulkarni

Getting Started with UNIX

Week 2. Exp 2 (a) (b): Introduction to LINUX OS, Installation of LINUX OS, Basic DOS commands

Introduction to Linux

Basic Linux Command Line Interface Guide

Week 2 Lecture 3. Unix

Unix Workshop Aug 2014

CS 2400 Laboratory Assignment #1: Exercises in Compilation and the UNIX Programming Environment (100 pts.)

commands exercises Linux System Administration and IP Services AfNOG 2015 Linux Commands # Notes

Command Line Interface The basics

Transcription:

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering ENG224 Information Technology Part I: Computers and the Internet Laboratory 2 Linux Shell Commands and vi Editor Objective: To practice the usage of Linux shell commands and vi editor Equipment: 1. Red Hat Linux 9.0. 2. vi editor. Methodology: Start up the system with your own Linux partition. Consult your tutor for the partition assigned to you. Login the system with username guest. Consult your tutor for the password required. Answer all the questions in this lab sheet and submit it to the tutor before you leave the laboratory. 1. Introduction What is a shell? The shell is the command line interface to Linux. It is similar to the Microsoft DOS prompt as known to Windows users, but is much more powerful with its scripting languages. Currently the standard setup of Linux has included two shells, bash and tcsh. What is a shell command? Users coming from the DOS environment are probably familiar with the concept of commands that encompass core features of the operating system, such as DIR, COPY, and ATTRIB. These commands provide the base on which more complicated actions could be built and from which sophisticated batch files could be written. But in the DOS world, as in many operating systems, the number of available commands is limited and generally static: users cannot add new commands. In Linux, and also in Unix, the concept is different. A command is any executable file. This means that any executable file added to a system becomes a new command on the system. How to use Linux Shell inside the X-Window system? A terminal emulator is a program that opens up a window and then runs a shell in that window. It could be compared to the DOS prompt in Windows. You start a terminal emulator like any other X program, so it means that you can have a command line while you are still safely in the GUI! There are a bunch of different terminal emulators out there, like xterm, rxvt, gnome-terminal, konsole, kvt, eterm, and many others. We use gnome-terminal to practice the exercise in this tutorial. Page 1

Open a console window by pressing the lower-left corner button and choose System tools, then Terminals. A terminal window similar to the one on Figure 1 will be shown. 2. Common Linux Commands Figure 1 a. Find the usage of commands If you don t know how to use the Linux shell commands, you can use man command to show you the usage of them. Usage: man <command-name> Example: man cd (press q when you finish reading the manual of cd) b. Finding out where you are The pwd command (which stands for present working directory) is the most basic of the three commands. By typing the command and hitting Return, you will be informed of which directory you are currently in. Usage: pwd Exercise 2.1 Type pwd in the command line. What is the result? Page 2

/home/guest Normally, every time when you open a terminal console, it would begin in your home directory. And the home directory is often located at /home/ <user name>. As we have login as guest, so our user name is guest and the command would give us /home/guest as the present working directory. c. Change directory The cd command allows you to change your current directory to any accessible directory on the system. Usage: cd <directory> Example: cd /usr/x11r6/bin d. Listing the contents of a directory The ls command can be used to view the contents of the current directory. Usage: ls [option] <filename> Example: ls al / Exercise 2.2 Type cd / and ls in the command line. What is the result? bin dev home lib misc opt root tmp var Boot etc initrd lost+found mnt proc sbin usr When we enter cd /, we have changed our present working directory to the root directory /. Then when we use ls, all files resided in the root directory / will be listed out. Exercise 2.3 Type these commands cd /usr/x11r6/, cd./bin/, cd.., and pwd. Give the current directory which you are located. What are the meanings of. and..? /usr/x11r6. current directory... parent directory. When we enter cd /usr/x11r6, our present working directory will be changed to /usr/x11r6, then when we enter cd./bin/, we will go into /usr/x11r6/bin. After cd.., we go into /usr/x11r6 again. Therefore, we can deduce the meaning of. and.. respectively. e. Find, Grep and Cat Page 3

The find and grep commands are powerful tools for searching files. While both commands are used for searching, their purposes differ: find is used to search for files by a number of criteria, including name or date of creation. grep is used to search the contents of files. The cat command combines one or more files and displays them on the standard output. If no files are given, the standard input is written to the standard output. Usage of find command: find <starting-directory> <parameters> <actions> Usage of grep command: grep <text-pattern> <file-list> Usage of cat command: cat <options> <filename> Exercise 2.4 Type "cd" to go back to your home directory. Type cat > testcat.txt in the command line. After pressing return, type the following line of text This is a test of cat., and then press crtl-d. Type cat testcat.txt again. What do you see? This is a test of cat. We have to change our present working directory back to the home directory by cd, as we are not permitted to create any file in the root directory /. From the manual page of man cat, we know that the command cat will concatenate files or standard input and print on the standard output. Therefore, through cat, we can make the computer to repeat every line we type. In other words, we type from the standard input and the computer repeats it in the standard output. For the command cat > testcat.txt above, the symbol > allows the output of the command cat (the keyboard input of user) to store into a file namely testcat.txt. Through cat > testcat.txt, we redirect the output from cat to a file called testcat.txt. As the output of cat is essentially the standard input which is also the line we have typed in. Therefore, testcat.txt contains This is a test of cat. Exercise 2.5 Type find. -name testcat.txt -print in the command line. What is the result? What are the purposes of the options -name and -print (use man to find out their meanings)?./testcat.txt From the manual of find, we know that it can search for files in a directory hierarchy. The way to call this command is, find [path ] [expression]. Also, we can find that the -name option is one of the expressions which allows us to specify the search pattern. While the -print option is another expression which allows us to print the full file name on the standard output, followed by a new line. Therefore, the meaning of find. name testcat.txt print is actually asking the computer to look for a file named testcat.txt in the current directory and print it out in the full file name format on the standard output. Page 4

Exercise 2.6 Type find. -name testcat.txt print > list.lst in the command line. You will find a file list.lst in your current directory. Use cat commands to show its contents. What is the result? What is the meaning of > list.lst? Same as 2.5 > list.lst means to redirect the output result of the previous statement to the file list.lst. After we have issued the command find. name testcat.txt print > list.lst, the computer did not print out anything! In fact, a file list.lst has already been created; we can check this out by ls. The left hand side of this command is the same as 2.5, so we would expect./textcat.txt to be printed out. However, this message has been redirected to the file list.lst, so no message will be printed. And the redirected message./textcat.txt could be found in list.lst by viewing its contents. Recall the function of cat ; it will concatenate files or standard input and print on the standard output. Therefore, we can view the content of the file by cat list.lst. Exercise 2.7 Go to / directory and type ls l grep bin in the command line. What is the result? What is the function achieved by this line of commands? (The character refers to pipe. It allows the output from the left hand side of to become the input of the command on its right hand side.) drwxr-xr-x 2 root root 4096 Sep 8 2004 bin drwxr-xr-x 2 root root 4096 Sep 8 2004 sbin Firstly, we change to the root directory by cd / and then ls l grep bin. We find out that only two lines containing bin and sbin were shown. However, if we neglect the right hand side command and try ls l, many more lines will be shown. The description from the man page man grep tell us that it searches the standard input for lines containing a match to the given PATTERN and print the matching lines. And the PATTERN now is bin, therefore, the whole command is to redirect the output of ls l as the standard input to grep bin, so that any file or sub-directory in the root directory / will be listed out. In fact, we can separate the above operation into two parts with an intermediate file. Firstly, we can ls l > /home/guest/temp, redirects the output to a file first. We shall read the whole list by cat /home/guest/temp. Then, we can pass it to grep by cat /home/guest/temp > grep bin. We will obtain the same result. The remove us the need of a temporary file in doing such redirection. What are the difference between and >? Is it possible to use ls l > grep bin instead of? 3. File Management Linux Commands Page 5

a. Copying files and directories To copy files, you can use the mv or cp commands. The command line mv file1 file2 allows one to move file1 to file2. file1 will no longer exist after the operation. So functionally it is similar to renaming a file. cp on the other hand copies one file to another. The following will copy file1 to file2. Note that if file2 does not exist, it will be created; but if it exists, it will be overwritten: Usage: cp file1 file2 There are not undo commands in Linux, so accidentally overwriting an important file would probably make you pull your head off. The risk of doing so is smaller if you use the -i option ("interactive") with cp. The following does the same as the above, but if file2 exists, you will be prompted before overwriting: Usage: cp i file1 file2 If you want to copy file1 into directory dir1: Usage: cp file1 dir1 The following would do the same as the above, copy file1 into dir1, but under a different name: Usage: cp file1 dir1/file2 You can also copy multiple files into one directory with a single command: Usage: cp file1 file2 file3 dir1 For copying directories, you can use the cp and mv commands just like you use them with files. If you have already tried to copy a directory with cp, you should have probably noticed that cp just complains at you. The cp command wants you to use the -r option if you want to copy a directory with its contents. The -r means "copy recursively": Usage: cp -r dir1 dir2 Exercise 3.1 Change to your home directory. Create a subdirectory called guestsub using the command mkdir guestsub. Copy the files testcat.txt and list.lst to guestsub. While staying at your home directory, copy./guestsub to another directory called./guestsub2? Give the command that you have used. mkdir guestsub cp testcat.txt guestsub/ cp list.lst guestsub/ cp -r guestsub guestsub2 First, we go back to our home directory by cd. Then, we create the new directory by mkdir guestsub. Then we copy those files into the sub-directory. We can do this separately by cp testcat.txt guestsub/ and cp list.lst guestsub/ or we can done this at the same time cp testcat.txt list.lst guestsub/. Now, we should be able to see two copies of these files, one in. and the other one in./guestsub, we can use ls. and ls./guestsub to verify it. Now, we want to copy the directory./guestsub to another Page 6

directory./guestsub2. We have to use cp r guestsub guestsub2, since guestsub is a directory. By issuing the -r, the recursive option to cp, we are able to copy a directory as well as its content to another. Remember to use the -r option whenever you want to copy a directory. You should be able to find three copies of these files now, in.,./guestsub and./guestsub2. Exercise 3.2 Rename the guestsub2 directory to guestsub3 using mv. Give the command that you have used. mv guestsub2 guestsub3 Before we rename the directory, we may enter ls l and mark down the creation time of the directory guestsub2 first. From the manual page man mv, we know that this function can be used in this way, mv [option]... source dest. As we do not require any additional option, so we can issue the command as mv guestsub2 guestsub3. Now, when we ls -l, we can no longer see the directory./guestsub2, instead we have a directory named./guestsub3. However, when we check its creation time, it will be equal to the creation time of guestsub2. Therefore, we have only renamed the directory but not created a new one. b. Deleting files and directories The rm command is used for removing files. To remove a file: Usage: rm file1 If you use the -i option, you'll be prompted before removing the file: Usage: rm -i file1 You can also delete more files at once: Usage: rm file1 file2 There are two commands you can use for removing directories. If the directory is empty, you can use rmdir: Usage: rmdir dir1 You can use rmdir only if the directory is empty. If you want to remove a directory with all its contents, you can use rm with the -r option. The -r option tells rm to remove a directory recursively: Usage: rm -r dir1 It goes without saying that you can cause a lot of trouble with rm -r if you are not careful. In some cases it might be a good thing to use the -i option when deleting a directory with its contents so that you would be prompted before each file in the directory gets deleted: Usage: rm -ir dir1 Be careful with the rm command! Linux does not have any undo commands, and it does not put files into Trash where you can save them later. Once you have deleted a file, it is permanently deleted from the file system. Page 7

Exercise 3.3 Delete the directory guestsub3 inside your home directory. Give the command that you have used. rm -r guestsub3 From the manual page man rm, we know that to remove a file we can issue the command as rm [option]... file... As we have to remove the whole directory now, we should use the -r recursive option. 4. vi Editor a. Introduction to vi editor The vi editor is a command-based editor used by many Linux users. The vi editor has powerful features to aid programmers, but many beginning users avoid using vi because the plentiful features overwhelm them. Although there are quite a lot of graphical based text editor provided by different distributions of Linux, learning vi is still important because in many situations, such as for server configuration, graphical user interface is not available. Text editing has to be done with commandbased editor like vi. This tutorial is written to help beginners get accustomed to using the vi editor, but also contains sections relevant to regular users of vi as well. Examples are provided, and the best way to learn is to try these examples, and think of your own examples as well. There is no better way than to experience things yourself. b. Starting the vi Editor The vi editor lets a user create new files or edit existing files. The command to start the vi editor is vi, followed by the filename. Usage: vi <filename> Example: vi test.txt When you start vi for the first time, you will see a screen filled with tildes (A tilde looks like this: ~ ) on the left side of the screen. Any blank lines beyond the end of the file are shown this way. At the bottom of your screen, the filename should be shown if you have specified an existing file, and the size of the file will be shown as well, like this: "filename" 21 lines, 385 characters If the file you specified does not exist, it will tell you that it is a new file, like this: "newfile" [New file] Page 8

Exercise 4.1 Go to your home directory. Use vi editor to open the file testcat.txt. What is the statement showing at the bottom of the editor? testcat.txt 1L, 16C To open this file in vi, we should issue vi testcat.txt. We can also execute vi by vi alone. However, we have to read in the file by ourselves then. To do so, change to command mode, which will be taught later, and then :r testcat.txt. c. The two modes of vi The first thing most users learn about the vi editor is that it has two modes: command and insert. The command mode allows the entry of commands to manipulate text. These commands are usually one or two characters long, and can be entered with few keystrokes. The insert mode puts anything typed on the keyboard into the current file. vi starts out in command mode. There are several commands that put the vi editor into insert mode. The most commonly used commands to get into insert mode are a and i. Once you are in insert mode, you get out of it by hitting the escape key. You can hit escape two times in a row and VI would definitely be in command mode. Hitting escape while you are already in command mode does not take the editor out of command mode. It may beep to tell you that you are already in that mode. Exercise 4.2 Move the cursor to the beginning of the file. Press i to go to insert mode. Insert the following characters This is a test.. What is the statement showing at the bottom of the editor? -- INSERT -- d. Getting out of vi Now that you know how to get into vi, it would be a good idea to know how to get out of it. The vi editor has two modes and in order to get out of vi, you have to be in command mode. Hit the key labeled "Escape" or "Esc" to get into command mode. If you are already in the command mode when you hit "Escape", don't worry. It might beep, but you will still be in the command mode. Exercise 4.3 Make sure that you are in command mode. Type :q and press <RETURN> key. What is that warning statement shown?? E37: No write since last change (add! to override) Page 9

The command to quit out of VI is :q. Once in command mode, type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, the command to quit out of vi without saving is :q!. This lets you exit vi without saving any of the changes. Of course, normally in an editor, you would want to save the changes you have made. The command to save the contents of the editor is :w. You can combine the above command with the quit command, or :wq. You can specify a different file name to save to by specifying the name after the :w. For example, if you want to save the file you are working as another filename called filename2, you would type :w filename2 and return. Exercise 4.4 Make sure that you are in command mode. Use :w to save to a file with name testcat2.txt. What is that statement? :w testcat2.txt testcat2.txt [New] 1L, 32C Sometime you may have executed in a wrong directory where you are not able to save the file. As a remedy, you may add the path of your home directory before in front of the file name. Suppose your home directory is /home/guest, then you may use :w /home/guest/testcat2.txt. Indeed, there is also a shorthand for the path of your home directory, which is ~. Therefore, you may also use :w ~/testcat2.txt. e. Some simple vi commands i. Cutting, Deleting and Undo commands The command commonly used for cutting is d. This command deletes text from the file. The command is preceded by an optional count and followed by a movement specification. If you double the command by typing dd, it deletes the current line. Here are some combinations of these: x : delete character under the cursor. d^ : deletes from current cursor position to the beginning of the line. d$ : deletes from current cursor position to the end of the line. dw : deletes from current cursor position to the end of the word. 3dd : deletes three lines from current cursor position downwards. u : undo the last change to the file. Typing u again will re-do the change. Exercise 4.5 Insert a string at the bottom of the line This is a tutorial of vi editor!! Then, make sure you are in command mode. Move the cursor to the first character of this line and type x. What is the result? Then type u in command mode. What did you see? 1. "x": "T" in the first character of the first line is deleted. 2. "u": "T" is recovered. Page 10

Exercise 4.6 Move the cursor to the beginning of this document. Try the following commands: dw, d$, and 2dd in sequence. Drop down what happen when executing each command. dw: "This" of "This is a test." is deleted. d$: All content of the first line is deleted. 2dd: All content of the file is deleted. "-- No lines in buffer--" is displayed in the bottom of screen. ii. Copying and pasting commands By now, you should be comfortable with moving around a file rapidly, editing text, cutting and deleting text. It is time to work on a more complex text manipulation: copying and pasting text. y : copy selected characters to the system buffer p : paste the system buffer to the current cursor position Exercise 4.7 Use vi to edit testcat.txt. Move the cursor to the first line of this document. Insert the three lines of text This is the first line.<return>this is the second line.<return>this is the third line.<return> Go to command mode. Move the cursor to the beginning of the second line. Type v to go to visual mode. What is the statement at the bottom of the screen? -- VISUAL -- Exercise 4.8 In the visual mode, you can move your cursor to select the text. When you selected the text area, the front and background color will been changed inversely. Select the second line and then, type y key. Move the cursor to the bottom of the document. Type p key. What is the result? This is the first line. This is the second line. This is the third line. This is the second line. iii. Search commands The vi editor has two kinds of searches: string and character. For string search, the / and? commands are used. When you use these commands in the command mode, the command just typed will be shown on the bottom line. You should then type the string to look for. These two commands differ only in the direction where the search takes place. The / command searches forwards (downwards) in the file, while the? command searches backwards (upwards) in Page 11

the file. The n and N commands repeat the previous search command in the same or opposite direction, respectively. Exercise 4.9 Move the cursor to the first line of this document. Type /the*. What is the result? How to search the next the* downward? 1. All "the" and "th" is highlighted. 2. "/"<return> The search pattern /the* will search for any combination of words beginning with th and followed by zero to multiple numbers of e. In other words, both th, the, thee, theee, etc, will be found out. One may ask why could not we just search for th? What is the purpose of e* while an absence of e would still match anyway? Now suppose we are going to search for words in the form like thre, there, theere, theeere, etc, then we have to use the pattern the*re. If we use th as the search pattern, we will obtain many other more irrelevant matches. Exercise 4.10 Open a file called testvi.txt in your home directory. Use vi to type the following paragraphs. You are requested to type the text in the same format as what is shown below. Remember to type your name and student number onto the file. ---------------------The paragraphs that you need to type start from the next line------------------- -------------------------------------------------------------- Name:<your own name> Student no.:<your own student number> The vi editor has two kinds of searches: string and character. For string search, the / and? commands are used. When you use these commands in the command mode, the command just typed will be shown on the bottom line. You should then type the string to look for. These two commands differ only in the direction where the search takes place. The / command searches forwards (downwards) in the file, while the? command searches backwards (upwards) in the file. The n and N commands repeat the previous search command in the same or opposite direction, respectively. --------------------------The End----------------------------- 5. File Access Permission Linux is a multiuser system. The files of all users are stored in the same file structure. A mechanism is provided such that one user is restricted from accessing the files of other users. In fact, each file is associated with access permission. Based on such permission, security to users files can be achieved. Page 12

Exercise 5.1 Go to / and use ls l to see the details of the directory /boot. Who are the owner and the owner group of /boot? owner: root owner group: root Take note that the root in the field of owner represents the directory is owned by the user root while the owner group of this directory is the user group root. There can only be one user root but there can be multiple users belong to the user group root. Exercise 5.2 Give the current access permission of /boot. rwxr-xr-x Take note that the execution permission is required in order to gain access to a directory. Without the execution permission, you will not be able to enter the directory. This execution right bears a different meaning to directory and an ordinary file. An execution right to an ordinary file means that this file is an executable program and who have its execution right can execute it. Exercise 5.3 Change to your home directory. Use ls l to see the details of guestsub. Who are the owner and the owner group of guestsub? Give the current access permission of guestsub. owner: guest owner group: guest access permission: rwxrwxr-x Exercise 5.4 Copy the testcat.txt in your home directory to /boot. Can you do so? If not, why? What is the error message? It can t. It is because /boot does not give the right for other group user to write. Error message: cp: cannot create regular file /boot/testcat.txt : Permission denied. Exercise 5.5 Use the command chmod to change the access permission of guestsub to 600 (use man to see how to use chmod). Try to use cd to change the current directory to guestsub. Can you do so? Why not? What is the error message? It can t. It is because after changing the access permission to 600, even the user himself does not have the execution right. Hence he cannot get into that directory. Error message: bash: cd: guestsub: Permission denied. 6. Process Management Linux is a multitasking system. It means that multiple programs can be executed at the same time. Since all programs must be executed by the CPU and very often there is only one CPU in a computer system, a mechanism is provided by Linux to schedule the work that should be executed by the CPU at a particular time. A program that is claimed Page 13

to be executing is called a process. A user can keep track of the processes in a Linux system by using the command ps. Exercise 6.1 Open two terminals on Linux. In each terminal, type the command ps. Give the process number of the bash process in both terminals. Why they are different? pts/0 1205 pts/1 1232. They are different because they are in fact two different processes although having the same name. A different process number is essential to the identification of a process since both of these processes are created from the same program /bin/bash. There are no other means for us to identify them except the process number, since they may be created by the same user at the same time. Exercise 6.2 Start the Mozilla web browser "mozilla" and type ps again in both terminals. Can you see the process of Mozilla web browser? If not, why? They can t. It is because ps by default only show the processes running in its terminal. Mozilla uses a window to execute the program. Hence it is not a process running on either of the terminal. Exercise 6.3 Try to use the command ps Al. Can you see the processes of Mozilla? What are their process numbers and state (check your course notes for the meaning of state)? From the output of the command, name one process that is in running state. There are 2 processes. Their process numbers are 1258 and 1272. They are sleeping. Exercise 6.4 Use the command kill to kill the processes of Mozilla. Give the command you have used. What did you see after killing their processes? kill 9 1258 and kill 9 1272. The Mozilla window disappears. The kill command is not only capable of killing a process. Actually, its main function is to send a signal to another process. The command kill -9 1258 is indeed a command to send the signal 9 to process 1258. Now, what is signal 9? You may look it up by man signal S 7. In the table, you will find that signal 9 means SIGKILL and its action is AEF, which are Default action is to terminate the process, Signal cannot be caught and Signal cannot be ignored. It means that any process must accept this signal without ignoring it, once received, it has to terminate. Therefore, issuing this SIGKILL signal to any process must kill it. DL/ENG224_Lab2.doc Aug.04 Page 14