Вы находитесь на странице: 1из 58

UNIX Talk 2010 Reference Notes

Dedicated to students of CENG & CSCI 3150. (the same handout as the one for CEG UNIX Talk, Jan 2010) Last update: September 13, 2010. Prepared by: Dr. WONG, Tsz Yeung.

CONTENTS

Contents
1 UNIX and Our Department 1.1 Your Rights - How Much Data Can You Own? . . . . 1.1.1 Keep an eye on your quota . . . . . . . . . . . 1.2 Computing Facilities - UNIX-Like Workstations . . . 1.2.1 Console login . . . . . . . . . . . . . . . . . . 1.2.2 Remote login . . . . . . . . . . . . . . . . . . 1.3 Network Services in Our Department . . . . . . . . . 1.3.1 Walkthrough: SSH login from a Windows PC 1.3.2 Web service: hosting your personal homepage 1.3.3 Email and email forwarding . . . . . . . . . . 1.4 A Short Summary . . . . . . . . . . . . . . . . . . . . 2 File Processing 2.1 File & Directory . . . . . . . . . . . . 2.1.1 Create and Remove File . . . 2.1.2 View Files Content . . . . . . 2.1.3 Move and Copy Files . . . . . 2.1.4 File Type . . . . . . . . . . . 2.1.5 Create and Remove Directory 2.1.6 Change Directory . . . . . . . 2.1.7 View Directorys Content . . 2.1.8 Special Directories . . . . . . 2.1.9 Redirection and Pipe . . . . . 2.2 File Permission . . . . . . . . . . . . 2.2.1 Read, Write, Execute Modes . 2.2.2 Changing Modes . . . . . . . 2.2.3 Remarks . . . . . . . . . . . . 3 Process Management 3.1 Display Process Details . . . . . . 3.2 Kill Process . . . . . . . . . . . . 3.3 Foreground and Background Job 3.3.1 Create Background Job . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 6 9 10 10 10 12 12 19 19 20 21 21 21 22 23 24 26 26 27 27 29 31 31 32 33 34 34 34 35 36

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . . . . . . . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

CONTENTS 3.4

Kill Background Job . . . . . . . . . . . . . . . . . . . . . . . 36 37 37 37 38 38 39 39 39 39 39 40 40 40 40 40 40 41 41 41 42 42 43 43 43 44 44 45 45 45 46 46

4 Printing in Department 4.1 Printing Files . . . . . 4.2 Printing Queue . . . . 4.3 Delete Print Job . . . 4.4 Print Quota . . . . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

. . . .

5 UNIX Tools 5.1 Internal Communication . . . 5.1.1 talk . . . . . . . . . . 5.1.2 write . . . . . . . . . . 5.1.3 nger . . . . . . . . . . 5.2 Password and Personal Details 5.2.1 passwd & yppasswd . 5.2.2 chfn . . . . . . . . . . 5.3 Text Editor . . . . . . . . . . 5.3.1 VI and VI IMproved . 5.3.2 PICO . . . . . . . . . 5.4 Internet . . . . . . . . . . . . 5.4.1 SFTP . . . . . . . . . 5.4.2 PINE and ELM . . . . 5.4.3 TIN . . . . . . . . . . 5.4.4 LYNX . . . . . . . . . 5.4.5 WGET . . . . . . . . . 5.5 Compression and Archives . . 5.5.1 ZIP, UnZip and Zipinfo 5.5.2 GZIP . . . . . . . . . . 5.5.3 Tar and GNU Tar . . . 5.6 Image Manipulation . . . . . 5.6.1 XV . . . . . . . . . . . 5.6.2 GIMP . . . . . . . . . 5.7 Document . . . . . . . . . . . 5.7.1 GV and GhostView . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

. . . . . . . . . . . . . . . . . . . . . . . . .

CONTENTS 5.7.2 ACROREAD 5.7.3 StarOce . . Dictionary . . . . . . Manual Page . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

4 46 46 47 47 48 50 51 55

5.8 5.9

A Installing the SSH Client Program B SHELL command C VI command D Pico Command

1 UNIX AND OUR DEPARTMENT

UNIX and Our Department

The rule of thumb to survive in our department is to know and to work


with UNIX. UNIX-like systems are everywhere in this department. Mac is a UNIX-like system; Linux is a UNIX-like system; and UNIX itself.

Email Server Web Server gateway (gw.cse)

External Network Internal Network Using SSH

Mac PCs Mac PCs Mac PCs

UNIX Storage Servers

Unix Unix Workstation Unix Workstation Workstation

Using SSH Mac PCs Mac PCs Linux PCs

Samba Servers (smb1, smb2, etc) H Drive Windows Windows PC PC

Figure 1: Networking and services in our department.

Figure 1 shows you the fact that nearly all services in our department are centered around the UNIX storage servers. They contain your user

1 UNIX AND OUR DEPARTMENT

accounts and your emails (and, of course, there are tons of other things that you may not be interested). If you are still not convinced that you do not need any knowledge about UNIX systems, please take a look at the following list of inconvenient truths. The Windows PCs in our department only allow you to access les stored in your user account, but not other services such as checking your disk quota. You cannot use the H drive in the Windows PCs in your home. Only SSH programs can help you, and you may also need those UNIX commands, too. You cannot check your print quota in the Windows PCs. You can only do so in UNIX. For some courses, you can only work in UNIX-like systems, e.g., CSCI & CENG3150, CENG3430, CENG3470, CSCI3170, CSCI5470, etc. So, why not learning how to work with the UNIX system?

1.1

Your Rights - How Much Data Can You Own?

