Unix By Example

Douglass Schumacher
(so now you know who to ask if you have questions)

 

·          Introduction

·          Conventions

·          Getting Started

·          Directories and files (cd, ls, cat, less)

·          Redirection of output (>, >!, >>)

·          Job control (&, fg, bg, ^Z, jobs, ps)

·          Some basic commands (cp, mv, rm, rmdir, chmod)

·          Regular expressions (?, *, [])

·          More on command execution (aliases, which, PATH)

·          Next steps

 

 

 

 

Introduction

 

This document provides a brief description of the Unix operating system, its use, and the department computer system that runs it. The goal is to describe enough of the system so that a new member of my group with little or no prior experience with Unix can begin to get to work. It is assumed that the reader has some experience with PCs or Macs and understands the idea of a directory system and simple file manipulation.

 

You must have a Unix account to access the Unix department machines. One account suffices for all machines. You must also be made a member of my computer group.See Bryan Dunlap (Rm. 2174, 2‑3402, bcd@mps.ohio-state.edu). Ultimately, you should also have your home directory moved to my disk partition so you don’t have to contend with disk space limits, but that can wait if you are just trying my group out.

 

All of the following discussion is simplified and restricted. There are exceptions to every rule and many important variations on a theme. In particular, every basic behavior described can be customized if you don’t like it. No attempt is made to indicate any of this in the interests of clarifying the discussion. If you find yourself wishing there was a better way to do something, just ask. You might get lucky.

 

My group uses PCs running MS-Windows and department workstations running Unix. I’ll just refer to Windows and Unix, respectively. You are not allowed to read e-mail from a PC running a PC based program such at MS-Outlook. Instead you must access one of the Unix machines using one of our PCs and use a Unix based mail reader, such as pine. Data collection is performed using PCs. Analysis and numerical work is done on the PCs and Unix machines. We also have access to supercomputers, and these run Unix.

 

When you use a PC, it makes sense to think of the keyboard and monitor as being part of the PC. The processor, keyboard, and monitor are all part of one machine. Unix is a multi-user operating system and does not have a given monitor that it uses for output. All of our PCs have software that allows the PC monitor and keyboard to act as if they are connected to the Unix machine. In fact, you can have sessions with many different Unix machines going at the same time as well as your usual interaction with MS-Windows.

 

Although the department has many machines, you should only use campbellto begin with. The other machines will come into play once you begin to do intensive numerical work.

 

You can have a graphical Windows-like connection to a Unix machine or a simpler one. For a simple connection, use SSH Secure Shell Client. For the graphical connection use exceed. Ask someone for help running these programs for the first time. This tutorial will work using either one since it does not cover any aspects specific to the graphical connection. You should know how to use both programs, however.

 

In either case, after starting SSH or exceed, your interaction with the Unix machine takes place in a window. If you use the graphical interface, the Unix windows all appear inside this window. Initially, there are several Unix windows, each with a different function. The biggest one is for typing in commands to Unix. This is where you should start.

 

Unix uses a tree directory system, like Windows. Unlike Windows, the disk drive is not part of the filename. Each user is given his or her own part of the tree, called the home directory and your home directory can have as many sub-directories as you like. For example, you start off with a sub-directory called public_html that is where you put your web pages if you decide you want your own web site. After you have logged on to Unix, you start off in your home directory. The full pathname to my home directory is /20/dws, but I rarely need to use this information. If fact, it is a bad idea to explicitly use the full pathname.

 

 

 

Conventions

Following are the conventions used in the rest of this document. Commentary and instructions are in italics. The Unix prompt is indicated like this:

>

In actuality, there will be some text before the > which we’ll consider later. If you are supposed to type in text you will see:

> text you should type in.

In cases where Unix’s response is given, that will be shown this way:

Unix printed this.

And this.

And finally this.

Note that the Unix output shown in this tutorial may look different than what you see depending on the width of your window. Longer windows allow longer lines. Also, some of the output is user dependent.

 

 

 

Getting Started

 

Unix can be customized.  The first step is to make some customizations that will simplify your work. I won’t describe the customizations you’re going to implement. Suffice it to say that they can be undone or be modified once you learn more about Unix if you like. If you have already added your own customizations, these may be modified, but then you don’t need this tutorial anyway.

