100% found this document useful (1 vote)
78 views

A Beginners Guide To UNIX

UNIX is a powerful and popular operating system that is used by a significant proportion of the computer industry. Despite being branded as archaic and unfriendly, UNIX can be tamed with patience and a bit of study. This guide aims to get you over that initial hurdle, and on your way to greater things.

Uploaded by

alphaville99
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
78 views

A Beginners Guide To UNIX

UNIX is a powerful and popular operating system that is used by a significant proportion of the computer industry. Despite being branded as archaic and unfriendly, UNIX can be tamed with patience and a bit of study. This guide aims to get you over that initial hurdle, and on your way to greater things.

Uploaded by

alphaville99
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 32

A Beginners Guide to UNIX

Aims of this Guide


UNIX is a very powerful and popular operating system that is used by a significant proportion of the computer industry. Yet many perceptions of the computer centre around PCs, and the operating systems commonly run on them. Upon encountering UNIX, whether it be on a personal computer or a mainframe, confusion and frustration often results, causing UNIX to be branded as archaic and unfriendly. But this is only due to UNIX being a vastly more complex system, as it copes with multiple platforms, processors, disks, and brings truly platform independent computing to many users simultaneously. With a bit of patience and a bit of study, this monstrous beast can be tamed, and your experience with UNIX, for whatever purpose, can be an enjoyable and profitable one. And that's where this guide comes in. It aims to get you over that initial hurdle, and on your way to greater things. But of course this is only a beginning. There are countless books which provide more extensive and advanced material, and you are advised to read one or more of these after this guide, should you wish to pursue your mastery of UNIX further. Just about any appropriately titled book will help you, but there are two which can be particularly recommended: Graham Glass, UNIX for Programmers and Users, A Complete Guide, Prentice-Hall, Inc., 1993. Daniel Gilly, UNIX in a Nutshell, a Desktop Quick Reference, O'Reilly & Associates, Inc., 1992.

Conventions Used
Different styles are used to make certain parts of the text stand out. These adhere to the following conventions.
Bold red keyboard style text is used for things you type directly into your UNIX prompt. Bold green sample style text is used for commands and output. Bold text is used for key words and phrases.

About the Authors (as of 1996)


Matt Chapman has recently graduated from the University of Warwick, UK, with a Bachelor's degree in Computer Science. He is now working as a software developer for IBM based in the IBM Centre for Java Technology in Hursley, UK. His other projects include a Window Managers for X page, a Finite State Machine Explorer in Java, and a variety of small games and utilities for X/UNIX platforms.

Frankie Blaskovic is a third year Law student at the University of Warwick. He is responsible for the University of Warwick Students' Union WebPages as well as for the Warwick Boar. He is currently working on numerous projects, the most important of which is the managment of the New Technologies Programming Team in the UK. He is also active on many commercial web sites, keep a lookout!

Commands covered in this section: login logout su logname passwd

First Steps
In this section we will cover the basics you will need, logging in and out of the system. Before launching into this, please note that you will need to get a login name and a password from you system administrator. Make sure that you type both your login name and password in the appropriate case, if your password is mik9998H and you type Mike9998H (or mike9998h, ...), you won't be able to login.

Logging in
When you are ready to start using a machine, you will initially see a prompt on screen like: login: type your login name. Then a prompt asking you to type your password in will be displayed. Type your password in as it was given to you. The password you type in will not be displayed on screen nor any other characters, this is for security reasons so don't worry if you don't see anything just type it in and press enter. If you have got it all right you should get a prompt with the name of your machine. You can now start using all the wonders of UNIX!!

Restarting your Login Shell


You might at some point need to change your login environment variables (more on these later on), or even the login name you are using (you can only do this is you have more than one account on the system). To do this you would use the command login. A few restrictions apply to this command, to be able to execute it you must not be in a subshell (if you don't know about subshells don't worry you are most probably not using one). The syntax to use is: exec login name where name is the new login name you want to use, if you just want to reinitialise your environment type your own login name. This new session will replace your former so that it will be as if this was the command you use to log into the system from the start.

Changing your Identity


Using the login command to do this was discussed above. We would like to introduce a new command namely su. su will allow you to login as another user but with the difference that you will be working in a subshell, so when you logout, you will resume your initial identity (whereas using login would log you out of the system altogether). su (which stands for substitute user) is used by typing su username, where the username is the name of the new user you wish to use. By typing su - username (spaces before and after - are necessary) will