As a matter of fact, you are entitled to host more than 2GB of data in our department. However, the access to the data is quite restricted. Figure 2 has shown the storage space and the corresponding ways to access them. UNIX personal account. This is the primary place for you to store your source code, lecture notes, etc. The good about this storage is not only big, but also is a shared place between the UNIX workstation and the Windows PCs. In order words, after you have copied a source code le from a Windows PC to the personal account, you can immediately modify it in a UNIX workstation!

1 UNIX AND OUR DEPARTMENT

Email 50MB

Directory: /var/mail/[login ID]

Unix Unix Workstation Workstation Workstation Directory: /public UNIX Personal Account 2GB Your home directory

H Drive

/public 100MB

Windows Profile 50MB

N Drive

Windows Windows PC PC in lab

Figure 2: The storage service provided to every user in the department.

Windows prole. The second attractive place is the Windows prole. It stores your Windows account data: your desktop, your wallpaper, etc. With this storage, you can log in at dierent Windows PCs but have the same desktop and the same wallpaper. However, the quota is just not enough and will easily be used up. Therefore, remember to always move les in your desktop to the UNIX personal account. Email. Emails are stored inside the UNIX storage system and therefore can only be accessed through UNIX workstations only, not Linux nor Mac. E.g., if you were the user tywong, then you can nd your email at the location /var/mail/tywong. It is always interesting to know that all your emails are stored as ONE

1 UNIX AND OUR DEPARTMENT Story-Telling Time - the /public You may wonder why there is a public, sharable storage with 100MB space only. As a matter of fact, there are historical reasons. In the late 90s, every undergraduate student was only given a UNIX personal account of 5MB space only. Now, think about it: what if I need to process a data le of 5MB? What if I need to produce an output of 5MB? (Yes, there were such needs. In my data structure course, I was told to read in a 1MB le. I needed to build some data structures over the data and I also needed to output it.) To solve such a problem, a publicly accessible, huge storage place is born. Nevertheless, people started to abuse such a place, e.g., sharing MP3, games, etc.

FILE ONLY. Those who are interested can take a look inside and guess why it is so dierent from those you found using the web mail service. /public. The way that we name is because of its location: in the workstations, the location of the 100MB-space is /public. The special thing about this storage is that everyone can share les there. Occasionally, you will nd some good stus there. Of course, the /public directory will be cleaned up everyday. Your Responsibility. A user shall not store any materials with oensive content in his/her computer account and /public in Unix workstations, or any materials that infringe the laws regarding Copyright, Patent and Trademark.

1 UNIX AND OUR DEPARTMENT For other responsibilities, you are required to read the following page: http://wiki.cse.cuhk.edu.hk/tech/regulations/main 1.1.1 Keep an eye on your quota

Now, you know that you are only given limited resources. It is your responsibility to keep track on your disk usage. But, what if you have used up your disk quota? If you have used up your disk quota, you cannot add any new data/les into your account. Whats the big deal? You may ask. The problems are: You cannot save any work in-progress. You cannot compile any programs. Well, the above problems are good sources of strange bugs while you are working on your assignments. To monitor your quota in your UNIX personal account and /public, please invoke: quota -v The above command displays your current usage and the quota imposed. For the usage of your email storage, you need another way: du -k /var/mail/[your login name] The above command measures how many KBytes you have used to store the emails.

1 UNIX AND OUR DEPARTMENT

10

1.2

Computing Facilities - UNIX-Like Workstations

The lists of UNIX-like workstations are given as follows. Machine Sun Ultra Linux Mac OS Solaris 8 Ubuntu 8.04 / 9.04 server Ubuntu 10.04 64-bit server Mac OS X / Darwin Hostname sparc1 - sparc30 linux1 - linux9 linux10 - linux15 mac1 - mac12

There are two ways to go into the machines and both ways require your UNIX credential.

What is a credential? It means your identity and is usually presented as the (login ID, password) pair.

1.2.1

Console login

The phrase console login means you log in the workstation in front of the workstation. However, except the Mac workstations, you can hardly nd a console in the laboratory; They are hidden by our system administrators. An analogy. You are doing the console login action in front of your Windows PC everyday.

1.2.2

Remote login

On contrary, remote login means that you log in the workstation from another workstation/PC (not in front of the target workstation). There are several forms of remote logins. Windows: remote desktop.

1 UNIX AND OUR DEPARTMENT How many user accounts do you have? Since the rst day you entered our department, you had been given two accounts: the UNIX account and the Windows account. The two accounts are dierent although they have the same login name. In other words, when you changing the password of the Windows account, you are not changing the password of the UNIX account, and vice versa. The UNIX account governs many things: - access to workstations; - access to printers; - access to email service; - access to the department VPN service; - access to some course web pages; However, the Windows account only governs the access to Windows PC only.

11

UNIX-like workstations: ssh and telnet (depreciated because it is not secure). In most of the cases, we will use a Windows PC to remotely log in a UNIX workstation using a SSH client program. All UNIX workstations, Mac workstations, and Linux workstations support the SSH service. In the next section, we will show you how to do so.

1 UNIX AND OUR DEPARTMENT

12

1.3

Network Services in Our Department

For further information, please refer to the following link: https://www.cse.cuhk.edu.hk/corner/tech/guide/network.html Mainly, there are three network services you will be interested: Remote login using SSH, Web server, and Email. 1.3.1 Walkthrough: SSH login from a Windows PC