> cp -r ~dws/group ~

> cd ~/group

> INSTALL

Now logout from Unix, for example by killing the SSH or exceed window, and then log back in to implement the customizations.

The tutorial will proceed by your typing instructions to Unix and looking at the outcome. Let’s call a line of instructions a ‘command line’. Here are some simple ways you can edit the command lines. In the following, text like ^U means you should hit the ctrl and U keys at the same time.

^H

delete the character before the cursor

^D

delete the character on the cursor

left-arrow key, right- arrow key

move the cursor forwards, backwards

^A,
^E

move to the beginning,
end of the line

^U

delete the entire line - start over

^P,
^N

go to the previous,
next command line

The main text editor used in my group is emacs. Unix accepts a number of emacs control keys for command line editing.

Finally, all the sections, with the exception of the following section on directories and files can be worked through at any time. You don’t have to go through the entire tutorial in one session. The next section should be done now. It is designed to be used immediately after you log on.

 

 

 

Directories and files (cd, ls, cat, less)

 

Let’s start with your prompt: campbell ~>.

campbellis the name of the machine you are logged onto. It is possible to have many windows open, each carrying a session with a different machine. After the machine name, the prompt lists the current directory, in this case ~. The ~is shorthand for ‘the home directory’. The > tells you where the prompt ends and your commands can begin.

campbell ~> cd group

Following the convention of this tutorial, the above means you should type in cd group, which means to change directory to group. Note that your prompt changes to reflect the change in your current directory: campbell ~/group>.

Group is a special directory containing directories and files for use with this tutorial. You created it earlier when you typed in cp -r ~dws/group ~. Feel free to delete it when you are done.

campbell ~/group> ls

INSTALL*      example_dir/

ls means to list the contents of the current directory excluding hidden files (more about those later). We see that group contains a file called INSTALL. The trailing * means that INSTALL can be executed. That is, INSTALL contains instructions that tell Unix to do something. You used INSTALL to install some customizations at the beginning of this tutorial. example_dir is a directory, as indicated by the trailing  /.

Here is a diagram of your directories:


 


campbell ~/group> cd dir1

dir1: No such file or directory.

campbell ~/group> cd example_dir/dir1

campbell ~/example_dir/dir1> cd ~/bin

campbell ~/bin> cd ~/group

cd dir1 failed because the directory dir1 is not in group, it is in example_dir. Note it was not listed when you ran the ls command above. cd example_dir/dir1 worked because the path to dir1 from group was given. When you enter something like, example_dir/dir1, you are specifying a “path”. A path specifies how to get from the directory you are in to the file or directory you want. Note that entering cd ~/group/example_dir/dir1 will always work, no matter what directory you are in. Here, ~/group/example_dir/dir1 is called a “full path name”. It specifies the absolute location of dir1, as opposed to its location relative to the current directory.

Henceforth, for brevity, I will only show the prompt as >.

A little bit more…

> cd

Changes to your home directory.

> cd -

Changes back to the last directory you were in. You are back in group now.

There are two special directory names.
.           means the current directory.
..          means the directory before the current directory.

> cd example_dir

> cd ..

> cd .

 

Note the effect of each command. Now, let’s work on the ls command some more.

> cd example_dir

> ls

a                c                count.c          dir2/

b                count*           dir1/            example_script*

So, we see two directories, two executable files, and four ordinary files.

> ls -l

total 44

-rw-r--r--   1 dws      dws            0 Jun 10 17:48 a

-rw-r--r--   1 dws      dws           15 Jun 10 17:48 b

-rw-r--r--   1 dws      dws           29 Jun 10 17:48 c

-rwxr-xr-x   1 dws      dws        24576 Jun 12 14:32 count*

-rw-r--r--   1 dws      dws          947 Jun 12 14:32 count.c

drwxr-xr-x   2 dws      dws         8192 Jun 14 07:19 dir1/

drwxr-xr-x   2 dws      dws         8192 Jun 14 07:47 dir2/

-rwxr-xr-x   1 dws      dws           45 Jun 10 17:50 example_script*

