Department of Chemical Engineering
Indian Institute of Science
Bangalore
This tutorial on Unix is designed to help you get over ``starting problems'' one generally has on encountering computers running Unix for the first time. Once this step is accomplished, the learning of the more arcane secrets & tools of the Unix world becomes an enjoyable, and rewarding experience.
This tutorial will give you sufficient information to use a Unix machine to create files, compile source code (you will be needing this for computer assignments given by your instructors), accessing computers in the Supercomputer Education and Research Center (SERC), etc.
This material is more or less sufficient (well ... almost!) to get you through the various jobs you will be required to do in the institute. But you may want to learn more; this tutorial provides information about where you can find additional material. The most important in our opinion are the people around you. Most often you will find that the person on the next terminal, your friend or your senior will be of help; this forms the dominant mode by which information is transmitted, and you will find ready made solutions to many of the problem you may face-these solutions have often been given to the person by the same means! Of course, the chain has to stop somewhere; they are the computer Gurus.
We hope this tutorial will be of help, and that you will have a happy and enjoyable computing time at IISc.
User response (in bold) % User response at the command prompt (%) Terminal output (in typewriter font)
The core of a computer that acts as a link between you and the hardware is called the operating system of the computer. There are two major operating systems-Unix and DOS (Disk Operating System). This manual restricts itself to giving you the basics of the Unix operating system.
Unix is like a hostel with several rooms in it. Similar to the room numbers, you are identified by the system by a username. Just like you would be given a key to your room, so that nobody else is allowed to access your room, you will be given a password, which is known only to you.
An ordinary user has several restrictions like, limited access to other areas, not all programs can be executed, etc. But there is one super-user, known as the root, who has absolutely no restrictions. The root manages the system and is the one who gives you an account to work on the computer.
Entering into one's area in a system is known as logging in. Before you can use the system, you will need to have been given a username and a password.
Example To login to an account type:
login:tintin Password:
This logs in the person of username tintin in the system. Unix is case sensitive. If your username is asterix, do not enter ASTERIX or Asterix. Your password is never displayed on the screen. If it were, other users might see it!
login incorrect login:Either you made an error in typing your username or password or the < CAPS-LOCK > key was on and everything was being sent to the system in uppercase letters.
To finish using a Unix system you must ``log out''. To logout enter the command logout or exit. If this does not work press Ctrl-d.1
CAUTION: Do not leave the computer without logging out. Do not ever switch off any Unix system.
% ps uThis displays all the process currently being executed by the user.
USER PID TTY STAT STIME TIME COMMAND chesen 11061 pts/2 S 19:47:54 0:00 -csh chesen 11618 pts/2 R 19:50:14 0:00 ps u chesen 15387 pts/0 S 19:47:08 0:00 -csh chesen 16179 pts/0 S 19:47:53 0:00 vi Unix.texThe number of logins is the number of times -csh or (-sh or -bash) appears under the COMMAND. In this example the user chesen has logged in twice. To terminate all the processes enter:
% kill -1 -1 or in certain systems % kill -1 -This will terminate all the logins-the present one and all the other previous ones under your username-and return you to the computer from which you issued the remote login command.
Your password is important; it stops other users from gaining access to your account. Never give your password to anyone-not even to your best friend. If you do, you may have your access to the system suspended or withdrawn. As an extra security measure change your password at regular intervals. Be sure to read the guidelines below before creating a new password for yourself.
Example To change your password enter the command passwd and then respond to the prompts by entering your old password followed by your new one. You are then asked to confirm your new password.
% passwd Old password:enter your current password New password:enter your new password Retype new password: re-enter your new passwordThe passwords will not appear on the screen as you type, to prevent other people from seeing them. If you make a mistake, the message
Mismatch - password unchanged.is displayed and your password remains unchanged. Try again.
Every computer on the Internet is identified by a name and an associated number known as IP (Internet protocol) address. This is like a dial-up number, to be specified whenever you need to contact a computer.
For example the main computer of our department is called chemeng.iisc.ernet.in and has a number 144.16.75.130, they can be used interchangeably. Some machines within the department or the institute have a shortened name too, in this case chemeng. This section describes how to login to a remote computer through a network.
To login to your account on a remote system use the command:
% rlogin remote_system_nameTo login to a remote system using a different username, use the command:
% rlogin remote_system_name -l usernameYou are prompted for the password to your account on that system.
Examples To login to the remote system prithvi under the same username use:
% rlogin prithvi Password:This logs you into your account on the remote system named prithvi. The username on this system is identical to the username on the system you are currently using. In case the username is different in the remote system, then press < RETURN > which will prompt for the username. To login to the remote system ibms9 using the username cheuser :
% rlogin ibms9 -l cheuser Password:
telnet enables you to connect your computer to another remote
system. To login to your account on a remote system use the command:
% telnet remote_system_name
You are prompted for your username and password for your account on
the remote system. Once you are logged in, you can begin to issue
commands on the remote system.
Examples To connect to an account on the remote system chemeng type:
% telnet chemeng or % telnet 144.16.75.130 The following prompt is then displayed.
At this prompt if the user name is given then, you will be asked for a password.
We will learn more about access controls in a separate section.
A Unix system can have many users accessing it at one time. As a user you are given a home directory in which you are placed whenever you log on to the system. You are allowed to create, copy, remove files and subdirectories from here.
Users' home directories are usually grouped together under a system directory such as /home. A large Unix system may have several hundred users, with their home directories grouped in subdirectories according to some scheme such as their organisational department. For example consider Anil's and Preethi's home directories. Although both Anil and Preethi are registered as users on their local Unix system, they belong to different categories; Anil is a student while Preethi is a faculty in a department. The directory tree may look like:
The full pathname to Anil's home directory is /home/students/anil-that of Preethi's is /home/faculty/preethi.
To change your current working directory use the command:
% cd pathnamewhere pathname specifies the directory that you want to move to. The pathname can be given as either a full pathname or a relative pathname.
Examples To move down one level to a subdirectory:
% cd exptThis moves you down one level from your current directory to the subdirectory expt. To move up one level of the directory tree:
% cd ..Every directory contains a hidden directory.. (dot dot) that is a shorthand name for this directory's parent directory. Using this shorthand name enables you to move up the directory tree very quickly without having to enter long pathnames. To move to another directory using a relative pathname:
% cd ../mathThis moves you up one level in the directory tree and then moves you into the subdirectory math. To move to a directory using a full pathname:
% cd /home/students/anil/exptThis moves you to the directory expt which is in the home directory of the user anil. To go directly to your home directory from anywhere in the tree:
% cdA short form to represent your home directory is the ~. From any directory a command,
% cd ~/flumechwill take you to the directory flumech which is under your home directory. Similarly from any directory,
% cd ~preethi/paperswill take you to the papers directory under the user preethi.
Examples
To make a directory in the current directory:
% mkdir mathThis creates a new directory math in your current working directory. To make a new directory in the parent directory:
% mkdir ../flumechThis creates the directory flumech in the parent directory of the current working directory. In effect, this creates a new directory at the same level of the file system as the current directory.
% rmdir directory_nameThe directory must be empty before you can delete it. You will need to remove any files and subdirectories that it contains. To remove a directory that contains files use the command:
% rm -r directory_nameThis deletes all the contents of the directory including any subdirectories. Be very careful in using commands such as these.
CAUTION If you remove a directory that still holds files there is no way to retrieve it. To avoid this, always use the rm command together with the -i option.
% rm -ir directory_name
You can use the ls command to list the files in a directory:
% ls [-options] [directory_name]
By combining different command options you can display as little or as much information about each file as you need.
Option | Action |
-a | list (all) hidden files |
-F | show directories with a trailing '/' executable files with a trailing '*' |
-l | long listing giving details about files and directories |
-t | sort by time modified instead of name |
To get more information about each file and directory, use the command:
% ls -lThis gives you a long listing about each file and directory, giving information about its: access permissions, number of links, owner, group ownership, size and date and time last modified.
% cp [-options] source destinationThe source is the name of the file to be copied; the destination is the name of the file in which the copy is to be placed.
Take care! Take care what name you give to the file copy. If you choose the name of a file that already exists it will be overwritten by the new copy! To avoid this,
% alias cp 'cp -i'
Examples To copy a single file in the current directory:
% cp notes sect3.txtThis makes a copy of the file notes in the file sect3.txt. To copy a file using the -i option:
% cp -i notes part2.txtThis makes a copy of the file notes in the file part2.txt. If this file exists, the message
part2.txt: File existsis displayed. The file is not copied. To copy a file from your current working directory to a subdirectory:
% cp fig2 part2/figure2This copies the file fig2 from your current working directory to the file figure2 in the subdirectory part2. To copy a file to the parent directory:
% cp mail.txt ..This copies the file mail.txt to the directory immediately above the current working directory. The .. (dot dot) is shorthand for the parent directory. To copy a file from a subdirectory to a directory elsewhere in the file system:
% cp docs/vi.Z /tmpThis copies the file vi.Z in the subdirectory docs to the directory /tmp. To copy a file to your home directory:
% cp /usr/local/doc/ue.txt $HOME or % cp /usr/local/doc/ue.txt ~This copies the file ue.txt to your home directory. $HOME is the environment variable that contains the value for your home directory.
To remove a file use the command:
% rm [filename1] [filename2] [...]You cannot remove a file in another user's account unless they have set access permissions for the file which allow you to delete them. Use the -i (interactive) option which makes the command prompt you for confirmation that you want to remove each file.
Examples To remove a single file:
% rm help.txtThis removes the file help.txt from the current directory. To remove several files interactively:
% rm -i *.fmThis will prompt you to confirm that you want to remove a file from the current directory when-ever that file has the suffix .fm. Answering y will delete the file. The file is not deleted if any other response is given.
To display a file use the command:
% more filenameThis will display the first screen full of the file. The prompt
-More-(52%)is given in the bottom left-hand corner of the screen. You can now use one of the following commands:
< space bar > | Display next screen full of text. |
< RETURN > | Display next line of text. |
q | Exit from more: this can be done at any time. |
d | Scroll forwards about half a screen full of text. |
b | Skip backwards one screen full of text. |
h,? | Display a list of commands (help). |
mv command. To rename a file: % mv junk preciousThis renames the file junk as the file precious. To be prompted when using the mv command:
% mv -i draft1.txt part1.txt remove part1.txt? nThis command prompts you to confirm that you really want to move the file draft1.txt to the file part1.txt; a file with that name already exists. Entering n cancels this action: a y confirms it. To move a file to another directory:
% mv precious treasureThis moves the file precious from the current directory to the directory treasure. To move a file to another directory and rename it:
% mv jewel casket/amethystThis moves the file jewel to the filename amethyst in the subdirectory casket.
% ftpThe program starts and the ftp > prompt is displayed. You are now ready to give commands to the ftp program.
ftp > open chemengThis shows the user opening a connection to their account on the remote host chemeng. Notice that you must supply your username and password for your account on the remote host. After successful login, the user is placed in his/her home directory on the remote machine. The user now has simultaneous access to two home directories-in local and remote machine. To close the connection type quit.Connected to chemeng. 220 chemeng FTP server ready.
Name (chemeng:guest): guest 331 Password required for guest. Password: not displayed
230 User guest logged in. ftp >
To send a single file from your current directory on the local host to the current directory on the remote host:
ftp> put myfileThis copies the file myfile from your current directory on the local host to your current directory on the remote host. If you don't give a name for the remote-file the name of the local-file you are copying is used to name the remote-file. To use file pathnames to define both the local-file and the remote-file:
ftp> put REPORTS/april reports/month4This copies the contents of the file april in the sub-directory REPORTS, which is in your current directory on the local host to the file month4 in the sub-directory reports, which is in your login directory on the remote host. To send multiple files to a remote host:
ftp> mput *.xdh mput index.xdh?This prompts you to confirm for each file with the extension *.xdh in the current directory in your local host that it is to be copied to your current directory on the remote host. Entering a y copies the file; an n cancels this.
To get a single file from the remote host:
ftp> get newsThis transfers a copy of the file news from the current directory on the remote host to your current directory on the local host. If you don't give a local filename the filename of the remote file you are copying is used to name the local file. To get a file from a subdirectory on the remote host:
ftp> get reports/quarterly/march tmp/month3This transfers a copy of the file march in the remote subdirectory reports/quarterly to the local filename month3 in the subdirectory tmp. To get several files from a remote host:
ftp> mget *.fThis transfers a copy of each file with the filename extension .f in the current directory on the remote host to the current directory on the local host. Entering a y copies the file; an n cancels this copy. To get a help of the ftp commands available use:
ftp> ?
Examples To search the online manual for a command with the keyword mail
% man -k mailTo display the manual page for the cp (copy) command use:
% man cp --More--48 %appearing at the bottom left of the screen means that only 48% of the man page is displayed. Press the space bar to display more of it or type q to quit.
% alias rm 'rm -i'By typing this the alias command allows you to type rm as a shortcut.
It is possible to specify a given set of commands that are to be executed every time you login. These are kept in files such as the .profile and .cshrc. You may get these files from senior users who have customised useful and important commands.
While using Unix you will often want to create a text file and then change its content in some way. A text editor is a program for inserting or amending text in a file. A text editor is not a word processor although some text editors do include word processing facilities.
The vi editor (pronounced ``vee eye'') is available on all Unix systems. Being able to use vi ensures that you will always have an editor available to you.
Examples of starting vi and opening files
To open an existing file for editing:
%vi introThis opens the file intro for editing. This file already exists in the current directory. To create a file to edit:
% vi part2This command starts vi with a new file named part2. This file does not exist in the current directory until it is written to using the write command. To simply start vi use:
% viThis command places you in vi. You can then enter and edit text and write it to a file.
Command mode
This is the mode you are in whenever you begin to use vi. In this mode commands are used to move around and edit text objects such as words, sentences and paragraphs. Pressing the ESC key returns you to command mode.
Insert mode
This is the mode you use to type (insert) text into a buffer. There are several commands that you can use to enter this mode and the various ways are discussed below.
Saving changes
To save changes while editing a file type in the command mode:
:w < RET >and then continue editing. To save changes to another file:
:w filename < RET >
Quitting vi
To quit the vi editor from the command mode:
:q < RET >But if you have edited (changed) the file you will be prompted with the message:
No write since last changeYou can either save the changes and quit by typing :wq or quit without saving any of the changes you have made by typing :q! in the command mode.
Cursor movement commands
To move the cursor | Do this |
one character to the left of the current cursor position | h |
down by one line from the current cursor line | j |
up by one line from the current cursor line | k |
by one character to the right of the current cursor position | l |
to the next word | w |
to the previous word | b |
down by 12 lines | CTRL d |
up by 12 lines | CTRL u |
up by 24 lines | CTRL b |
down by 24 lines | CTRL f |
to the end of the line | $ |
to the beginning of the line | 0 (zero) |
List of insert commands
These are the commands to go from command mode to the insert mode.
To do this ... | Command |
Insert text after the cursor | a |
Insert text at the cursor | i |
Append text at the end of the current line | A |
Insert text at the start of the current line | I |
Open a new line below the current line | o |
Open a new line before the current line | O |
Searching for text
To search | Do this ... |
forward for a pattern | /pattern < RETURN > |
backward for a pattern | ?pattern < RETURN > |
repeat search | n |
repeat search in reverse direction | N |
Deleting characters
To delete | Do this |
current character | x |
previous character | X or dh |
Deleting words and lines
To delete | Do this |
current word | dw |
previous word | db |
entire line | dd |
to start of line | d0 (zero) |
next n lines | ndd |
rest of the line from current position | D |
Undeleting text
To get back a word or line that you have just deleted enter the command:
p or u
Modifying Text
To modify | Do this |
Replace character at current position with character x | rx |
the current line | cc |
the current word | cw |
rest of the line from current position | C |
to join lines | J |
``Cut-and-Paste'' commands
To do this.. | do this |
Yank current line into buffer | yy |
Yank next n lines into buffer | nyy |
Put back yanked text after cursor | p |
Put back yanked text before cursor | P |
Yank in this context means to copy a certain number of lines to a buffer.
Line Number Search
To do this.. | do this |
Display current line number | CTRL g |
Go to line number n | n G |
A source code is a program written in high level languages such as FORTRAN or C. Converting this code to a form that can be understood by the computer is called compilation. The computer then executes the resulting file and produces an output. Here we will see how FORTRAN and C programs should be compiled and executed in a Unix platform.
Example 1 - FORTRAN Consider the file sample.f:
C C sample.f : A sample FORTRAN code. C IMPLICIT none REAL a, b, c a = 5.0 b = 6.0 c = a*b WRITE(*, *)'a =', a WRITE(*, *)'b =', b WRITE(*, *)'a * b =', c STOP ENDTo compile this file use the command
% f77 sample.fThis by default creates an executable file a.out. To execute this file type:
% a.outTo create an executable file of a different name (such as sample.e) use:
% f77 sample.f -o sample.eThis FORTRAN code must give the following output on execution.
a = 5. b = 6. a * b = 30.
Example 2 -C Consider the file sample.c
/* sample.c: A sample C code */ #include<stdio.h> void main(){ float a=5.0,b=6.0,c; c = a*b; printf("a = %.3f\nb = %0.3f\na * b = %.3f\n",a,b,c); }
To compile this file use the command % cc sample.cThis by default creates an executable file a.out. To execute this file type:
% a.outTo create an executable file of a different name (such as sample.e) use:
% cc sample.c -o sample.eThis C code must give the following output on execution.
a = 5.000 b = 6.000 a * b = 30.000
Read Unix Primer document carefully before you start practice session. If you have any difficulty during the session, consult the convener in the laboratory you are working.
f(x) = x3 - 4x2 + x + 2 = 0, |
xk+1 = xk - |
f(xk)
| , |
We hope that you would have gained a good exposure to Unix through these sessions. There are many features of Unix which you will learn only through practice. We have compiled information about some of the advanced features which is available in a small document kept in our library.
1This means press ``d'' while pressing
the ``control key''