The word SSH is a abbreviation of the name Secure SHell and we usually use SSH as a verb. E.g., you cannot ssh to a Windows PC. Figure 3 shows how to ssh from your home to a UNIX machine. We describe the steps involved in the following context. Installing the SSH client program. Sorry, on behalf of Uncle Bill, all versions of Windows do not have any SSH client program pre-installed. Dont worry. Our university has purchased the license of a SSH client program. Every Windows PC in our department has such a program installed. Nevertheless, if you are connecting from home, you will need the installation. For the details, please refer to the Appendix of this handout. SSH to the gateway rst if you are outside CSE network If you are using a connection outside the CSE department, that means: Commercial ISPs, such as PCCW and HKBN;

1 UNIX AND OUR DEPARTMENT

13

Windows PC in home

SSH to gw.cse.cuhk.edu.hk

gateway (gw.cse)
SSH to a workstation in the gateway

Unix Unix Workstation Unix Workstation Workstations Mac PCs Mac PCs Mac PCs Internal Network

Mac PCs Mac PCs Linux PCs

Windows PC in Lab

SSH to a workstation directly.

Figure 3: How and where to ssh?

CUHK network service, including the ClassNet, the Resnet, the VPN, and the CUHK Wi service; ERGWAVE Wi service; then, you must go through the department gateway in order to remotely log in the workstations. In Figures 4-8, step-by-step guidelines will be given. Step 1, Figure 4. Launch the SSH program and click the Quick

1 UNIX AND OUR DEPARTMENT Story-Telling Time - the gateway Before 2000, all workstations and Windows PCs in our department were directly connected to the outside world. I used to remotely control a Windows PC in the lab using VNC from my home. Nevertheless, this measure gave a window to hackers and we did suer from an attack. After such an attack, our department tightened our network. The gateways were installed since that attack.

14

Connect button. Step 2, Figure 5. Type in the name of the department gateway and your login name. Step 3, Figure 6. Type in your UNIX password. Step 4, Figure 7. If everything is smooth, you will be inside the gateway machine. Else, you will be prompted to enter the password again. It is because you have typed either a wrong login name or a wrong password. Now, you are one step away from logging in the workstation. The next step is to type the following command: <myHome> ssh sparc1 The above command means that you want to ssh to the UNIX workstation sparc1. Next, you will be asked to enter your password. Yes, you have to type in the same UNIX password again. tywongsparc1s password: Step 5, Figure 8. Great! You are now inside the UNIX workstation sparc1. Congratulations!

1 UNIX AND OUR DEPARTMENT

15

Figure 4: Step 1. Click the Quick Connect button.

Figure 5: Step 2. Host Name = gw.cse.cuhk.edu.hk; User Name = your login name.

Figure 6: Step 3. Remember, type your UNIX password, and it is casesensitive.

1 UNIX AND OUR DEPARTMENT

16

Figure 7: Step 4. Now, you are inside our department gateway.

Figure 8: Step 5. Congratulations! You are in the UNIX workstation sparc1.

1 UNIX AND OUR DEPARTMENT SSH to the workstations directly if you are inside CSE network Then, the procedure becomes very easy: just skip the gateway.

17

Step 1. Launch the SSH program and click the Quick Connect button. Step 2. Type in the name of the workstation you want to reach, e.g., sparc1 or mac1, together with your login name. Right, you can omit .cse.cuhk.edu.hk (and thanks for the work done by our administrators). Step 3. Type in your UNIX password. Step 4. Bingo! You are now inside the UNIX workstation. That is not the end of the SSH program, you can do many things: Use the UNIX environment with commands, and such an interface is called the command line interface, CLI for short. Use another feature of the SSH program to transfer les to and from your UNIX account. Figures 9 and Figure 10 have shown you how.

1 UNIX AND OUR DEPARTMENT

18

Figure 9: You can launch the Secure File Transfer by clicking New File Transfer.

Figure 10: Left Hand Side: your local disks. Right Hand Side: the home directory of your UNIX account.

1 UNIX AND OUR DEPARTMENT 1.3.2 Web service: hosting your personal homepage

19

To host your personal homepage, the following steps can help you. 1. Create the directory www in your UNIX account. 2. Mark the directory as readable by all people with the following command: chmod 711 www (We will say more about the chmod command later.) 3. Then, create your homepage inside that directory. 4. Last, you can view your homepage. E.g., if your login name were tywong, then the location of your homepage would be: http://www.cse.cuhk.edu.hk/~tywong/ 1.3.3 Email and email forwarding

By the way, do you know that you have a department email address E.g., if your login name were tywong, then your email address would be: tywong@cse.cuhk.edu.hk A truth: most tutors and teachers try to contact you using your department email address only. Therefore, in order to guarantee you are not going to miss any emails from our department, our system administrator has set up an email forwarding service. The conguration le about this service is called .forward and it is preinstalled inside your home directory of your UNIX account. The .forward le is a plaintext le and an example content of the .forward is as follows.

1 UNIX AND OUR DEPARTMENT

20

chantaiman@gmail.com

The content species that every email sending to your department email address will be forwarded to the email address chantaiman@gmail.com. Can I disable the email forwarding service? Sure. To disable the service, you just have to remove the .forward le. How Can I check the department emails? The simplest way is to use the web mail system in the following link: http://webmail.cse.cuhk.edu.hk/ More advanced ways will be using the UNIX commands and we will show you later.

1.4

A Short Summary

This is not the end of the basics of the usage of UNIX in our department. For more details, you are welcome to visit the following site: http://www.cse.cuhk.edu.hk/corner/ In the next chapter, we start introducing you the UNIX commands.

2 FILE PROCESSING

21

File Processing

In this section, we are going to introduce the most important thing in UNIX,
that is File. File is everything in UNIX including directory, hard disks, and even printers. In the following text, we would like to introduce the processing of les and directories. Including creation, removal, moving and copying, as well as permission policy in les in UNIX system.