Options that modify program operation usually begin with a dash in Unix. The -l option instructs ls to give a long listing. total 44 gives the total size of the items in the directory being listed in kB. After that follows one line for each file or subdirectory. Working from the back we have: the name, the time and date of its last modification, its size in bytes, the group the file bellows to (my group is called dws), the user it belongs to (me, in this case), the number of links to it (links are not discussed in this tutorial) and the file type and permissions.

Type and permissions: The first character is a or a d if the item is a regular file or a directory. The remaining characters come in three groups of three specifying the read/write/execute permissions for the user, group, and everyone else.  Examples:

count.c I have permission to read and write (rw-), but my group can only read (r--), and everyone outside my group can only read(r--).

count I can read, write and execute (rwx), my group and everyone one else can read and execute (r-w).

> ls ~

This lists the contents of your home directory excluding hidden files. Any file that begins with a dot (for example, .cshrc) is hidden and is called a “dot file”. Generally configuration files for Unix and its programs are dot files placed in your home directory. Hiding them keeps them out of the way and prevents accidental modification. You can list hidden files using the -a option.

> ls -a ~

Notice that . and .. are listed now.

Time to look at the contexts of our text files in example_dir. cat simply prints the contents of a file to the screen (cat stands for concatenate).

> cat b

this is file b

> cat c

this is anther file

called c

> cat b c

this is file b

this is anther file

called c

> cat a

>

cat b c simply prints the contents of file b, then file c. File a happens to be empty, so cat a does not produce any output.

cat is useful for looking at small files and, as you’ll see later,  combining files. For larger files, cat is inconvenient. Try cat count.c. For viewing long text files I prefer a program called less. (Names in Unix can be obscure. I generally won’t try explaining them.)

> less count.c

less starts by printing the first screenful of the file. Navigation through the file is then controlled by various  key strokes.

f,b

forward, backward one screen

e, y

forward, backward one line

g,G

top of file, bottom of file

/,?

forward, backward search

N,P

next, previous file (if multiple files were entered on the command line)

q

quit

Try going forward one screen, and then forward or backward a few lines.

Now, as an example we’ll search for the text stderr starting at the top of the file: go to the top of the file (hit g); start the search (hit / ); enter the text to be searched for (stderr); and hit return. You can jump to the next instance of the search string by hitting n.

To practice on a longer file, try less ~/.cshrc. This file is the main file customizing your interaction with Unix. Note it is a hidden file.

 

 

 

Redirection of output (>, >!, >>)

 

This next section uses the program count located in ~/group/example_dir. count 5 yes counts to 5 in one second intervals, printing the count as it goes. count 5 no counts to 5 silently.

> cd ~/group/example_dir

> count 3 no

> count 3 yes

1

2

3

> count 3 yes >countout

> cat countout

1

2

3

The >countout means to redirect the output of count into a file called countout.

> ls

a                count*           dir1/

b                count.c          dir2/

c                countout         example_script*

Notice a new file is listed: countout.

> cat countout

1

2

3

countout contains the output of count.

> count 5 yes >countout

countout: File exists.

As a safety precaution, you can’t overwrite an already existing file using output redirection.

> rm countout

> ls

> count 5 yes >countout

> cat countout

1

2

3

4

5

rm is the remove file command. With countout removed, redirection to countout works again.

> count 2 yes >! countout

> cat countout

1

2

>! means to redirect output and overwrite countout if necessary.

> count 3 yes >>countout

> cat countout

1

2

3

1

2

>> means to append the output of count to countout.

 

> count xxx yes >! countout

> cat countout

Error. Unable to interpret 'xxx' as a number.

Note that error messages are not redirected to countout, but appear on the screen.

Here’s an example showing how cat can combine files.

> cat a b c >abc

> cat abc

this is file b

this is anther file

called c

Let’s end this section by cleaning up the new files we created.

> rm countout abc

> ls

a                c                count.c          dir2/

b                count*           dir1/            example_script*

 

 

 

Job control (&, fg, bg, ^Z, jobs, ps)

 

Unix allows you to run and manage multiple programs at the same time. It can be very convenient to run one calculation while analyzing the results of another at the same time.

> cd ~/group/example_dir

> count 2 no &

[1] 15249

>