allow you to work in a new shell (used by the user who's identity you are assuming), along with that user's environment set-up. However if you omit the - you will still be working in your original shell/set-up but with the new user's identity and privileges. Note:The su command will not allow you to login without the new user's password!!

Checking your Identity


If at some point you want to see what login name you are using (during one of your logins) just type logname and your login name will be displayed.

Logging Out
This is the important one! To logout (and terminate you session) simply type logout at the prompt. This will log you out and make sure you have no processes running. An alternative to this command is to use the Ctl and D keys, it does exactly the same thing as would the logout command. If you use logout while using a subshell this will log you out of the subshell and not out of the system. Always make sure you have logged out properly! Wait for the login prompt to be displayed again or if worst come to the worst reboot your machine.

Note on Passwords
Changing your password is very important. You show change it at least once a month, and specially if people see you typing it in! Most systems require you to have a password of at least 6 characters long, and you should use numbers as well as symbols (eg: us76.jh#q9*7$ ), make it as difficult as possible to guess. If you are the luck owner of several accounts DO NOT use the same password for each!

Changing Passwords
To change your password simply type passwd, you will be asked to enter your old password and then to type in a new one twice. This is because, what you type will not be echoed on screen, so to make sure you have typed it in properly you need to do it twice! If you have given a correct password and typed your new one in properly your password will be changed, if not, well start again!! You can also use passwd username. This will allow you to change the password for the user specified by username (the same process applies as if you were changing your own).

Commands covered in this section: ls cd pwd cat more

The Filesystem
UNIX employs a directory hierarchy, as illustrated in the picture below. The top of the hierarchy is known as the root directory and is specified as /. In the example shown the

root directory contains three directories, which are sub-directories of the root. The home directory is the parent of the two sub-directories foo and bar.

An absolute filename is the full path of a file or directory, from the root of the hierarchy, given using the / symbol (called a slash - specifically a forward slash) as the directory separator. So the absolute filename of the bar directory is /home/bar. A relative filename is one without an initial / which refers to a directory in relation to the current directory.

What's Inside?
The most frequently used command in UNIX is ls. It has many options, but on its own it simply shows the contents of the current directory, which may be directories or files. Try it now: simply type ls at your command prompt. If you specify an argument to ls it will take this to be a directory and will attempt to list the contents of that directory. Try entering ls / which will list the files and directories in the root directory. There will be some of the ones shown in the diagram, and many others. The command pwd tells you which directory you are in. If you haven't changed directory since logging in, you should get something like /home/fido, with your usercode in place of fido. This directory is your home directory, and is the part of the filesystem reserved for you. In here you can create the files and directories required for whatever tasks you are engaged in.

Moving Around
You are always in a particular directory, referred to simply as the current directory. To change the current directory, the command cd is used. It can take as an argument a directory, and will then move to this directory. So cd / will move you to the root directory - try using pwd and ls to verify this. Typing cd on its own will return you straight to your home directory, from wherever you are. There are two important conventions employed when referring to UNIX directories. The first is a single dot: ".". This means "the current directory". The second is two dots: "..", which means "the previous directory up in the hierarchy", or "the parent directory of the current one". Use cd and pwd to experiment with these. Start in your home directory, and work up the hierarchy by using cd .. repeatedly. What happens if you attempt to go above the root directory? Try and it see. You won't get an error message, you just won't go anywhere.

What is it?
The -l option to ls will give you a long listing of the contents of the directory. It includes such things as the size of files, the date and time they are last modified, and whether the item is a file or a directory - the first character of the output shows this: d for a directory and - for a regular file. Experiment with the ls and cd commands until you find a regular file that is not too large. The contents of this file can then be examined in two ways. Firstly, the cat command will simply output the entire file to the screen. It is called simply by typing cat followed by the name of the file you wish to view. If the contents are more than a screenful, they go off the top of your window, so you miss some. If this is the case, the second command is best used. This is known as more, and is called in the same way as cat, but pauses after each screenful has been displayed. Simply press the spacebar to move on to the next page. Alternatively, you can press q to quit the command, and stop the rest of the file being displayed.

Commands covered in this section: touch mkdir mv cp rm rmdir

A Word of Caution
The previous section showed you how to move around the filesystem and examine text files. We will now show you how you can change things - the names of files, their location, and how to remove them. You can NOT do any damage to the system through use of the commands covered here, but you could lose your own files by accident if you are not careful. So if you have anything important, keep a copy of it - either somewhere else in your home directory, or (even better) on a secondary storage device, such as a floppy disk. Remember: there is NO undelete command under UNIX, although the system administrator should make regular backups, so it may be possible to retrieve lost files from these.

Creating a File
You may already have some files in your home directory to play around with, but don't worry if you haven't. The easiest way to create some is with the touch command. Make sure you are in your home directory (type cd to get there, and then pwd to check). Now type touch rents which will create an empty file called rents in the current directory. Use ls to verify its existence, then use the -l option to see that its size is 0 bytes. Look at the time and date column as well - this shows when the file was last modified, or in this case created. The touch command has another use, when given a filename; it changes the modification time to the current time. Do touch rents again, then ls -l or ls -l rents to see the new modification time.

There are obviously many ways to create files, such as with text editors. These will be covered in a later section.

Creating a Directory
Directories are very useful for collecting related files together. The typical UNIX system contains many thousands of files. Imagine the mess if every one was contained in the same directory. You can too can use directories to organize your files. The mkdir command is used to create new directories. Create one now, with mkdir leith and use ls to see it.

Moving and Renaming


This is all achieved with the mv (short for move) command, which operates differently, depending on the arguments given to it. Many of these operations are illustrated in the examples below, which can be carried out in order using the files and directories created earlier. As before, use ls to examine the effects of each command. Rename a file:
mv rents forry

Rename a directory:
mv leith hibs

Move a file into a directory:


mv forry hibs

Move it back into the current directory:


mv hibs/forry .

Making a Copy
The copy command is cp. Notice how most of the command UNIX commands are only two letters, to save some typing. Later you'll see that there are many other ways in which the amount of typing you have to do can be reduced. The copy command has two modes of operation; it can make a copy of a file under a new name, and it can make copies of a list of files and place the copies in a given directory. To illustrate this, we'll need to create some more files, using touch again. Type this:
touch tommy franco matty

This creates three new files, all in one go. You don't need to use three separate touch commands. This sort of thing can be done with most UNIX commands, and is another example of making things quicker to do. Now we can make a copy of one of the files:
cp franco begbie

Let's make a copy of two of the files and place them in the hibs directory:
cp tommy matty hibs

Use ls to verify that the files you have created are arranged like this:

Removing things
The remove, or delete, command is rm. Related to this is the rmdir command for deleting directories, but only if they are empty. We'll finish this session by removing all the files we created. It is important to know how to clean up your directories by removing unwanted files, otherwise they will become cluttered and you won't be able to find things easily. Also, your home directory may be limited in the number and total size of files it can hold. Remove two files:
rm forry tommy rmdir hibs

Attempt to remove the hibs directory: You'll get a Directory not empty error. You could delete all the files in it with rm, and then use rmdir, or you can use the -r option to rm, which recursively deletes all files and sub-directories in the given directory, and then the directory itself. Be careful with this command. Delete the hibs directory and its contents:
rm -r hibs

Finally, remove the remaining files:


rm matty begbie

Commands covered in this section: emacs xemacs jove vi lpr lpq lprm

Editors
The two main text editors available on most UNIX systems are emacs and vi. In recent times, emacs has become the most popular, due to its power and flexibility, whereas vi can seem confusing to use, as it is rather unusual, although once mastered many find it very quick to use.

The Emacs Family


Emacs is a powerful, extensible text editor. The name stands for Editor Macros, although some pretend it stands for Emacs Makes A Computer Slow (it is so large it can be slow compared with more simple editors), or for Escape, Meta, Alt, Control, Shift (in reference to the complex key sequences often required). Full versions of emacs come with packages written in Emacs-Lisp, which provide all manner of additional features, such as e-mail and news modes, and even a web browser mode. Also, different modes are usually supplied to aid the editing of different types of files, such as plain text, programming language source code, and TeX files. They can highlight different parts of the file in different colours or fonts, which can make the file easier to work with. Due to its immense popularity in the UNIX world, emacs has spawned numerous flavours and clones. Three of the main ones are:
Emacs - the original. Jove - a simple, cut-down version, excellent for beginners. XEmacs - Emacs: The Next Generation. The biggest and the best. It has full graphical user interface support, running under The X Window System. (It was used to create this page, using an advanced HTML mode.)

All versions should come with extensive documentation and tutorials, which are obviously worthy of careful study if you intend using the program. However, a few basics will be covered here, to start you off. They should apply to all versions of emacs. To start up the editor, simply type its name. If you type a filename after it, it will attempt to load this file, and it if doesn't exist it may create it. You can then enter some text straight into the editor window. Commands are given to the editor through key combinations, although in some cases a mouse can be used to select operations from a menu bar. In describing key combinations, the following abbreviations will be used: C- means hold the Control key down and press the following key, so C-g means press and hold the Control key and press g while Control is still held down. Similarly the abbreviation M- is used to indicate the Meta key, which is often next to or close to the space-bar and may be labelled as a diamond. Users of Jove (and some other versions) should use the Escape key instead, labelled "Esc", and this should pressed and released, before pressing the next key.

Movement Forward one word Back one word Up one screen

Key Combination M-f M-b M-v C-v C-a C-e M-a M-e M-< M->

Cursor Movement

Down one screen To start of line To end of line To start of sentence To end of sentence To start of file To end of file

There are numerous ways of moving the cursor around a text file, aside from the cursor keys which move one character at a time. The more common ways are shown in the table. If there are no cursor keys on your keyboard the following key combinations can be used instead. The letters used stand for Previous, Next, Forward and Back.
C-p C-b C-n C-f

Action Delete character after cursor Delete word before cursor Delete word after cursor Delete to end of line Delete to end of sentence Insert previous kill (Yank) Undo

Key Combination C-d M-delete M-d C-k M-k C-y C-X u

Deleting and Pasting

In addition to the standard Delete or Backspace key, which deletes the character before the cursor, there are numerous commands for deleting and moving text, as shown in the table. The Yank command is very useful when used in conjunction with the Delete to end of line operation. Multiple lines can be deleted, and then pasted back in a different place, using the Yank command. This provides an easy way of moving text around. If you are running a graphical version of emacs you can also use the mouse to select and paste text.

Action Search forward Search backward

Key Combination C-s C-r C-g C-x C-s C-x C-f C-x C-c

Searching and Saving

Cancel operation Save current file Edit a file Quit

The current text can be searched by pressing the appropriate key combination and then entering the string to search for. In some versions of emacs the search is performed interactively; the cursor jumps to the next position which currently matches the entered text. You can search for the next match by pressing the key combination again. The search can be cancelled by pressing C-g, which can also be used to cancel many other operations. If the Edit file command is given you will be prompted for a the name of a file, which will then be loaded into the editor. But any file you were previously editing will remain in memory. You can open as many files as you like, and each is placed in its own buffer, and there are various ways of changing buffers, depending on your version of emacs.

The Vi Editor
The main feature which distinguishes vi from most editors is its Insert text in front i separate modes: text entry mode and command mode. The of cursor editor can be started by typing vi, with a optional filename. On startup Insert text after a it enters command mode. To change to text entry mode one of the cursor keys listed in the table is pressed, depending of the nature of the entry Insert text after o mode required.
current line Insert text before current line Overwrite text O R Action Key

You can now enter text, but you cannot use any of the cursor keys to move around or perform any other operations. To do so, you must leave the mode and enter command mode, by pressing the Escape key. You can then perform the operations described in the following sections.

Movement To start of line To end of line Forward one word

Key Combination ^ $ w b C-b C-u C-f C-d :n

Cursor Movement

Back one word Up one screen Up half a screen Down one screen Down half a screen To line n

The keys shown in the diagram below can be used to move the cursor around. In addition, cursor keys can be used, if they are present on your keyboard.
k h j l

The last command listed, to jump to a given line number, shows use of the extended command entry facility. The Enter key should be pressed after the command is entered.
Action Delete character Delete word Delete entire line Delete to end of line Delete block Replace characer Replace word Replace line Yank lines into buffer Paste buffer after current line Paste buffer after line n Key Combination x dw dd D :<range>d r cw (then Esc) cc (then Esc) :<range>y :pu :npu

Deleting and Pasting

Vi provides numerous commands for manipulating text, many of which are shown in the table. To replace a word or line, press the keys listed, then enter the replacement word or line, pressing Escape to finish. Two of the commands listed contain a <range> parameter which specifies the lines the command is to be applied to. This can be a single line number, or a start and end line number, separated by a comma. There are two special characters that can be used: $ for the last line, and . for the line containing the cursor.

Action Search forward Search backward Repeat last search

Key Combination / ? n :<range>s/str/rep/ :<range>s/str/rep/g :w :w <name> :e <name> :q :q!

Searching and Saving

Replace first occurrence of str on each line with rep Replace every occurrence of str on each line with rep Save current file Save as <name> Edit <name> instead of current file Quit if saved Quit without save

To search for a string in the current buffer, press the key shown in the table, type in the search string, and then press Enter. The replace commands operate on the entire buffer, unless a range parameter is given. You must save the buffer before quitting, unless you use the quit without save option.

Printing files
Most networked systems have at least one printer attached to the network, which can be used to obtain a hard copy of your work. For most purposes fast dot matrix printers are adequate, although laser printers can be used for higher quality output. To send a file to a printer type lpr followed by the name of the file to be printed. This will send the file to the default printer, whatever that may be set to. If you wish to use a different printer, you need to know the name of the printer, as it is known to the system, and then you can use the -P option to lpr. For example a laser printer may be known as ps (for Post-Script), in which case lpr -Pps text will send the file to it. Files sent to a printer are held in a queue until the printer is free to print them. On a busy system this could take some time, especially if the printer runs out of paper or breaks down. You can monitor the print queue, and the progress of your file in it, using the lpq command. It will list the job number, name, size, and owner of all the items on the queue. Files can be removed from the print queue before they are printed by using the lprm command. Provide this command with the job number or numbers you wish to cancel, obtained using lpq, or use lprm - to cancel all of the items on the queue belonging to you. Make sure you use this command if you make a mistake or change your mind; don't waste resources, and remember you are responsible for files you print, which you may have to pay for. Find out first!

Commands covered in this section: date wc who ps du quota

Date & Time


To find what the date and current time are, simply type date. This will give you the date and time in a format similar to this Thu Aug 13 13:50:33 EDT 1996. If the system is set to display GMT then this is what you will get, otherwise you can force date to display GMT by using date -u, which will produce your output in GMT.

File Lengths
The wc command will enable you to find out how many words, lines or characters are in a text file. This is a useful command if you have some work which is restricted in length, or simply find out how long you latest master piece is! wc filename will give you the number of characters, words, and lines in the file. If you don't want all these details you can restrict the output by using: wc -c filename to get the number of characters in the file only, wc -w filenameto get only the number of words in the file, wc -l filename to get the number of lines, or you can use any combination of these.

Space Used
One thing you are bound to notice at some point, is that you do not (most often) have unlimited space on your account. Each file/directory you create will take up some of your file and disk quota. Monitoring how much disk space you are using is an essential part of your UNIX life. There are two commands you can use to do this, namely du and quota.

du
du will report how many disk blocks your files and directories are using. Use du to find out the space taken up by the files in your current directory as well as any sub-directores immediately beneath the current. If you use du name1, name2, ... you will get the amount of space taken up by the files/directories you specified. The output of du can be formatted, to make reading it easier. Use du -a to force du to display details of each file on a separate line, or use du -s to give you details of how much space the entire directory is using, not displaying each the details for each file.

Another way to find out how much space you have left is to use quota. This command will tell you how much of your quota you are using and what your limit is.

quota
quota on its own will tell you how much space has been allocated to your account and how much you are using. quota -v will display quota on file systems where there is no storage. quota -g will tell you how much quota you are using as well as the group to which you belong, if you want more details use quota -q. To get another user's quota information use quota username. Or you can use quota groupname to get the quotas for a given group.

The UNIX Shells

A fundamental concept in the UNIX world is that of shells. A shell is a program that forms an interface between you and the raw operating system. Whenever you enter commands into a UNIX system, you talk to a shell, which takes your input, processes it in various ways, makes the required operating system calls and displays any results. Having this separation allows many useful features to be introduced without changing the underlying operating system. All shells provide certain functionality, much of which will be described in this section, but some shells offer advanced compabilities which will discussed in a later section dedicated to that shell. The main shells are:
sh - the original Bourne Shell csh - the C shell tcsh - extended C shell bash - the GNU Bourne Again SHell (one of the most popular) ksh - the Korn shell zsh - the Z shell (the most advanced)

The first two shells listed above are fairly basic, and some versions do not provide some of the facilities dicussed here, such as tab-completion. Therefore it is recommended you use one of the other four, if possible. You can find out which shell you are using by typing echo $SHELL. You can use a different shell by typing its name, or you can change shells completely using the chsh command, if this available on your system (run the command and you'll be prompted for the full pathname of the shell you wish to use - use which followed by the name of the shell to tell you this).

Tab-Completion
Filenames can be long - up to 256 characters. They are also case sensitive, so for example, file, FiLe, and FILE are all different files. You can used mixed case and other characters like "_" and "." to give your files meaningful names. Don't be put off from using long filenames because you don't want to have to keep typing them in - there is no excuse with tab-completion! To experiment with this feature, create some files by typing touch psychohistorians foundationsEdge foundationAndEmpire. Don't worry, you'll only have to type that lot in once. Now let's say you want to rename the first file to seldon. To do this you would use the command mv psychohistorians seldon. But wait! Type mv psy (without pressing Return) and then press the TAB key. Providing your shell supports it, the rest of the filename will be completed for you, as if by magic! What is happening here is your shell is looking for all files that match the prefix you have entered. Assuming there is only one

matching file, it will fill in the rest of the name for you. You can then enter the rest of the command. Next you want to delete the second file. Type rm fou and press TAB. The shell will fill in as such of the name as it can - foundation in this case (assuming there are no other similar files in the directory.) It will then beep to let you know there is more than one completion. Now type s and press TAB again. This prefix should now be unique, so the full filename will be filled in. When there is more than one completion, some shells can display the available choices (for example, press TAB again with the bash shell). This time, typing mv fou and pressing TAB should cause the full filename of the third file to be filled in, as the other has been removed.

Filename Substitution
This facility, also known as globbing, allows you to select files using wildcards. This form of pattern matching can be used to refer to a collection of files in one go, again saving considerable time and effort. To experiment with this feature, repeat the touch command from above, either by entering it again, or by pressing the cursor up key repeatedly to select it from your command history, if your shell supports this. Also do touch found.c mule.p. Patterns are specified using wildcard metacharacters, which are simply characters which are treated specially by the shell. If you wish to use one of these characters as itself, and not
Wildcard * ? [] Meaning Matches any string Matches any single character Matches any of the characters between the brackets. A dash is used to indicate a range of characters

its special meaning, precede it by a backslash (\) character; for example, touch the\*star would create a file called the*star. The meaning of the wildcards is given in the table and their use is demonstrated by the following examples. List all files beginning with fou:
ls fou* ls *.c

List all files ending with .c: List all files ending with a dot and a single character:
ls *.?

List all files beginning with f or m:


ls [fm]*

List all files beginning with a letter in the range a..m:


ls [a-m]*

The shell expands the pattern into a sorted list of filenames and passes this to the command involved, exactly as if you had entered each of the names separately. Now use filename substitution and/or tab-completion to remove all the files created here.

Pipes and Redirection


Pipes allow you to send the output of one command to the input of another, using the pipe metacharacter "|". For example ls | wc -l will count the files in the current directory, and ls -l | more will give a detailed file list, pausing between each page if the list is too long to fit on the screen at once. You can join any number of commands together in this way, to form a pipeline. These are fundamental to UNIX, and can solve complex problems using only the basic building blocks of UNIX commands. For example ls -l | cut -c33-42 | sort -nr | head -1 finds the size of the largest file in the current directory. It works by listing the files, extracting the size column, sorting this in descending numerical order, and taking only the first line. Redirection allows commands to get their input from a file instead of the keyboard, and send their output to a file instead of the screen. The two metacharacters "<" and ">" are used to do this. For example, ls > contents will store the directory listing in a file called contents and will not display anything to the screen. Verify this by running the command and then using cat or more. Note that the contents file is created in the process, but if it already exists it will be overwritten, so be careful. Input redirection is similar; wc -l < contents will cause the wc command to read from the file. There are two additional forms of redirection: ls >> contents will send the output to the named file, but it will append this output to the current contents of the file, if any. The input version of this operation creates what are known as here documents. Enter cat <<END. Your prompt will probably change, to indicate that the command is waiting for some input. Type a few lines of text, and then type END exactly, on a line of its own. The command should then complete by repeating what you entered. The <<HERE part of the command tells your shell to accept what is typed as input to the command, until the word END is read on a line of its own - any word can be used instead. The command then behaves as if what you entered was stored in a file and redirected into the command. This facility has numerous uses, such as quickly creating a small file; cat <<HERE >textfile will cause what you type to be stored in the named file.

Input, Output, and Error Streams


The pipe and redirection characters discussed above manipulate streams of characters. There are three standard streams: input, output, and error. Normal communication to and from commands takes place via the input and output streams, but there is a third channel for

reserved for errors. For example, wc -l contents > out will count the lines in the file, assuming it exists, and store the results in a file called out. However, if you remove the contents file and try again, you will get an error message: wc: contents: No such file or directory, and the out file will be empty. The error message is not send to the file as it is using a separate stream, namely the error stream. It is, however, possible to redirect the error stream in a similar way to the standard output stream. Typing wc -l contents 2> out will send any errors to the file, with any standard output going to the screen. Multiple redirection is possible as in wc -l contents >out 2>errors, which sends the two output streams to different files. Note that spaces after the redirection characters are optional.

Commands covered in this section: ps wait sleep fg bg nohup kill

Foreword
When using UNIX, process management becomes very important, because a given program can start more than one process and you can have suspended processes, processes running in the background, ... In this section we will show you how to see what processes you are running, how to run a process in the background and how to bring them to the foreground.

Viewing Running processes


Use ps to view what processes are running, this will give you the following output: PID TTY TIME COMMAND 067 console 00:05 sh 063 tty02 10:20 /bin/local/usr/zsh 062 tty02 10:22 ps 060 tty00 12:05 /net/pppd

This tells you what the Process Id of the given process is, where it is being run, at what time it started, and what command is being executed. To get more details use ps -f, this will give you the userid which started the process, the parent process which spun the child process, and a few more ... There is also the ps -l option which is an extended version of the output ps provides, all the extra information given with the -l option is really of use just for system admins, you can have a go at it but it won't be of much use. Further useful options ps offers are also available threw the command line options of ps. Use ps -e to get details about every process running, ps - u userlist gives details about all the processes run by the given user. Use ps -t ttynn gives details about all processes run on the given device, you can replace ttynn by any valid device listed in the /dev directory.

Process Execution
To suspend the execution of a process to a later time use sleep time replacing time with the time in seconds to wait before executing the process.

To run a process after another process use wait PID where PID is the process Id to wait for. After that process is executed the specified process will run. If you do not give a PID your process will wait for all other background processes to finish executing first.

Background & Foreground Processes


To run a process in the background use bg commands, where commands is what you want your shell to execute. Only non- interactive processes can run in the background and interactive process will be just suspended. To bring a process to the foreground use fg process, the process will then run in the foreground. You can use fg to bring to the foreground any suspended process or any process running in the background.

Background Processes (Advanced Use)


At certain time the need to leave a process running when you log off. To do this the nohup command is availabe. This will in effect start the process and let you log off ignorinag any hangup signals. Use nohup command args... &, this will start the command you specify and pass arguments to it (if any) leaving the command to execute when you log off. The & sign is required because the nohup command will not cause the process to run in the backgound automatically, so the & is used to start running the process.

Killing Processes
It might, at some point, become necessary to kill/terminate a process. This is most commonly used to terminate a process which is no longer responding, or to kill all processes before logging out. Leaving running processes which are practically useless will slow down your system, so make sure no processes are left running. This is just a case of cleaning up after use type of situation. Use kill -s SIGNAL PID to send a signal to the given PID. If you do not know which signal to use type kill -l to get a list of all available signals. To kill a process you would use the KILL signal as such: kill -s KILL PID, this would in effect terminate the process indicated by its PID. To find out what signal terminated the previous shell command use kill -l $?. Note that indiscriminate use of kill can get you into trouble, and that you will only be allowed to kil you own processes.

Commands covered in this section: ftp trn mail elm pine lynx netscape

Transferring Files
In this section we will introduce many of the most common networking utilities provided by UNIX. The first and most commonly used is ftp.

is a command interpreter which has as primary function to transfer files from one computer to another. The most useful function of ftp is what is know as anonymous ftp, which allow files to be exchanged between a number of users and the rest of the world, over a public connection. Most FTP servers will provide a directory named /pub under which are located all the publicly available files for that site. The fundamental steps for retrieving a file using an ftp connection are as follows:
ftp i. ii. iii. iv. v. vi. Open a connection to the server Log in Find the file Retrieve the file Log out Exit ftp

Here is a summary of the ftp commands, because ftp acts on both the local and the remote computers there are two sets of commands. We will here discuss each type of command separately.

Connecting & Controlling Transfers


open - used to open a connection, type open hostname port. The port is optional, if you are given a port use it, otherwise don't. user - Used to log into the remote computer, type user userid password. If you omit password the remote computer will ask you for one. (see notes bellow for public access). ! - suspends ftp session and starts a subshell, the shell started depends on the SHELL environment variable or if SHELL is not defined will default either to sh or to your login shell (depending how your system is set up.) ? or help - Used ? command or help command to get help on a specific command or don't specify a command to get a list of available commands/options. status - Displays status of ftp and show the sessions opened, transfer mode as well as the settings of various toggles. close or disconnect - Disconnects you from the current remote computer, but remains in ftp. bye or quit - Disconnect from remote computer and exits from ftp completely.

Setting various ftp Toggles/Options


The following commands are used to set various ftp toggles, this will effect how ftp transfers files as will as how the connection is effected. To change the state of a toggle use toggle on (or off). ascii - Set to on by default, will cause all transfers to be made in ASCII mode.

binary - Set binary to on, when transferring any non ASCII/text file. If you do not set binary to on and transfer a non-ASCII file it will be corrupt and useless, you have been warned! glob - Set this toggle to on if you are planning to use wildcards as *.*, if this is set to off ftp will attempt to retrieve/send a file called *.* and not all files in the current directory. case - Set to off by default. If you set it to on all filenames which are in upper case will be in lower case when transferred.

Remote Directories
The following commands are available to deal with the directories on the remote computer. pwd - Displays current remote directory cd dirname - Changes remote directory to directory specified cdup - Chages directory to parent of the current directory, equivalent to cd .. dir remdir - List files in either current remote directory or if you specify a directory, in the directory specified. The listing will be in an equivalent format to ls -l ls remdir - Same as dir but the listing is in an equivalent format to ls -a, it will show only the files, without attributes, but will include all files which commence with a . mkdir dirname - Will create a new directory on the remote computer. rmdir dirname - Removes directory specified on the remote computer.

Local Directories
lcd dirname - Will change the local working directory, you can use the usual cd options (such as cd .. would be lcd ..)

Remote Files
Use these commands to manipulate remote files. get filename - Retrieves file specified and stores it under the same name in your current local directory. If you want to store the file under a different name use get filename newfilename, where newfilename is the name you want to store the file under. mget file1 file2 file3 ... - Retrieves multiple remote files, you can specify as many files as you want. Wildcards can be used if the glob toggle has been set to on. send file or put file - Will send the file specified to the remote computer where it will be stored in the current remote directory. If you want to store the file under a different name use: store file newfilename. mput file1 file2 file3... - same as put but store multiple files on the remote computer, the same rules apply here as to the mget command. delete filename - Will delete file on remote computer mdelete file1 file2 file3... - Deletes multiple remote files rename filename newfilename - Renames file on remote computer to newfilename.

chmod mode filename - Changes access mode for specified file. Note that the mode needs to specified as a octal value, ftp will not accept the ASCII settings (as u-r a-x ...).

Reading NewsGroups
Just as ftp, internet newsgroups is a great way of communicating with other users, as well as distributing programs. Because of the complexity and number of uses/options of trn we will here only cover the basics, leaving you to explore the rest. The major difference between trn and other command line news readers is the fact that trn supports news threads. What this means is that when a new article is posted to a given newsgroup, it starts a new thread and each subsequent reply is a child to the main one. This acts in much the same way as the UNIX directory tree, where the first message is the parent directory and each reply to that message is a subdirectory to it.

Upon starting trn, your .newsrc file will be read (if it doesn't exist it will be created), and the newsgroups you have subscribed to will be displayed as: ====== 17 unread articles in warwick.help -- read now? [ynq] ====== 07 unread articles in alt.binaries -- read now? [ynq]

At this point you can either type y to read this newsgroup (in which case messages are displayed sequentially.), n to skip the group and move onto the next with unread messages or q to exit trn completely. Other options available are: by typing + to enter newsgroup threw the selector, = to list headers before displaying articles. To find a given newsgroup use /pattern where the pattern is the newsgroup string you seek (this will search forward threw the newsgroup listing, and find the group matching your string). To search backwards use ?pattern. Typing l string will search for a newsgroup containing the string you specified, threw the groups you haven't subscribed to (ie: including those that are not listed in your .newsrc file). Use the u command to unsuscribe from a newsgroup, and c to mark a thread as read.

Using the Thread Selector


If you entered a group using the + command and the -X option at the command line, you will be able to read the groups in a selective way, this is particularly useful if you only want to read a small number of messages. To start reading a given thread, select an item to read use the letter or number specified, and hit CR. If you do not select a thread first the first unread one will be selected. To mark an item as killed, use either k or a comma. To navigate the threads the following keys are available:

Both the /pattern and other search features used to search for a given newsgroup are available here to find threads/articles. You can get help on any command, at any stage by simply typing h, and you can escape to a subshell using The ! command. Typing a q will get you out of the current level, and bring you to the previous, so if you were reading an article and typed q you would return to the thread selector, and a q there would take you back to the newsgroup selector.

Electronic Mail
Email is most probably the best known service the internet offers to its users, practically everyone has an email address (referred to some as e-address,for electronic address). This is in the following format: [email protected] Here are the different types of hostnames: hostname.ac.uk - Defines an academic institution email hostname.co.uk - A company based in the UK hostname.com - A US based company hostname.org - A non-profit making organisation hostname.gov - A government organisation hostname.net - A network host (most major ISPs have .net extension) Let us take an example [email protected], is my email at ukonline. My userid is frankie.b2 and my host is ukonline, the final extension .co.uk specifies ukonline to be a company in the UK. Another example is Matt's address [email protected], his userid is csuoq, the main mail server is csv, and the host is warwick. The .ac.uk extension specifies warwick to be an academic institution.

UNIX Mail Readers


Mail
mail is a UNIX utility available for sending and reading of email. Use mail -t recipient, to start mail sending a message to the person specified as recipient. The message to be sent will be read from standard input, until you type a . followed by a CR. on a line of its own. Or if reading from a file it will terminate when and EOF is read. When the message is sent the following header information is generated: From sender date_and_time The form header will specify your email address, the sender will specify the address of your machine (usually just the hostname), then the date and time the message is sent. Some mailers include more information in the headers, such as the name of the mailer, a reply to address... Note you can use any of the redirection features of UNIX with, mail. So for example, use mail -t [email protected] < file1 to send file1 to john Reading mail is just as simple, use mail -h to display a window of headers instead of the latest message, or you can use mail -r to display messages in a first in first out order. To view mail using another file rather than the default use mail -f filename. If you use mail without any options you will get a list of messages, in a last in first out order. When reading messages use, these commands

Print previous message + or n or Space Print next message ! Escape to shell d Delete current message and move to next u Undelete previously deleted message h Display message header r user Reply to user s filename Save message to file (default mbox) x Put all messages back in mailfile without changes and exit ? Print command summery (help)

Elm
Elm works in much the same way as does mail, however the user interface is a bit better and it offers you menus as well as a list of available commands for each section as you enter it. Elm is just as extensive if not more than mail, you can invoke elm in three main different ways. Using elm without any arguments will start elm in am interactive way so as to allow you to read, send, forward mail,.... You can start elm using elm -s hello mark, this would start elm directly in the editor (see bellow for details about mail editors), where you would be sending a message to mark with the subject hello (-s is used to indicate the mail subject, if not specified on the command line you will be prompted for one). The third most common way to start elm is to use elm -s sendingdata mark < file.h. This would in effect mail the file called file.h to mark with the subject being sendingdata. Note that here mark is used as an alias (aliases can be defined from the options menu in elm), you can use a full internet/uucp address instead, so that elm -s hello [email protected] < file.h you also be a valid command line invocation of elm.

Other useful command line option to elm are as follows, elm -h or elm -? will give you a listing of all options as well as their use, elm -f filename will read mail from the file specified instead of INBOX, elm -i filename will cause the file specified to be included in the editor as part of the outgoing message, and elm -z will check if there is new mail, if not elm won't start.

Introducing Pine
Like mail and elm, pine is an Internet mailer, but pine is also a newsreader. Reading news or email is achieved in exactly the same way, with the exception that when reading mail, it will be read from either the INBOX or a mail folder, but the news is read either directly from the news server or from a news spool file (depending on your set-up). Pine is designed to be highly interactive, it is very easy to use, and the menu navigation is well implemented. All you really need to do is use your arrow keys and the spacebar or CR to navigate threw the messages. One major advantage to pine is that it will flag your messages as read, answered, urgent, important or new. You can set your own flag by using the + when reading your messages and then execute macros on them.

To start pine just type pine. If you want to start pine directly in the editor, use pine eaddress where eaddress is the internet address of the recipient (pine does not deal with UUCP mail properly, if you want to use UUCP use elm). If you use pine -f folder, this will cause pine to read your mail from the specified folder instead of the default mailbox, pine -i will start pine in the FOLDER INDEX so that you can start selecting mail to read immediately, pine -h displays all commands available as well as their usage, pine -F file will display file in pine's editor, pine -sort sort string will cause pine to sort messages in the order specified (valid orders are, arrival, subject, from, date,
size, orderdsubj or reverse, add /reverse if you want to sort in reversed

order).

There are many specialised functions in pine as well as in the other mailreaders, to find those out read the online help files provided with each mailer.

Editors
To change the editor used by the mailers you will need to set an environment variable. EDITOR=whatever editor you like to use can be any valid editor such as those described in the former sections. EDITOR=emacs or EDITOER=jove are but two examples. Note that when using pine the default editor has a wrap facility, which can be very useful.

The World Wide Web


The WWW is an internet form of document/information distribution which is growing in both popularity and importance, initially it could handle on text, but has now expanded to contain not only graphics but sounds as well as fully interactive contents, such as programs, grams, videos.... There are many browsers about, but here we will just briefly mention two, Lynx and Netscape. Lynx is a text based browser and can handle only textual documents, ignoring anything else, as such it has greatly decreased in popularity, but it is still used on systems which do not support full graphical displays. Type lynx, and you will get a hypertext document describing how to use it as well as a list of links where you can find more detailed information about the browser. Navigation is relatively simple use the numbers keypad as illustrated in the diagram:

To go to a specific URL use g, this will bring up a prompt asking you for the url, type it in and press CR. Lynx will then open the documents for you. Use m to go back to the starting document, i to view an index of documents and o to go to the options menu from where many other more details functions of lynx can be used. Netscape will not run under the command line in UNIX you will need to use the X Windows System to be able to run Netscape. Once under X simply type netscape or on some systems you can use a specific version of netcsape by adding it to the name of the executable as netscape-2.0 (this will start version 2). Netscape supports all the latest tricks of the HTML trade, such as sound, video, executable content and scripting. Because of its graphical nature navigation is very simple. Use the scroll bars to scroll threw a document, the reload button to reload a page, and type the URL in the box provided, or simply use the Open Location (from the file menu) to open a URL. You can use Open File to open a local file. One thing to note when using Netscape, is that because most web pages are highly graphical in content, netscape will eventually use all the colours available on your system, and starting any other application which requires colours will result in either distorted colours or the application not starting. To prevent this type the following when invoking netscape netscape -install. This will prevent netscape taking all the colours and you may use other applications which requires colour (such as a paint program).

Netscape also allows you to read your Mail and News from within its environment, to use these options you should select the mail and new preferences option from the options menu, to set your email (found under the Identity tab) and the specific servers to use (These might be set-up for you already but you will still have to enter your email address). To learn how to use Netscape in depth just use the options provided in the Help menu, which will take you to on line documentation and tutorials.

Commands covered in this section: rlogin telnet sz rz ping finger who rwho talk write mesg

Logging into Remote Computers


There are times when you will need to log into a remote computer to do this there are several commands available on the UNIX systems. Use rlogin hostname to log into the computer called hostname. You will be accessing that computer as if it was the machine you are using, this means that your environment settings will be effective for that rlogin (the shell that account is set-up with will be operational, so if you are using zsh and that account is set to use bash on login you will be using the bash shell). On occasions the need may arise for you to log into a remote host using another userid. to do this use rlogin -l userid hostname. This will allow you to log into hostname under a different identity, which will remain effective during that session. Note: Some UNIX systems inverse the order of the hostname and userid so if the example given above does not work use rlogin hostname -l userid. You will need to have a password for the account you are trying to log into or have permission to use it without a password.

Using Telnet
Telnet is a well known internet utility and as such it is available on most systems, the most important difference between rlogin and telnet is that you cannot use rlogin with PC based networks, it will only work on UNIX networks. To access PC networks use telnet

Type telnet hostname to connect directly to the machine specified by hostname, if you want to connect to a specific port type telnet hosname port (note, space required). The hostname can be either a full name such as crocus.csv.warwick.ac.uk or it can be a simple IP address as 128.83.165.133 If you just type telnet without supplying a hostname you will get the telnet prompt which looks as this: telnet> at this prompt you can use any of the telnet commands described bellow. Note: To use any of these command s you will need to be at the telnet prompt, if you are in a telnet session to escape use Ctl & ] to get to the prompt (you connection to the remote computer will not be lost).
Command open o Short Format Parameters hostname Action Opens a new telnet connection to remote computer

(port) close quit status c q st none none none Closes your current connection to remote computer and returns you to the telnet prompt As close but exits from the telnet utility completely. Displays telnet status

Advanced Telnet Use


Telnet provides may option allowing you to make more advanced use of the networking facilities offered by UNIX, we will here describe a few of the most commonly used. z allows you to either suspend telnet and return to your shell or to start a subshell, this will mostly depend on your system set-up, but the end result will be practically the same, you will be working in your sub/shell while the remote connection is suspended. On most UNIX systems this means that you will be able to use rz or sz to receive (or send) files via the Zmodem transfer protocol.(see bellow) m or mode Will allow you to change the modes of transmission between the two computers. This will effect the speed of transfer as well as the way the data is transferred(note: both computers need to support these option for mode to work, try it, if it starts complaining, it doesn't support that mode!). mode c will cause transmission to be in the character form, so that each character you type at your terminal will be immediately transferred to the remote computer. This mode is very efficient when you are telneting to a computer located on the same network as your own, or one that is very close. if you are communicating with a computer that is far use the next mode. mode l sets the communication to be in line mode. This will cause telnet to wait until you have pressed ENTER before transmitting, and it will then transmit the whole of your line. This is faster as all your data is sent in as little data packets as possible.

sz amp rz
These two utilities are used in conjunction to telnet to send and receive files using the Zmodem Protocol. Use the z telnet command as described above, then type binary on to switch from ASCII transfer to binary transfer, start the rz in the background by typing bg rz. This will cause the system to wait for a Zmodem transfer. Then bring the telnet session back to the foreground (see section on Controlling Processes for details on how to use fg bg, ...) and start the sending the file in Zmodem. Because of the wonders of the Zmodem Protocol all the necessary setting will be in set-up by the two computers and the file transmitted, once the rz job is finished your file will be received. To send a file use the same procedure but type bg sz filename, and instruct the remote computer to receive the file.

Computer Status
Telnet connections refused? Unable to rlogin? The computer you are trying to access might be down. To find out if it is the remote computer or your utility type ping hostname . This give you either hostname is alive or hostname is not responding (some say dead!). If when you type ping hostname you get no response for a long time, then the remote computer is either dead or remote connections have been disabled. Try later!

Finding all about a Remote User


There are time, when we need to (or would like to) know as much as possible about a given user. To do so use finger userid This will give you something like:

[localhost] Login name: lauvj In real life: Mr F Blaskovic Directory: /home/crocus8/la/uvj Shell: /usr/local/bin/zsh Mr F Blaskovic (lauvj) is not presently logged in. Last seen at crocus on Sun Aug 18 12:39:05 1996 from somewhere.com Mail forwarded to someplace.on.the.net

This is the long version of the output given by finger. (which is usually the default on localhosts). If it isn't the default use finger - l userid, or if it is the default and you want the short format use finger -s userid. If you have a file called .plan or .project, those will be displayed. Note in the above example there is a reference to main being forwarded followed by the address to where the mail is forwarded. This will be displayed if the user has a .forward file in their home directory. If you don't want to wait for finger to display the .plan, .project or .forward files use finger -p userid. Use finger -options userid@hostname to find a user at hostname, or any user that matches the string userid. If you are on a network with several hosts instead of using finger userid, use finger -options userid@ this will search all your LAN hosts machines. Use finger -options @hostname to get a listing of all users on hostname

Finding Users with who


To find out about users, processes on your machine use who. who will display a list of users in this format: root -tty03 Aug 20 03:42 frankie +tty01 Aug 20 10:22 matt +tty20 Aug 20 19:03 ops -tty00 Aug 20 00:00 As show the output will include the login name of the users, the tty, date and time. If you use who -T, before the tty a + or - will be displayed (as above), this indicates whether you can write to the terminal or not (a + means that you can write to the terminals and - you can't). The who -m will show you the equivalent of the who am i command, all the information you need about yourself. Another useful option to who is -H which will put a header above each column of the output.

who for Remote Users


The who command can be used in the same way as you would use who, but will display information about remote users instead of local ones. Use rwho hostname, to get a list of all users logged in at hostname. To get a listing as that which is produced by who, type rwho -l hostname. And a useful switch is -a which will produce a report on the specified machine even if no users are logged in.

Talking to Other Users


If you need to talk to another user use talk. First you will need to know at what machine the person you want to talk to is logged on (use finger, rusers, ... to find out).

Type talk userid@hostname where userid is the login name of the person you want to talk to and hostname is the machine where s/he is logged on (don't forget the @ sign!). If the person you want to talk to is still logged in s/he will get a message that looks like: Message from Talk_Daemon@darkstar at 16:00 ... talk: connection requested by user@darkstar. talk: respond with: talk user@darkstar

To respond you would type talk user@darkstar, your screen will then split in two. All you type will appear in the top half and what the person talking with you type will appear on the bottom part. You will quickly notice that your screen becomes a mess, to clear it use Ctl & L. To quit from the talk connection use Ctl & C. This will terminate the talk session and return you to your shell.

Using Talk Across Networks


To use talk across networks you need to know the machine name as well as the hostname for the person whom you want to talk with. Best way to find that out is to ask them! Alternatively use finger. type talk [email protected], for example you would use [email protected] to get in touch with me. Notice how you put the user name followed by the @ sign, the name of the machine (crocus), and then the hostname (here warwick.ac.uk).

Using Write
To just write a message on another user's screen use write userid or optionally write userid ttynn, where nn is the tty number the user is using. After you typed in the command type in any text/messages you wish to send. then end it with a Ctl & C. That person will then receive something like: Message from yourid yourtty The message you typed in.

Stopping Messages
Some times you just don't want other people disturbing your work, and having talk requests and messages popping up all over the place can be very disturbing. To stop any messages use chmod -w /dev/tty or you can use mesg n. This will prevent you from being disturbed by the talk/write messages. Note: These commands won't stop any messages from the superuser, the only way you can stop these is, not being logged on!

Commands covered in this section: gzip compress tar gunzip uncompress gzcat

Saving Space

However much space you've got, it's still limited, and one day you're going to wish you had more. You should regularly clean up your filespace by removing unwanted files, but there are usually some things you don't use frequently, but still want to keep, such as completed assignments or old e-mail messages. These are perfect candidates for compression, a way of reducing the size of files using complex algorithms that take advantage of patterns in the file. This means that some files compress better than others, for example plain text is excellent, due the limited character range and repeated groups of letters found in natural language. Text files can typically be reduced to a third of their original size. Image files, however, often hardly compress at all, as their format (such as gif or jpeg) already uses compression. There is little point trying to compress an already compressed file, as any uneven distributions of characters and repeated sections have already be detected and replaced by a more compact encoding. You can either compress files individually or collect groups of files or directories together and compress them into a single file. Each file actually takes up a certain minimum amount of space in the filesystem, regardless of its given size in bytes. This is known is the block size, and could be 1K, 4K, or more. Any file smaller than this will still take up this amount of space. Therefore compressing small files is pointless, but if they are grouped together and compressed into a single file, they will take up much less space, because the large file will use the blocks more effectively, in addition to being compressed.

Compressing files individually


There are many programs for compressing files, and one of the most popular is gzip. To use it simply give the name of the file, or files, to compress, for example gzip largefile. This will create a compressed file called largefile.gz, deleting the original file. Another popular choice is the old compress utility, which creates files with a .Z extension, although it rarely compresses files as well as gzip.

Compressing files collectively


This is a two stage process; the files are grouped into one large file, which is then compressed as discussed above. The grouping can be performed using the tar program (for Tape ARchive, although a tape device doesn't have to be used). You need to specify -cf as a option to tar, to Create a File archive (not a tape one). This is followed by the name of the archive you wish to create, usually with a .tar extension. Finally you list the files and directories to be included in the archive. If a directory is given it will be added to the archive, along with its entire contents, including sub-directories. It is often best to put all the files you wish to group together into a directory and then you need only specify this directory. Also, when you extract the contents of the archive, the directory will be created (if it doesn't exist), so all the extracted files will be contained on their own in a single directory. For example, you have a directory called project which contains everything you wish to compress. You could then do tar -cf project.tar project. It is a good idea to use the directory name as the archive name, after adding .tar, so you can tell the contents of the archive from its name. This archive will be similar in size to the sum of the individual file

sizes, as no compression has been performed yet. Therefore the last step is to do gzip project.tar, which will replace the file with project.tar.gz, which may be considerably smaller than all the individual files. You can perform the whole operation using a pipe, by doing tar -cf - project | gzip > project.tar.gz. Specifying a dash as the archive name causes tar to send the archive to standard output, which is then piped to gzip which reads it as input, redirecting the compressed output to the required filename. Note that you can also use this technique to create backups of important files and directories. It conveniently groups files together, and compresses them to save space. Also note that sometimes the extension .tgz is used as a short form of .tar.gz.

Uncompressing files
Of course, there's little point compressing files if you don't know to uncompress them, but this is a simple matter. To uncompress largefile.gz simply do gunzip largefile.gz. This will replace the compressed file with the uncompressed file largefile. Using gunzip is equivalent to gzip -d. As with many UNIX commands you can specify a group of files using wildcards, so gunzip *.gz will uncompress all the files in the current directory that have been compressed using gzip. If you used the compress command, the process is reversed with the aptly named uncompress command. To extract the contents of a tar archive, after uncompressing it if necessary, use tar with options -xf, for eXtract from a File archive. So, for example, tar -xf project.tar would create a directory called project, if it doesn't already exist, and extract all the files and sub-directories into it. Again, a pipe can be used to combine the two operations of uncompressing and extracting by doing gunzip -c project.tar.gz | tar -xf -. The -c option (where available gzcat and be used in place of gunzip -c) writes the uncompressed contents to standard output, leaving the original file unchanged, and the dash on its own is used in this case to specify standard input as the source of the tar achive. Keeping the original file unchanged is often useful as less space is required for the operation and if you want to keep the file you'd have to compress it again, if you had performed the operations separately.

Commands covered in this section: man

The On-line Manual


Each system has its man (short for manual) pages, they usually have detailed information on what a given command does, its syntax, any optional settings, the output it produces, and the various context in which it can be used. But this is not all the man pages can do for you. They also have details of system calls,

functions/routines used for UNIX programming and file formats, in a few words you learn all your UNIX by using the man pages and by practise. So use the man command and learn....

Reading a man page


Nothing simpler! (Who said UNIX was complex), type man name, where name is the name of the command you wish to know more about. This will display the manual page for that command on your screen. You might want to keep the page for future reference, if this is the case type man name | col -b > filename. This will convert your manual pages to ASCII format and then store the page in a file for you.

Man for Programmers


What usually happens is that man displays only the first manual page, this is because the others, in most cases, contain details of how the command is used in a shell script/program. You might want this information, to display all the manual pages for a given command use man -a name.

Another useful feature of man is that if you use man -w name, man will display the location of the manual pages for the command specified by name, this information can then be used as the need may be.

Searching for a Command


If you are looking for a specific command but either can't remember its name or spelling use man -k keyword. This will cause man to search a database of on-line descriptions of manual pages and to display any matches found, man -k will tell you all you need to know on how to use man. You can also use man -h to give a help message and exit.

Advanced Use of man


By default man will use the more pager. You can change the pager man uses by typing man -P pager name, where pager can be any pager that is locally available (for example less) and name is the name of your command. Another useful feature of man is the -M option. Use man -M path name, where path is a path to look for the manual pages, and name you command. This feature is particularly useful if the manual page you are looking for is not in your MANPATH, or is on another system.

Environment variable for man


The following three environment variables effect the functions of man, you can override then by using the appropriate command line argument, but if you are planning to use these value often put them in the environment variables!

PAGER = the name of the pager to use for viewing of man pages MANPATH = A list of path names, separated by colons, indicating the directories to search for man pages. MANSEC = A list of manual sections to display (also colon separated). Changing the values of any of these will effect how your man pages are found, displayed and which section of the man pages are displayed!

Copyright 1996 to Matt Chapman & Frankie Blaskovic - All Rights Reserved Last modified: Sun Feb 10 20:47:50 GMT 2002

You might also like