2.1
2.1.1

File & Directory


Create and Remove File

- We can use the following methods to create les Using command touch touch is a command that can create empty les (an empty le is a le contains zero byte). If the le that is being touched already exists, it will only change the le access and modication times. To use touch: touch [lename] Using text editor Text Editor is a program that can save the text we type.

- To remove les, we can use the command rm. To invoke: rm [lename] rm can remove every le. So please be careful:

rm * can be dangerous !!!

2 FILE PROCESSING 2.1.2 View Files Content

22

In this subsection, we will talk about text le (or ASCII le). - Using Text Editor Text Editor not only can view a les content, it can also modify a les content. - Other than Text Editor cat can be used to display a les content (Not a pet like dog:). To invoke: cat [lename(s)] It can be used to concatenate several les into one. e.g. cat file1 file2 file3 > aLargeFile

more is a useful tool to browse through a text le. It is much better than cat. cat will dump out all the content. But, more can t the text le into pages. To invoke: more [lename] Below shows some simple command for more b f space enter / q page up page down page down go down a line search quit

less is the opposite of more (just kidding!!). less is a similar tool as more. It supports more commands than more. To invoke:

2 FILE PROCESSING less [lename] Below shows some simple command for less b f q / g G ng page up page down quit scroll up scroll down search Go to the rst line Go to the last line Go to the nth line

23

head can display the rst N lines of a le. It can be adjusted to display 1 to N lines. The following example display the rst 5 lines of the le aFile. e.g. head -5 aFile

tail is the opposite of head (This time is not a joke). It displays the last N lines of a le. e.g. tail -8 aFile 2.1.3 Move and Copy Files

- We can move a le from one place to another by using mv. We can also use mv to change a les name. To invoke mv (move): mv [source] [destination] e.g. mv 3150.cpp 3150/example.cpp This example moves 3150.cpp into the directory 3150 and the le name is changed to example.cpp.

2 FILE PROCESSING e.g. mv 3150.cpp 3150/ This example has the similar eect as the previous one, except that the le name is still 3150.cpp.

24

e.g. mv 3150.cpp 3150.cpp.backup This example changes the lename 3150.cpp into 3150.cpp.backup.

- We can copy a le to other places by using the command cp. To invoke cp (copy): cp [source] [destination] e.g. cp 3150.cpp 3150.cpp.backup This example copies a backup of 3150.cpp and names the new le as 3150.cpp.backup. e.g. cp 3150.cpp 3150/assg1/ This example copies 3150.cpp into the directory 3150/assg1. e.g. cp -R 3150/ passed course/ This example copies all the les inside 3150 including subdirectory into the directory passed course. The argument -R is needed if you want to copy all the sub-directories inside 3150. 2.1.4 File Type

- In DOS/Windows, the type of le is usually determined by its le extension. But in UNIX, the le extension is useless (or not signicant). - We have a very useful tool which can determine the le type easily. It is called le. To invoke le: le [lename]

2 FILE PROCESSING e.g. file hello.gif return: hello.gif: GIF file, v89

25

- Remember, le extensions in UNIX is not so important. Therefore, we need the le command to help us in determining le types. E.g., mv hello.gif hello.mp3 file hello.mp3 return: hello.mp3: GIF file, v89

2 FILE PROCESSING 2.1.5 Create and Remove Directory

26

Directory is an important le type in UNIX system. The above commands such as mv, cp, etc are also applicable since directory is also a le. However, its creation, removal as well as content are special.

- We can create a directory by using mkdir. To invoke: mkdir [directory name] e.g. mkdir unixTalk This creates a directory called unixTalk.

- We can remove a directory by 2 ways: 1. By using rmdir. To invoke: rmdir [directory name] This command fails if the target directory contains les. In this case, we should choose the second method. 2. By using rm -r. To invoke: rm -r [directory name] This command will ask for conrmation of the removal of all the les in the directory. 2.1.6 Change Directory

We can browse through the directory structure by a famous command. It is cd (stand for Change Directory). To invoke: cd [directory name]

2 FILE PROCESSING 2.1.7 View Directorys Content

27

As directory is also a le, we can read its content. However, directory is a special type of le, we need a special and famous command to read it. It is ls. To invoke: ls [argument] [directory or les] e.g. ls -l assignment This example will list the directory assignment in a long format. There are a lot of arguments for ls. Here, we describe some of them. -a -l -R List all entries including hidden les. List all entries in long format. List all les and sub-directories recursively.

2.1.8

Special Directories

We will describe 3 kinds of special directory: . (a dot), .. (two dots) and . Below shows a table of these special directory: . .. current directory parent directory home directory

e.g. ls -l . This command lists all the entries in current directory. e.g. cd .. This command browses to the parent directory.

2 FILE PROCESSING e.g. cd This command browses to the home directory of the current user. e.g. cd tywong This command browses to the home directory of tywong.

28

2 FILE PROCESSING 2.1.9 Redirection and Pipe

29

- In UNIX, we have 3 special (or default) les. They are namely STDIN (standard input), STDOUT (standard output) and STDERR (standard error). The following is the description of these 3 les. STDIN It stands for the input of a program. Typically, the standard input is the keyboard. STDOUT It stands for the output of a program. Typically, the standard output is the screen. STDERR It stands for the output of the error message(s) of a program. Typically, the standard output is also the screen. - We summarize these with the below analogy. STDIN STDOUT STDERR keyboard monitor monitor

- We can change the STDIN and STDOUT of a program by using redirection and pipe le 1. Redirection We have 3 kinds of redirections: >, < and >>. The > symbol is used in this way: [program] > [le] The STDOUT of the program will be replaced by the target file. If the file already exists, we add a ! after >. e.g. ls -l >! output This command will create(or replace) the le with name output. The le output will contain the result of ls -l.