The & means to run count in background. You’ll notice that you have been given the prompt and Unix is ready to take more commands while count is executing. The output line [1] 15249 means that the command count 5 no has been assigned job number 1 and process number 15249. As you can see, your output line is somewhat different since you have been assigned a different process number.

You can think of the job numbers as your own private list of programs running during your session. The job number is 1 since this is your first job. The process number refers to the list Unix keeps of all processes running by all users on the system. You’ll sometimes need this number as well.

Unless  you are a fast reader, sometime while you were reading the above count finished counting to 2 and the program completed. Unix then notified you of this by printing:

[1]    Done                   count 2 no

and then giving you another prompt.

For this next exercise enter the first command line, wait a couple of seconds and enter the second command line, and then wait until you get a prompt.

> count 5 no &

[1] 15671

> fg %1

count 5 no

>

The first line started count running in background again (note the job and process numbers and compare to last time). The second line,  fg %1, told Unix to move job 1 into the foreground. This means that count continues to execute as if you had entered count 5 no without the trailing &. Thus, no further command lines can be entered until count completes and you get another prompt.

A program cannot print to the screen while it is in background. If a program tries to do this, Unix halts it and notifies you.

> count 5 yes &

[1]  + Suspended (tty output) count 5 yes

> fg

count 5 yes

1

2

3

4

5

(Unix refers to your screen and keyboard as a tty.)

Programs run in background are usually run with their output redirected to a file so they can work uninterrupted.

> count 500 yes >countout &

[1] 11342

> jobs

[1]  + 11342 Running                count 500 yes > countout

The jobs command lists all running jobs. Going from the end of the line to the beginning we are given: the command line that started the job, the job’s status, the process number, a + or – or blank (we’ll ignore this), and the job number.

> count 200 yes >countout2 &

[2] 11758

> count 300 yes >countout3 &

[3] 9033

> jobs

[1]  + 11342 Running                count 500 yes > countout

[2]  - 11758 Running                count 200 yes > countout2

[3]     9033 Running                count 300 yes > countout3

> fg %1

count 500 no

Job 1 has been placed in foreground and has control of your tty. No more command lines can be entered now. We can change this situation by suspending the foreground job. Hit control-Z:

^Z

Suspended

> jobs

[1]  + 11342 Suspended              count 500 yes > countout

[2]  - 11758 Running                count 200 yes > countout2

[3]     9033 Running                count 300 yes > countout3

Now, let’s get job 1 running again in background.

> bg %1

Let’s kill all three jobs. We can terminate them using the kill command.

> kill %1

[1]    Terminated             count 500 yes > countout

> kill %2

[2]    Terminated             count 200 yes > countout2

> kill %3

[3]    Terminated             count 300 yes > countout3

If you end your session with programs running in background, they will continue to run but are no longer associated with a job number. Job numbers refer to jobs started in the current session. However, process numbers always work. (Although a little confusing perhaps, this arrangement is actually very helpful.)

Even if you didn’t bother to write down the process number after you first entered the command line (I never do this), you can look it up using the ps command.

> count 120 no&

[1] 21954

> count 240 no &

[2] 22518

> ps

   PID TTY      S           TIME CMD

 21437 ttyq7    S        0:00.40 -tcsh (tcsh)

 21954 ttyq7    S        0:00.02 count 120 no

 22518 ttyq7    S        0:00.02 count 240 no

Each line describes one of the processes you’re currently running. Describing each line starting from the end we have: the command line that started the process, the cpu time it has received, it’s status, the name of the tty it was started from, and the process id number. (The process -tcsh is your login shell, the program that takes care of reading in your commands and implementing them. It was started by the operating system when you logged in.)

Wait until the first process finishes. Unix will notify you.

[1]    Done                   count 120 no

Now, kill the remaining process using its process id number.

> kill 22518

[2]    Terminated             count 240 no

 

 

 

Some basic commands (cp, mv, rm, rmdir, chmod)

 

The first four commands in this section are:
cp        (copy) make a copy of a file or files.
mv       (move) renames a file or moves it from one directory to another.
rm        (remove) deletes a file.
rmdir   (remove directory) deletes a directory.

