<<
Home , Vi

______

THE HONG KONG POLYTECHNIC UNIVERSITY Department of Electronic and Information Engineering

ENG224 Information Technology Laboratory 1 Linux Shell Commands and vi Editor

Objective: To practice the usage of Linux shell commands and vi editor

Equipment: PC with Ubuntu Linux 5 preloaded. vi editor.

Methodology: Start up the system. Login the system with username student. Consult your tutor for the password required. Answer all the questions in this lab sheet.

1. Introduction

What is a shell?

The shell is the line interface to Linux. It is similar to the Microsoft DOS prompt as known to Windows users, but is much 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 , such as DIR, COPY, and ATTRIB. These commands provide the base on 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 , the concept is different. A command is any executable . 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.

Open a console window by pressing the upper-left corner button "Applications", "System Tools" and then "Terminal" (Not Root Terminal). You may also open a new terminal by a single right click on the desktop, then choose Open terminal from the pop up menu. A terminal window similar to the one on Figure 1 will be shown.

Page 1 ______

Figure 1 The Terminal in Ubuntu Linux

2. Common Linux Commands a. the usage of commands

If you don’t know how to use the Linux shell commands, you can use man (which stands for MANual) command to show you the usage of them.

Usage: man Example: man (press q when you finish reading the manual of the command ls) b. Finding out where you are

The command (which stands for Present Working Directory) is the one of the basic commands in Linux. By typing the command and hitting Enter, you will be informed of which directory you are currently in.

Usage: pwd

Exercise 2.1 “pwd” in the command line. What is the result?

______

Page 2 ______c. Change directory

The command allows you to change your current directory to any accessible directory on the system. Usage: cd Example: cd /usr/X11R6/bin

Note: using the command cd without applying any parameter will bring you back to your home directory. d. Listing the contents of a directory

The ls command can be used to view the contents of the current directory. Usage: ls [option] Example: ls –al /

Exercise 2.2 Type “cd /” and “ls” in the command line. What is the result?

______

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 .. ?

______e. Find, and

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 Usage of grep command: grep Usage of cat command: cat

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 of cat.”, and then press crtl-d. Type “cat testcat.txt” again. What do you see?

______

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.

Page 3 ______

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 find to find out their meanings)?

______

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?

______

Exercise 2.7

Go to “/” directory by the command "cd /" 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.)

______

3. File Management Linux Commands a. Copying files and directories

To copy files, you can use the or commands. The command line “mv file1 file2” allows one to move file1 to file2. After the move, file1 will no longer exist. So functionally it is similar to renaming a file in DOS. Command 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; however if it does exist, it will be overwritten: Usage: cp file1 file2

There is NO undo command in Linux, so accidentally overwriting an important file would probably you pull your hair out. 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 to 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

Page 4 ______

Exercise 3.1

Change to your home directory by using the "cd" command. Create a subdirectory called “studentsub” using the command “ studentsub”. Copy the files testcat.txt and list.lst to studentsub. While staying your home directory, copy ./studentsub to another directory called ./studentsub2? Give the command that you have used. ______

Exercise 3.2

Rename the “studentsub2” directory to “studentsub3” using mv. Give the command that you have used. ______b. Deleting files and directories

The 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 : 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 can cause a lot of trouble with rm -r if you are not careful. Therefore, it might be a good practice to use the -i option when deleting a directory with its contents so that you would be prompted before each file in the directory was deleted: Usage: rm -ir dir1

Notice: Be careful with the rm command! Linux does not have any undo commands, and it does not put files into Trash where you can recover them later. Once you have deleted a file, it is permanently deleted from the file system.

Exercise 3.3

Delete the directory “studentsub3” inside your home directory. Give the command that you have used. ______

Page 5 ______

4. vi Editor a. Introduction to the 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 provided by different distributions of Linux, learning vi is still important because in many situations, such as for server configuration, the graphical is not available. Text editing has to be done with a command-based editor like vi. This tutorial is written to 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 Example: vi test.txt

When you start vi for the first , 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]

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? ______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 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. ” and hit ENTER. What is the statement showing at the bottom of the editor?

______

Page 6 ______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 key. What is that warning statement shown??

______

Note: 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 will not let you quit. To ignore this message, the command to quit vi without saving is :q!. This lets you vi without saving any of the changes.

Note : 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 :. 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? ______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 , 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 the 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? ______

Page 7 ______

Exercise 4.6

Now make sure you have only 3 lines "This is a test.", "This is a test of cat." and "This is a tutorial of the vi editor!!" in your open file. Move the cursor to the beginning of this document. Try the following commands: dw, d$, and 3dd in sequence. Jot down what happen when executing each command. ______

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 : the system buffer to the current cursor position

Exercise 4.7

Use vi to edit testcat.txt. Remove all its content. Move the cursor to the first line of this document. Insert the three lines of text “this is the first line.this is the second line.this is the third line. ” 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? ______

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 be inverted. Select the second line and then, type “y” key. In the insert mode, start a new line at the bottom of the document. Type “p” in the command mode. What is the result? ______

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 search 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.

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? ______

Page 8 ______

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: Student no.:

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 multi-user system. The files of all users are stored under the same file system. A mechanism is provided such that one user is restricted from accessing the files of other users. In fact, each file is associated with an access permission. Based on such permission, security to users’ files can be achieved.

Exercise 5.1

Go to / and use ls –l to see the details of the directory /boot. are the owner and the owner group of /boot? ______

Exercise 5.2

Give the current access permission of /boot. (Hint: look for the combination of "r" "w" and "x") ______

Exercise 5.3

Change to your home directory. Use ls –l to see the details of studentsub. Who are the owner and the owner group of studentsub? Give the current access permission of studentsub. ______

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? ______

Page 9 ______

Exercise 5.5

Use the command to change the access permission of studentsub to 600 (use man to see how to use chmod). Try to use cd to change the current directory to studentsub. Can you do so? Why not? What is the error message? (Hint: r = 4 , w = 2 , x =1, 600 means rw------) ______

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 to be executing is called a process. A user can keep track of the processes in a Linux system by using the command .

Exercise 6.1

Open two terminals on Linux. In each terminal, type the command “ps”. Give the process number (PID) of the bash process in both terminals. Why they are different? ______

Exercise 6.2

Start the Fire-Fox web browser by typing "mozilla &" at the command line and type “ps” in the terminal. a) Can you see the process of Fire-Fox web browser? On another terminal, type "ps", b) Can you see the process of Fire-Fox web browser? If not, why? ______

Exercise 6.3

Try to use the command “ps –Al”. Can you see the processes of Fire-Fox? What is its process numbers? ______

Exercise 6.4

Use the command “” to kill the processes of Fire-Fox. Give the command you have used. What did you see after killing their processes? (Hint: use "man kill" and "man signal -S 7" to look for help) ______

-- The End --

Page 10