2 FILE PROCESSING The symbol >> is used in this way: [program] >> [le]

30

This symbol is similar to >. However, instead of erasing all the data in le, >> appends the STDOUT to le. The < symbol is used in this way: [program] < [le] The STDIN of the program will be replaced by le, i.e. the content of le will become the input of program. To summarize, please have a look of the below table. > Replace STDOUT and ush to a le >> Replace STDOUT and append to a le < Replace STDIN by reading content of a le 2. Pipe File A pipe File is a symbol, |. The usage is: [program1] | [program2] It is a special le. It acts as a temporary storage to store the STDOUT of program1. This storage will then serve as the STDIN of program2. e.g. ls -l | more The ls -l will give its STDOUT to the pipe. Then, more will read the pipe as its STDIN. The result of this example is to generate a page-separated directory content.

2 FILE PROCESSING

31

2.2

File Permission

In this sub-section, we would like to discuss the permission policy of les in UNIX system. 2.2.1 Read, Write, Execute Modes

Below is a typical result of ls -l: -rw------1 tywong lec 11 Jan 1 00:00 test.c

The rst 9 character, i.e. -rw------are the permission that is set to the le. If all the states are on, the permission will be: -rwxrwxrwx The rst rwx stands for the permission for the owner of the le. The middle rwx stands for the permission for the group of the owner. The last rwx stands for the permission for other users. From the following example: -rw------1 tywong lec 11 Jan 1 00:00 test.c

We know the following information about this le. Owner Group Permission for User Permission for Group Permission for Other tywong lec Read, Write No permission granted No permission granted

The rw- permission of user means that the user is allowed to Read and Write the le. However, he/she is not allowed to eXecute the le. The users of the same group and other users are not allowed to Read, Write or eXecute the le.

2 FILE PROCESSING 2.2.2 Changing Modes

32

UNIX system has a very useful command to change permission of a le. It is chmod (change mode). The format is: chmod [absolute modes] [lename], or chmod [symbolic-mode-list] [lename]

1. Absolute Modes The absolute mode contains 3 numbers between 0 and 7. The calculation of the mode is carried out like this: For example, if we want to change to the following mode: rw- --- --- = 600 (absolute mode) We convert the permission into a 9 bits representation, i.e. rw- --- --- = 110 000 000 Then, we group the bits 3 into 1 group, and convert it into a base 8 number, i.e. rw- --- --- = 110 000 000 = 6 0 0 The mode calculation is completed, and then we can type: chmod 600 test.c to change mode. e.g. rw- r-- r-- = 110 100 100 = 644 e.g. rwx r-x r-x = 111 101 101 = 755

2 FILE PROCESSING 2. Symbolic Mode

33

This mode uses symbols for changing mode. For example, you want all people to view your photo. By absolute mode, you need to change mode to 644. But, using symbolic mode, it is like this: chmod a+r myPhoto.jpg a+r means that All(a) Add(+) Read(r) permission.

Or, you have written a program and want to share with all your Y09 classmates only. you can: chmod g+rx myProgram a+r means that Group(g) Add(+) Read(r) and eXecute(x) permission.

The following table shows the symbols used in symbolic mode. r w x a u g o + 2.2.3 Remarks read permission write permission execute permission all users owner(user) group other add permission(s) remove permission(s)

Directory should be EXECUTABLE if you want to cd to it. Directory should be READABLE if you want to ls it. Program must be EXECUTABLE.

3 PROCESS MANAGEMENT

34

Process Management

Process is another important component of UNIX system.

Every program which is executing is a process (you will learn a more formal denition later). In this section, we are going to introduce the management of processes.

3.1

Display Process Details

Whenever a process is created in UNIX, an unique ID will be created for the process also. It is called Process ID (PID). We can locate a process by searching the program name as well as PID. The following text introduces some of the commands and tools. top top is a tool that displays information about the processes including PID. It is an interactive one and updates the cpu information periodically. ps ps is also a tool that displays information about processes. But, it only shows the jobs that are running on the working terminal. However, if you type the command like the following, you can get all the processes PID that belong to you in a single machine. ps -u [username] w w cannot shows the PIDs of running processes. However, it can display all current active jobs in the machine.

3.2

Kill Process

There are cases that process will not terminate even if you use Ctrl+C. One of the method is to switch the machine o. However, this is FORBIDDEN

3 PROCESS MANAGEMENT

35

in our department as you will kill other peoples jobs also. UNIX is a nice system; it provides ways for you to kill those stubborn processes. kill kill sends signal to your target process in order to let it to rest in peace. The format is : kill [PID]

If the process is so tough that it is still not being killed, try to add an argument -9 to kill. kill -9 [PID]

top top has a option for users to kill processes. Here, we provide some options of top. k kill u list specic users job

3.3

Foreground and Background Job

There are cases that you are running a job which consume a long period of time (several hours) to nish. You have dated your boy/girl friend, you cannot wait for it to nish. However, if you logout your machine, the terms and jobs will be killed too!!!! What should you do?? No panic!! Those ordinary jobs that you are running are called foreground jobs. UNIX has another type of job which is called background job. This kind of job can be run behind the login terminals.

3 PROCESS MANAGEMENT 3.3.1 Create Background Job

36

To create background job, there are 2 ways. Create a Background One To create a background job, just type & at the end of the command. e.g. /usr/local/bin/netscape4.7 & Change Foreground to Background

1. Type Ctrl+Z in the terminal that the foreground job is running. 2. The job is now changing to a suspended job. 3. Type bg (Back-Ground) onto the terminal that the suspended job is on, a background job is created.