The operation of these programs, like most other Unix programs, can be changed drastically by entering options on the command line. As described earlier, options in Unix are introduced with a hyphen (-). For example, rm can be used to delete a directory and all its subdirectories using the -r option. cp can be used to copy an entire directory including all it’s subdirectories using its -r option. The ‘r’ in  ‑r  stands for recursive in these cases.

> cd ~/group/example_dir

> ls

a                c                count.c          dir2/

b                count*           dir1/            example_script*

> cp a copy_of_a

> cp -r dir1 copy_of_dir1

> ls

a                copy_of_a        count.c          example_script*

b                copy_of_dir1/    dir1/

c                count*           dir2/

> ls dir1 copy_of_dir1

copy_of_dir1/:

data1

 

dir1:

data1

In the following, the ls commands needed to see the results of the other commands are given but not the ls output.

Make copies of files a, b and c and place them in copy_of_dir1:

> ls copy_of_dir1

> cp a b c copy_of_dir1

> ls copy_of_dir1

Rename the directory copy_of_dir1 to renamed_dir.

> ls

> mv copy_of_dir renamed_dir

> ls . renamed_dir

Move the file renamed_dir/data1 to the current directory.

> mv renamed_dir/data1 .

>  ls . renamed_dir

Rename data1 to fileZZZ.

> mv data1 fileZZZ

> ls

Move fileZZZ to renamed_dir renaming it file.dat.

> mv fileZZZ renamed_dir/file.dat

> ls . renamed_dir

Clean up using rm and rmdir.