3.4

Kill Background Job

1. You can type fg in the terminal that you create background jobs. Then, you can press Ctrl+C to kill it. If the term does not exist, then 2. You can use the methods introduced in the previous subsection.

4 PRINTING IN DEPARTMENT

37

Printing in Department

Our department has provided us 7 postscript laser printers and 2 dot matrix
printers for us to use. Their locations are: Printer Name ps ps2 ps3 ps4 cps cps1 cps2 openlab declab Location SHB924 SHB924 SHB904 SHB122 SHB122 SHB924 SHB924 SHB904 SHB924 Type Laser Laser Laser Laser Color Laser Color Laser Color Laser Dot Matrix (Plaintext only) Dot Matrix (Plaintext only)

4.1

Printing Files

We can print les in UNIX with this command: lpr -P[printer name] [lename]

This command can only print plain text les and postscript les (usually with extension .ps).

4.2

Printing Queue

As the printers are shared among several hundreds of classmates, we will need to queue up in order to get the printing service. Once you send the job to the printer, you can check the queue of the printer: lpq -P[printer name], or showprint

4 PRINTING IN DEPARTMENT

38

4.3

Delete Print Job

If your job is not processing (or printing), you can dequeue your job from the printer by: lprm -P[printer name] [Job ID] [username]

4.4

Print Quota

Printing in our Department costs money. Initially, each undergraduate has 50 quotas. After each semester, 50 more quotas will be added. Each quota costs $0.30. Below show the charges for printing. ps ps2 ps3 ps4 cps cps1 cps2 openlab declab (Duplex) 1 quota (Duplex or non-duplex) 1 quota (Duplex or non-duplex) 1 quota (Duplex) 1 quota 5 quota 5 quota 5 quota free of charge free of charge

To check how many quotas are still left, use this: printquota

5 UNIX TOOLS

39

UNIX Tools

In this section, we introduce some useful tools in UNIX.


5.1 Internal Communication

This category of tools is for communicating with people within the same network. 5.1.1 talk

This tool allows 2 people to chat with each other. But, it only allows 2 people. talk [username]

5.1.2

write

This tool allows an user to send message to other users. write [username] [terminal]

5.1.3

nger

It is not a communicating one, but it allows users to see other users information nger [username]

e.g. finger tywong Login name: tywong In real life: Wong Tsz Yeung Directory: /uac/lec/tywong Shell: /bin/tcsh On since Jan 4 00:23:02 on pts/1 from 137.189.90.237 Unread mail since Sun Jan 3 23:53:43 2010 Plan: I teach OS.

5 UNIX TOOLS

40

5.2
5.2.1

Password and Personal Details


passwd & yppasswd

You can change your UNIX password by using passwd. In our department, passwd and yppasswd are the same program. To invoke: passwd or yppasswd 5.2.2 chfn

You can change your finger information by using chfn. To invoke: chfn The above programs need your current UNIX password in order to change your personal informations.

5.3

Text Editor

Text editor is an important tools. It enables us to compose a text le such as email, programs, etc. There are a lot of text editors in UNIX. We introduce two useful text editors. 5.3.1 VI and VI IMproved

VI is nearly the most powerful text editor in UNIX system, and Vi IMproved (VIM) is the VI-enhanced version. VI has 2 modes, one is Command Mode and another one is Edit Mode. The detailed commands will be shown in the appendix. To invoke VI or VIM: vi [lename] or vim [lename] where the lename can be omitted. 5.3.2 PICO

PICO is a less powerful text editor than VI or VIM. However, it is easy to use and has a lot of useful commands. The detailed commands will be shown in the appendix. To invoke PICO:

5 UNIX TOOLS pico [lename] where the lename can be omitted.

41

5.4

Internet

This section introduces a lot of tools that are related to the Internet. 5.4.1 SFTP

SFTP stands for Secure File Transfer Protocol. The tool allows you to login to the target host, and can download or upload les from that host. To invoke sftp: sftp [hostname or IP address]

Below shows some commands of sftp bye cd [pathname] lcd [pathname] get [lename] mget [lename(s)] put [lename] mput [lename(s)] ls prompt close connection change the directory in the remote host change the directory in the local host download one le download multiple les upload one le upload multiple les list le entries in hosts turn prompting o

5.4.2

PINE and ELM

PINE and ELM are mail reader and composer. You can read and reply your email in these 2 programs. PINE is a program for Internet News and Email. It is a more convenient Email program. It supports attachment also. To invoke PINE: pine

5 UNIX TOOLS

42

ELM is a not as user-friendly as PINE. However, it supports command line to send mail. To invoke ELM: elm [arguments] [email address] Notes: you can omit arguments and email address

e.g. elm -s "Just to Say HI" tywong@cse.cuhk.edu.hk This will send an email to tywong@cse.cuhk.edu.hk with a subject Just to Say HI.

5.4.3

TIN

It is an Internet News program. It allows you to read, reply and subscribe to newsgroups. To invoke tin: tin Below shows some commands of tin. y s u w f Browse groups on server or Browse subscribed groups subscribe group unsubscribe group post a news (or thread) followup in a thread go down one level in the newsgroup go up one level in the newsgroup scroll up scroll down

5.4.4

LYNX

It is a famous text mode web program. It allows you to surf the Internet, but not Javascript support. To invoke lynx: lynx [hyperlink]

5 UNIX TOOLS 5.4.5 WGET

43

It is a very handy tool that allows you to download stus with hyperlinks. It also provides some great features, such as downloading from a list of hyperlinks and recursively downloading a web site.

wget [hyperlink] e.g. wget http://www.cse.cuhk.edu.hk/index.html This command downloads the le index.html from our department. e.g. wget -i list.txt This command instructs wget to download from the list of URLs stored in list.txt. e.g. wget -l 2 -R http://www.cse.cuhk.edu.hk/ This command instructs wget to start downloading recursively from our department homepage, with a maximum depth of 2 levels.

5.5

Compression and Archives

There are a lot of compression tools in UNIX. We introduce some popular ones. 5.5.1 ZIP, UnZip and Zipinfo

ZIP is very popular in Windows. The UNIX Zip is compatible with the zip program in Windows environment. To use Zip: zip [destination le (zip le)] [le(s) to compress] unzip [destination le (zip le)]

5 UNIX TOOLS If you want to compress directory, then you need to argument -r. zip -r [destination le (zip le)] [directory(s) to compress] zipinfo is to view the content of a zip le. To invoke : zipinfo [zip le]

44

5.5.2

GZIP

GZIP is another compression tools in UNIX. To use GZip: gzip [lename] gunzip [lename] e.g. gzip hello.cpp A compressed le hello.cpp.gz will be created and hello.cpp will be deleted.

5.5.3

Tar and GNU Tar

Tar is not a compression tool, instead it is a tool to create an archive. An archive stands for a package of les. To use Tar or GNU Tar (gtar): tar -[arguments] [tar le] [lename(s)] gtar -[arguments] [tar le] [lename(s)] Below shows some arguments for tar and gtar. c t x f v z Create tar le View a tar les content Extract a tar les content Specify the name of a tar le View the name of each le being processed by tar Use Gzip in Creating and Extracting tar le

5 UNIX TOOLS e.g. tar -cf abc.tar test.cpp test.html test.c This command will create a tar le abc.tar. The tar le contains test.cpp, text.html and test.c. e.g tar -tf abc.tar This command will list the les contained in abc.tar. e.g. tar -xf abc.tar This command will extract the abc.tar. e.g. gtar -czf abc.tar.gz test.cpp test.html test.c This command will create a gzipped tar le named abc.tar.gz. e.g. gtar -xzf abc.tar.gz This command will extract the abc.tar.gz.

45

5.6

Image Manipulation

In this section, we introduce some image manipulation tools in UNIX. 5.6.1 XV

XV allows your to view, crop and print your images. It supports a lot of image format such as JPEG, GIF, TIFF, BMP, etc. To invoke XV: xv [image lename] where the image lename can be omitted.

5.6.2

GIMP

GIMP stands for GNU Image Manipulation Program. Or, it is the UNIX PhotoShop. It has the similar power as PhotoShop in Windows. To invoke GIMP:

5 UNIX TOOLS gimp [image lename] where the image lename can be omitted.

46

5.7

Document

In this section, we introduce a few document viewers and composers in UNIX. 5.7.1 GV and GhostView

GV and Ghostview are used to view PostScript les (usually with extension .ps). To invoke: gv [lename] ghostview [lename] where the lename can be omitted.

5.7.2

ACROREAD

AcroRead is the AcroBat Reader. It is capable of opening PDF le. To invoke it: acroread [lename] The lename can be omitted.

5.7.3

StarOce

StarOce is a oce suite implemented in JAVA. It can open nearly all kinds of MicroSoft Oce les. The setup of StarOce is a little bit troublesome. For reference, please go to: http://www.cse.cuhk.edu.hk/corner/tech/doc/ht/typeset/#soce

5 UNIX TOOLS

47

5.8

Dictionary

We have a dictionary installed in our UNIX system, the webster. To invoke: webster [word] The word can be omitted.

5.9

Manual Page

Last but not least, we introduce you the manual page in UNIX. To invoke: man [program name] e.g. man ls The manual page (or man page) of ls will be shown.

Installing the SSH Client Program

Step 1. Go to ITSC homepage: http://www.cuhk.edu.hk/itsc/index-en.html Then, select the option as shown in Figure 11.

Figure 11: Step 1. Go to ITSC homepage and select the Useful Tools and Links.

Step 2. Select SSH as shown in Figure 12. Step 3. After some nexts and agrees, you will reach the page shown in Figure 13. Download the rst two exe les. The rst exe le is the primary installer. Please install it rst. The second exe le is the update. Please install it after the rst one.

48

Figure 12: Step 2. Select the Secure Remote Access with SSH.

Figure 13: Step 3. Download two exe les.

49

SHELL command

This following commands are applicable in bash and tcsh.

Ctrl + b Ctrl + f Ctrl + a Ctrl + e Ctrl + u Ctrl + k Ctrl + y Ctrl + d Ctrl + h Ctrl + s Ctrl + q Ctrl + l history jobs tab

move to left by 1 character move to right by 1 character move to the beginning of a line move to the end of a line cut and copy all the characters on the left of the cursor cut and copy all the characters on the right of the cursor paste the copied characters on the right of the cursor delete the character at the cursor position backspace Scroll lock (lock keyboard) Unlock scroll lock Clear Screen show all the commands previously invoked show all the jobs running or suspended auto completion

50

VI command

Cursor movement Commands -----------------------j - down 1 line k - up 1 line h - left 1 char l - right 1 char ^f - forward 1 screen ^b - backward 1 screen ^u - up half screen ^d - down half screen H - top (Head line) of screen L - bottom (Last line) of screen ^y - scroll down 1 line at top of screen (cursor not move) ^e - scroll up 1 line at bottom of screen (cursor not move) 0 - (zero) - first char of current line $ last char of current line w - move to start of next word W - move to start of next WORD (white-space delimited) e - move to end of word E - move to end of WORD b - move back to previous word B - move back to previous WORD f<c> - find char c in current line in forward direction F<c> - find char c in current line in backward direction ; - repeat last Find in same direction , - repeat last Find in opposite direction % - find matching () [] {} m<c> - mark current line -- <c> can be a to z <c> - goto line with mark <c> G - goto last line of file 1G - goto 1-st line of file 123G - goto line number 123 51