> rm copy_of_a renamed_dir/*

> rmdir renamed_dir

Now lets look at chmod, which stands for change mode. This command is used to control the read, write and execute access of files and directories.

> cd dir1

> ls -l

total 1

-rw-r--r--   1 dws      dws           16 Jun 14 07:17 data1

I have read/write access, my group and everyone else has read access only.

> chmod g+w data1

> ls -l

total 1

-rw-rw-r--   1 dws      dws           16 Jun 14 07:17 data1

Now I and my group have read/write access. The g stands for group. You can also use u for user (me, in this case) or o for others (everyone else). The +w means to add write permission. -r would mean to deny read access. +x would mean to make the file executable (you have to do this when you  write batch files, which are called scripts in Unix).

You can combine these in various ways to select any arbitrary set of permissions in a single command. The manual for chmod describes  how to do this. Unix manuals are discussed later. Let’s reset the permissions.

> chmod g-w data1

 

 

 

Regular expressions (?, *, [])

 

You can represents many files at once using regular expressions. This is very handy when you want to copy, move or delete a number of files at one time.

> cd ~/group/example_dir/dir2

> ls

a            data001.s    data004.ss   data008.q    data012.s    data019.q

aa           data001.ss   data005.q    data008.rr   data013.q    data019.s

b            data002.q    data005.rr   data008.s    data013.s    data020.q

bb           data002.rr   data005.s    data008.ss   data014.q    data020.s

c            data002.s    data005.ss   data009.q    data014.s    dd

cc           data002.ss   data006.q    data009.rr   data015.q    e

d            data003.q    data006.rr   data009.s    data015.s    ee

data.q       data003.rr   data006.s    data009.ss   data016.q    f

data.rr      data003.s    data006.ss   data010.q    data016.s    ff

data.s       data003.ss   data007.q    data010.s    data017.q    g

data.ss      data004.q    data007.rr   data011.q    data017.s    gg

data001.q    data004.rr   data007.s    data011.s    data018.q

data001.rr   data004.s    data007.ss   data012.q    data018.s

We’ll use regular expressions to pick out different subsets of files from those above. But first, a word about naming data files. It will often occur that you will include numbers in the names of files and directories. For example, you might have several directories for each day of data collection (march1, march2, etc.) or, within a directory, a numbered file for each run (run1, run2, etc.).

Always zero-pad the numbers so that every name has the same number of digits. In the above examples, it would be better to use (march01, march02, etc.) so you can accommodate data collection on march10. The reason is that Unix and Windows sort files and directories alphabetically and while march02 comes before march10 (like you’d want in a directory listing), march2 would come after march10 because 2 comes after 1.

Now, back to the job at hand. The ? matches any single character.

> ls ?

a   b   c   d   e   f   g

> ls ??

aa   bb   cc   dd   ee   ff   gg

> ls data01?.s

data010.s   data012.s   data014.s   data016.s   data018.s

data011.s   data013.s   data015.s   data017.s   data019.s

The first lists all files whose name is a single character, the second all files whose name consists of two characters. The third lists all files that begin with data01 and end with .s with any arbitrary character in between.

The * matches anything.

> ls *.q

data.q      data004.q   data008.q   data012.q   data016.q   data020.q

data001.q   data005.q   data009.q   data013.q   data017.q

data002.q   data006.q   data010.q   data014.q   data018.q

data003.q   data007.q   data011.q   data015.q   data019.q

> ls data*.??

data.rr      data002.rr   data004.rr   data006.rr   data008.rr

data.ss      data002.ss   data004.ss   data006.ss   data008.ss

data001.rr   data003.rr   data005.rr   data007.rr   data009.rr

data001.ss   data003.ss   data005.ss   data007.ss   data009.ss

> ls data0*.??

data001.rr   data002.ss   data004.rr   data005.ss   data007.rr   data008.ss

data001.ss   data003.rr   data004.ss   data006.rr   data007.ss   data009.rr

data002.rr   data003.ss   data005.rr   data006.ss   data008.rr   data009.ss

The first example lists all files that begin with anything, but end with a .q. The second example lists all files that begin with data followed by anything and ending with . followed by two characters. Compare the second and third examples.

Brackets with characters in-between match any one of the characters. Examples: [abd] matches a or b or d; [m­­­-z] matches any character between m and z.

> ls data00[47].q

data004.q   data007.q

This example lists all files that begin with data00 followed by a 4 or a 7 and ending with .q. Here are two more examples.

> ls data00[4-6].q

data004.q   data005.q   data006.q

> ls data00[236-9].[sq]

data002.s   data003.s   data006.s   data007.s   data008.s   data009.s

 

 

 

More on command execution (aliases, which, PATH)

 

> cd ~/group

It is possible to save yourself some typing by using aliasing. When you define an alias, you pick an alias name and the text that it corresponds to. For example,

> alias lsdws ‘ls -r ~dws’

means that anytime you enter lsdws at the beginning of a command line, the text lsdws  is  replaced by the text ls ‑r  ~dws. (The -r option tells ls to print the directory in reverse order.)

> lsdws

I won’t print the result, but you should see a reverse listing of my home directory now. Note that the alias does not mean that ls -r  ~dws becomes the new command line. Only that lsdws is replaced by that text. So, if you were to type lsdws  ~/group, that would expand to ls -r  ~dws  ~/group and both your ~/group directory and my home directory would be listed. Try it.

If you find that you always want a command to run with certain options, but you don’t want to type them in every time, aliasing is a great way to see that they are used automatically. For example, ls by default does not add the special characters to the directory listing that tell you if entries are executable or directories. To see the true behavior of ls, run it specifying it’s full pathname.

> /bin/ls

INSTALL      example_dir 

Your customization files defined ls to be an alias for ls -FC so the F and C options are automatically added. If you type /bin/ls, the command line doesn’t begin with an alias and so the command line is not modified. Compare the following:

> /bin/ls

> /bin/ls  -FC

> ls

 To see a list of all aliases, use:

> alias

I won’t list the output here, but take a look at it. To add more aliases just for the current session, type them in as you did for lsdws above. To add an alias that is available in every session, add it to the file ~/.aliases using the text editor emacs.

How do you know if a command is an alias or directly executing a program? Use the which command.

> which ls

ls:      aliased to ls -FC

> which cat

/usr/ucb/cat

The second example tells us that when you enter cat, no alias is in use. Instead, the cat program in /usr/ucb is directly executed.

You may not have any need for them, but the main analysis programs I use are called genplot and analysis. Your ~/.aliases file defines  aliases that will run them for you if you simply enter genplot and analysis respectively. (You must use exceed to access Unix for these to work.)

Finally, you may want to have you own programs. Suppose you want to run the count program from anywhere in your directory system. You could, of course, simply type in the full path name every time.

> cd ~

> count 3 yes

count: Command not found.

> ~/group/example_dir/count 3 yes

1

2

3

However, that would be tedious if you needed to use count often. You could use an alias, but another approach is sometimes better. When you enter a command, a number of directories are searched to find the program that will execute the command. The total list of directories searched is stored in the PATH variable.  This tutorial does not cover variables, but you can see its value by entering:

> echo $PATH

/net/campbell/20/dws/lib/grace/home/bin:/net/campbell/20/dws/Tree/bin:

/net/campbell/20/dws/bin:/net/campbell/20/dws/bin/axp:

/usr/local/TeX/bin/alpha-osf4.0:/usr/local/bin:/usr/lang:/usr/ucb:/bin:/usr/bin:/etc:

/usr/etc:/usr/local/bin/X11:/usr/bin/X11:/usr/openwin/bin:/usr/openwin/bin/xview:

/usr/bin/mh:/usr/local/bin/mh:/usr/new/mh:/usr/local/bin/ape:/usr/new:/usr/old:.

Your PATH variable will be different, but one of the entries in your PATH is the bin directory in your home directory. I highlighted the part that specifies my bin directory in blue. This means that the system looks in my bin directory for commands.

Copy the count program into your bin directory.

> cp group/example_dir/count bin

Inform the system that you’ve added a program to one of the directories in the PATH.

> rehash

count is now installed in your bin directory. If you hadn’t run the rehash command, it wouldn’t have been installed until the next time you logged back in.

Test it:

> count 3 yes

1

2

3

> cd group

> count 3 yes

1

2

3

You don’t really want the count command installed so we’ll clean up by removing it.

> rm count

rm: count: No such file or directory

Oops. count isn’t in the current directory.

> rm ~/bin/count

 

 

 

Next steps

 

This tutorial was designed to provide a functional introduction to the Unix operating system. It covers some of the basics that are necessary for working with Unix. I decided on what to include by asking myself what I wished my first students had known when they first joined my group. However, vast areas required to claim proficiency have been left out. If you wish or need to learn more, we have texts that offer a thorough introduction. You will also find links to outside online tutorials on the group links page. Following are some subjects you should be on the watch for. They are not covered in this document, but are vital aspects of Unix.

 

·        Redirection of input. We saw that you can redirect a program’s print output to a file. You can also redirect the input to come from a file instead of the keyboard.

·        Pipes. The output of one program can be designated as the input to another program.

·        Shell/kernel. The operating system is actually divided into a shell and kernel. The kernel is the core that provides the basic functionality such as process management. The shell provides the interface between you and the kernel. When you type in commands, it is the shell that reads and interprets them. There are a number of different shells. You are using one called tcsh.

·        Shell command processing. It is the shell that handles regular expressions on the command line, aliasing and the PATH. There is much more it can do.

·        Shell variables. The shell provides something that is not unlike a programming environment. Shell variables allow customization of the shell and also customization of many of the programs available from Unix.

·        Shell scripts. Scripts are files that contain sequences of command lines to be executed. The INSTALL program you ran at the beginning of this tutorial is a very simple script. Scripts also can contain structured programming constructs like if…then…else…endif so different commands can be executed for different situations automatically.

·        Shell commands. Many commands are not implemented by programs run from Unix but are implemented by the shell directly. rm is a program but cd is a shell command. Try this: which rm and which cd and which which.

 

Naturally, you may also need to know something about the programming environment. In my lab we use c and fortran90, but c++, perl and java are available on Unix. We also have all this on our PCs with the exception of perl. The development environment on the PCs is superior, but it is easy to write code that will run on both our PCs and Unix.

 

If you are going to do work of any substance on Unix, you will need to learn to use the emacs editor. Getting started is very easy – just ask one of us.

 

You can learn about any Unix program by reading the online manual. For example, to get the manual for the cat command:

> man cat

The INSTALL script you ran has designated less as your manual reader. A useful, but long, manual to read is the one for tcsh.

 

Finally, here are some other programs on Unix that you might want to keep in mind:

·        lpr – print a file

·        grep – find a specified text string in a file

·        diff – find differences between two files

·        find – find a file

·        script – keep a transcript of your interaction with the shell

 

The most important bit of advice I can offer is this. To read all the relevant manuals and appropriate introductory texts would take some doing. For advanced work, this may well be necessary, but an introduction sufficient to get you going on all of the above topics can be given easily given what you know now. Ask someone for guidance.