Insert Commands (insert mode terminated by <ESC>) --------------a - append after curser i - insert before cursor A - Append after end of line I - Insert before 1st non-white space o - open an empty line after current line for insert O - Open an empty line before current line for insert Other Commands (replace/change-command terminated by <ESC>) -------------r<c> - replace current character by char <c> R - replace characters until <ESC> pressed s - substitute current char until <ESC> pressed S - substitute current line until <ESC> pressed cw - change up to end of word until <ESC> pressed cW - change up to end of WORD until <ESC> pressed dw - delete word dW - delete WORD dd - delete line yw - yank (store into buffer) 1 word yy - yank (store into buffer) 1 line Y - yank (store into buffer) 1 line p - put buffer content after current position P - put buffer content before current position [c|d|y]t<c> - change/delete/yank up To char <c> u - undo last command U - ignore all changes in line since cursor moves into current line . - repeat last editing command NOTE: ---52

(1)

All above commands can be preceded by a number, which indicates number of repetition. E.g. 100G go to line number 100 2dd delete 2 lines 3yW yank 3 WORDS There are 10 unnamed buffers: "1 to "9 and 26 named buffers: "a to "z Deleted lines are stored in to "1 to "9 as a stack implicitly. Line/word/WORD(s) can be deleted/yanked into "a to "z explicitly. E.g. "a3Y yank 3 lines into "a "bP put content of "b before current cursor position

(2)

EX line editing commands -----------------------:q - quit :q! - quit - no change of the file being edited :w - write out the file being edited :w! - overwrite the file being edited :w fileName - write new file with name "fileName" :w! fileName - overwrite the file "fileName" :r fileName - read "fileName" in after current line :e fileName - edit another file "fileName" :vi fileName - edit another file "fileName" (same as above) :e! fileName - edit another file - ignore change in current file :!vi fileName - fork another process to edit another file "fileName" :1 - goto 1-st line of file :$ - goto last line of file :1,$/pattern1/p - search for "pattern1" in file globally & print it :g/pattern1/p - search for "pattern1" in file globally & print it :g/pattern1/s//pattern3/ - search globally, 53

replace first occurrence of "pattern1" in ALL lines by "pattern3" :g/pattern1/s//pattern3/g - search globally, replace ALL occurrences of "pattern1" in ALL lines by "pattern3" :g/pattern1/s/pattern2/pattern3/g - search globally, in those lines with "pattern1", replace ALL occurrences of "pattern2" by "pattern3" :set nu - set NUmbering :set nonu - set NO NUmbering :set ai - set auto-indent :set ts=4 - set Tab-Stop = multiple of 4 char positions

54

Pico Command

Curser Movement =============== ^F move forward a character ^B move backwad a character ^P move to the Previous line. ^N move to the Next line. ^A move to the beginning of the current line. ^E move to the End of the current line. ^V move forward a page of text. ^Y move backward a page of text. ^W Search for (where is) text, neglecting case. Note: After ^W, press ^T to search for the line ^L Refresh the display. Delete, Copy and Paste ====================== ^D Delete the character at the cursor position. ^^ Mark cursor position as beginning of selected text. Note: Setting mark when already set unselects text. ^K Cut selected text (displayed in inverse characters) Note: The selected texts boundary on the cursor side ends at the left edge of the cursor. So, with selected text to the left of the cursor, the character under the cursor is not selected. Or, cut the currrent line. ^U Uncut (paste) last cut text inserting it at the current cursor position. Other Command ============= ^I Insert a tab at the current cursor position. ^J Format (justify) the current paragraph. 55

^T ^C ^R ^O ^X ^G

Note: paragraphs delimited by blank lines or indentation. To invoke the spelling checker Report current cursor position Insert an external file at the current cursor position. Output the current buffer to a file, saving it. Exit pico, saving buffer. Display Help

56

Index
<, 30 >, 29 >>, 30 , 27 ., 27 .., 27 .forward, 19 acroread, 46 background job, 35 bg, 36 cat, 22 cd, 26 chfn, 40 chmod, 32 Absolute mode, 32 Symbolic mode, 32 cp, 24 credential, 10 du -k, 9 elm, 41 email forwarding, 19 fg, 36 le, 24 nger, 39 foreground job, 35 ghostview, 46 gimp, 45 gtar, 44 gunzip, 44 gv, 46 gzip, 44 head, 23 kill, 35 less, 22 Linux, 5 lpq, 37 lpr, 37 lprm, 38 ls, 27 lynx, 42 Mac, 5 man, 47 mkdir, 26 more, 22 mv, 23 passwd, 40 pico, 40 pine, 41 printquota, 38 ps, 34 quota -v, 9 rm, 21 rm -r (directory), 26 rmdir, 26 Secure shell, 12 57

sftp, 41 SSH, 12 staroce, 46 STDERR, 29 STDIN, 29 STDOUT, 29 tail, 23 talk, 39 tar, 44 tin, 42 top, 34, 35 touch, 21 UNIX, 5 UNIX account, 11 UNIX personal account, 6 unzip, 43 vi, 40 vim, 40 w, 34 webster, 47 wget, 43 Windows account, 11 Windows prole, 6 write, 39 xv, 45 yppasswd, 40 zip, 43 zipinfo, 43

58

Вам также может понравиться