Unix Questions

General Help, File Structure, vi, Other Commands, Shells

General Help

Where can I buy a Unix guidebook? What do you recommend?

Any bookstore, including places like B. Dalton. I learn these things through different associations than you would so can't give a personal recommendation. I've know people who liked "UNIX for Idiots".

How much help can you get from the "man" command?

It varies wildly. Usually there is far more than you want to know, and your problem is sorting out the few nuggets of gold. As my class example of the use of the "-k" option with man showed, sometimes it is difficult to locate information based only on a general idea of what you want to do.

By the way, is the Unix system used widely in Meteorology?

The workhorse computer for large weather programs is a Cray, which operates on a version of Unix called Unicos. Unix Workstations are also heavily used.

Why do you sometimes type "set term=vt100"? Should I do this every time I use Unix?

Use of the "set term" command is necessary when Unix is confused about how to display things (particularly in programs like "vi") on your screen. Most of the Telnet programs on campus leave Unix in this state of confusion when you login. When I'm using one of these, by typing "set term=vt100", I tell Unix exactly how to deal with display to my screen. Telnet is running something called a "vt100" terminal emulation. At the Hammond Workstations, your terminal windows are called "xterm", but you normally don't need to type "set term=xterm", because the Xterm software is better at talking to Unix. If you follow the instructions below to force a "vt100" setting at login time, you may want to type "set term=xterm" when sitting at one of the Hammond workstations (or use my "hamm" alias). I say may, because the Xterm software is smart enough to deal with "vt100" communications. You just don't get all of the power of a full Xterm.

File Structure

Why do we need all of these files? What does the computer do with them?

Files are simply a place to store information on the computer's disks. They are given names so that we can easily find them. We will primarily use files in 5 ways. Although you don't think about it, most commands that you enter at your terminal simply cause the CPU to execute machine instructions stored in a file with the command's name (try the command "ls /bin" to see some of these files). Each Fortran program that we write to perform some calculation will be stored in a file. When we compile the file the resulting instructions that can be used by the CPU are stored in a separate file. We tell the machine to execute the commands in that file to do the calculation. For some projects we will create files that contain data to be used in a program. We will also create programs that put their results in specific files for later use.

I'm confused about directories.

Directories are simply a way of maintaining order and ownership of files on disks. You don't want to see every file on a disk, and don't necessarily want everyone to look at all of your files. If my analogy to file folders didn't help, try thinking of the disk as a building. You enter the front door and might find a few signs to read (files that can be viewed with utilities such as "more"), and in the elevator you see numbered buttons on the wall ("ls" provides you with eyes on the computer) and punch a labeled button to go to a selected floor (use "cd" to move to a subdirectory or file folder in a disk file system). When you step out of the elevator you see a few more signs posted and see numbered doors to rooms (a lower level of subdirectories). Some of the rooms may be locked. If a room belongs to you (check "ls -alF" to see if it is yours), you can lock out or grant access to others with "chmod". You enter a room ("cd" again), find more notices on the wall to read, and a pen ("vi" editor) on a desk that you can use to leave your own notes in the room. You can protect these notes from prying eyes with "chmod". You might call the maintenance crew to clear all the useless notices off of the wall ("rm") or ask them to remodel and give you some connecting rooms (mkdir). If there are connecting rooms that you no longer want, the maintenance crew can get rid of them for you. An empty room can be removed with the command "rmdir". If the room has any contents you can yell "rm -r (room name)", but may regret the wanton destruction.

I am still really confused about the structure of the directories. Is it like DOS? It seems like you can't have one directory or file with the same name even if they are in different subdirectories.

Yes it is very much like DOS. Within a given directory, you can't have two files, or a file and a subdirectory with the same name. However, files can exist with the same name in different subdirectories. If this keeps bothering you, set up an appointment to do a directory and file tour in my office.

How can a file and a directory be the same?

Perhaps I gave you too much information too early. If I create a subdirectory called "subdir1", in fact I create a file called "subdir1" containing information that will let the system find the files I've put in "subdir1". These details aren't useful to you at this point (and probably never will be).

Why aren't the subdirectories used by Unix to identify the current directory and the next one higher marked with a * ?

Although all directories are marked as executable in the list of access privileges, they are simply marked with a / when the "-F" option is used by ls to indicate their function. You don't really execute an entry for a directory.

How do I protect files?

File protection is handled with the "chmod" command or by using "umask" in your .cshrc file. If you don't want anyone looking at the file "cannon.f" issue the command:

chmod og= cannon.f

This removes all access privileges from others (o) and your group (g). This is not your class group, but a broader definition including all students. You can protect whole directories with something like "chmod og= hw3". If for some reason you want to give others write access to a file use "chmod o=w cannon.f".

How do I delete files and directories?

To delete files use the "rm" command. "rm cannon.f" deletes cannon.f. "rm *.f" deletes all files with names ending with ".f". If a subdirectory "hw3" is empty you can delete it with "rmdir hw3". You can also get rid of "hw3" with the command "rm -r hw3", but this takes out "hw3", and all of its contents including any subdirectories existing in hw3. If you like the computer to check with you for confirmation on each delete, use the "-i" option.

Are there any restrictions in naming of directories and aliases?

The full name of a directory tracing back to the root must be unique. However, it is possible to have both "/tmp_mnt/avenger/jhm123/sub1" and "/tmp_mnt/avenger/avy321/sub1". Some characters that are used for special purposes by csh such as &,*,[,], and \ may cause you grief. With aliases it is possible to redefine commands that are normally used by csh or the underlying Unix operating system, so exercise a little caution. This overwrite feature can be useful to force your favorite options when you execute a utility. Take a look at the file ".alias" in your home directory for an example of this redefinition. You should set your favorite aliases in the ".alias" file, so that they are automatically defined when you login. Please note that ".alias" is not something Unix automatically uses, it is executed by this system's default ".cshrc" file when csh or tcsh are started.

Can Unix read a txt file created by an MS-DOS software appliciaton?

Yes, but it needs to be moved by mail or FTP in ascii text mode to take care of details in file record structure.


How do I delete blank lines in a program when using vi?

Position the cursor on the blank line while in command mode (hit the Esc key if you're not sure) and type "dd". If you have 2 blank lines in a row, put the cursor on the first one and type "2dd".

I still have a question about how to back-up your work when you are using vi via a telnet.

See some of the FTP questions for backup to floppies. Best Unix backup is to copy your file to one with another name before using vi:

cp hw4.f hw4.f.back

While in vi you should type ":w" from time to time to save your most recent changes to the disk.

Is it more useful to use Autoindent when you have a program that has a lot of comments

I tend not to. However, one trick I use is to type the basic program in autoindent mode, then insert the comments with vi not in autoindent mode.

When on a remote connection what do you do while in vi command mode to correct a mistake?

Backspace only works in insert mode, so you have to use the "x" command to delete characters under the cursor. If you are having trouble with Backspace while in insert mode, get out of vi and type the command "stty erase" followed by a space, then hit the Backspace key, and then hit the Return (Enter) key.

How do I deal with strange behavior of the screen and cursor in vi?

If you just see a few lines that stay at the top of the screen while everything else moves, its because you have more than the vt100 standard of 24 lines visible on the screen. No big deal there. If lines are abruptly vanishing when they shouldn't, or the cursor is jumping to unexpected positions, use the "set term=vt100" command.

vi is a major pain, what can I do?

Some students are successfully using MicroSoft Word on PC's and Macs. This option of requires changing your files on a PC and moving them to Unix machines with ftp. Two other options are to learn the "emacs" editor or the "xedit" editor.

Other Commands

In a command like "cd /" do we need a space after the slash?

No! When entering full file path specifications such as "/home/jhm/test/test.f" the presence of blanks will confuse the computer.

How do I copy files? How do I get to files shared by the group?

The Unix copy command is "cp". Before making massive changes to a Fortran program (say the program "test.f"), you might want to issue the command "cp test.f test.fo" to create a backup copy. If user jhm123 is keeping the latest copy of your group's program in the chain homework/hw3/cannon.f, you can copy it to your current directory with :

cp ~jhm123/homework/hw3/cannon.f cannon.f


cp ~jhm123/homework/hw3/cannon.f .

For you DOS folks, the period at the end of the last form is required by Unix, telling it to drop the copy into the current directory with the name cannon.f. If you are used to DOS I recommend that you include a line in your .alias file containing "alias copy cp".

When using diff do both files have to be in the same directory?

No, but it is easier. In referring to a file you must give enough information so that diff can find it. Say you are in a directory with a file "hw6.f" and there is a subdirectory under your current directory called "test", containing another file called "hw6a.f". You can compare them from the current directory by typing:

diff hw6.f test/hw6a.f

If you happen to be in the subdirectory "test" you could do exactly the same thing by typing:

diff ../hw6.f hw6a.f

What else can we do with dbx?

You can list lines in your original Fortran program (list n1,n2 where n1 and n2 are line numbers). You can tell it to stop execution at a given line number (stop at n1). This is referred to as setting a breakpoint. You can continue executing from the last stop until the next breakpoint (cont), or just execute the next line of Fortran (step or next). There is much more. I'll give an example after the exam and you can check "man dbx"

How do I mail files from Unix?

Say you want to mail file "grp11.hw3" to me at cs201@mowgli.psu.edu. Simply issue the command:

mail cs201@mowgli.psu.edu < grp11.hw3

On my end I receive the file by typing "mail". mail gives a numbered list of files in my mailbox. If your file is item 14 in my mailbox, I type the command "w 14 grp11.cannon". I then type "q" to end my mail session, and can look at the new file "grp11.cannon" in my current directory.

What does "Unknown tilde escape mean"?

It means that mail is processing an input line that begins with a "~", but is followed by a letter that it does not recognize as a valid "tilde" command.

How do you erase files in your mailbox?

Use the "d" command. "d" deletes the current message. "d 10" deletes message number 10. "d 10-20" deletes messages 10 through 20.

How would I go about printing a program that I have written?

Easiest way is to move it to a PC with FTP, open it with your favorite Word Processor, and print like any paper. I recommend setting the font to 10 point Courier at the top of the document. If you are in 316 Hammod and want to print a program named "test.f", try the Unix print command:

lpr -Pasci test.f

I am not comfortable with commands preceded by a - sign.

These are not true commands, just options that modify the behavior of basic Unix commands. I will continue to give examples. You should try the options to appreciate their behavior.

How do you add the LINPACK subroutines to your Fortran Program?

I'll assume that you have 3 files: main.f containing the main program; sgefa.f containing the matrix factorization subroutine, and some routines from BLAS; and sgesl.f containing the linear system solution subroutine plus some more stuff. The quick way to do the job is to use the Unix "cat" command. At the Unix prompt type:

cat main.f sgefa.f sgesl.f > hw11.f

This combines the three files into a new file named hw11.f. The other option is to open the file containing your main program with "vi". Place the cursor in the last line of the file and type the command ":r sgefa.f". Move the cursor to the end of the file again and type ":r sgesl.f".

I did not understand the "cat" command.

The cat command is used for 2 purposes. One is to simply to quickly display the contents of a file on the terminal screen. Typing "cat test.f" will show you everything in test.f, but won't stop for you if the file has more lines than available on the screen (screen normally shows the last 24 lines). The primary use of cat is to combine the contents of several files into a single file. "cat a.f b.f c.f d.f > list.f" combines everything in the 4 files "a.f", "b.f", "c.f" and "d.f" into the file called "list.f", assuming "list.f" does not already exist. If the file "list.f" already exists, you can replace it with the command "cat a.f b.f c.f d.f >! list.f". If what you really want to do is add other files to the end of "list.f", you issue the command "cat a.f b.f c.f d.f >> list.f". Use of >, >!, and >> in any Unix command has the effect of redirecting output, that would normally go to the screen, to a disk file. You can get more information by typing "man cat". I was moving too rapidly when going over "cat:". When my explanations become too fast or terse, feel free to get my attention by shouting "Question!" at high volume.

How do I get a file like "add.f" up on my screen to edit and change?

If you want to change it, you must copy it to your own directory space first. Change to the subdirectory where you what it with the "cd" command then use "cp" to copy it in and "vi" to edit it.

cd homework cp ~jhm/201/add.f . vi add.f How do I save my homework directly into my homework subdirectory?

After you login, type "cd homework", before starting "vi" to create a program file. When you are done the file will be left in the homework directory. The commands:

cd homework vi hw4.f will result in a file named hw4.f in your homework subdirectory


What is a shell?

A shell is just another computer program. It accepts commands that you type into your keyboard, and passes them on to the Unix operating system (perhaps with some translation).

Can I use tcsh?

Yes. Try "man tcsh" for information on tcsh. Once you are logged on, type "tcsh" to start this shell. Commands are the same ones you have learned, but using the up and down arrow keys will let you scroll through commands used since starting tcsh. For a given command use the left and right arrow keys to position yourself in the line for changes. Normally what you type is inserted in the line. Holding the "Ctrl" key and hitting "d" (sometimes referred to as "^d" in instructions) deletes the character under the cursor. "Ctrl" with "k" deletes the rest of the line beyond the cursor point. When you are using tcsh, the first time you type "exit", you will be returned to your original csh session. Another "exit" is required to logout.

What is the first line of the runit file ( "#!/bin/csh")?

It tells the operating system that I am using a script for the C Shell. A number of Shells are available under Unix, and you have to do something specific here, or Unix assumes that you are writing a script for the Bourne Shell. This doesn't make a difference until you get into some of the fancy loop and conditional structures.

I learned that !v gets you back into the file in vi you were working on. Are there any other shortcuts you can tell us.

Actually "!v" executes the last command that started with the letter "v". You can use the same idea to compile the same file again with "!f", assuming that you haven't executed any other commands starting with "f" since the last use of "f77". If you had used the command "find" after the last compile, you could go to a longer abbreviation of "!f7" to redo the compile.

You can create your own shortcuts with the "alias" command, particularly by including "alias" commands in your ".cshrc" file. If you get good enough with Unix, you can also create shortcuts by writing scripts (files containing a series of Unix commands).

I would like to learn further Unix shortcuts.

I will be introducing various Unix tricks during class, but my goal is to just make you functional in Unix. There are a large number of good Unix books in the Library and bookstore, if you want to become a Unix guru.

How can you put alias commands into a file that is run at each login?

You can add alias command lines to your ".cshrc". Also look at your ".cshrc" file for a line like "source .alias" or "source .alaises". If it exists (or you add it), you can put lines in the ".alias" or ".aliases" file. The command "source .alias" simply tells the Unix shell to execute the commands contained in the text file ".alias".

Is there a way to repeat the previous command in Unix

In csh (your default shell) "!!" will repeat the previous command. "!f7" will repeat the last command line that began with the letters "f7". The better way out is to type "tcsh" right after you login (you will have to type "exit" twice to get all of the way out of the machine). The arrow keys will let you scroll through previous commands.

To access a file, you just type the file name at the prompt, right?

You do that to execute the file if it contains machine instructions or a Unix shell script (like .cshrc). To open a text file and enter or change a Fortran program you must type "vi" followed by a space followed by the file name.

Why do I always type "a.out" to execute a file after every Compilation. Why for instance can't I type "mult.out" after I compile my "mult.f" file?

The reason is that "a.out" is the name of the file created by f77, containing the machine instructions corresponding to your Fortran program. It's just a default convention used by f77 (and the C compiler "cc"). If you would like another name use the. -o option. If you would like to type "mult.out" then compile with the line:

f77 mult.f -o mult.out

Is there a way I can program or alias so that I won't always have to type "set term=vt100" and the stty command?

Yes, if you are only going to be using 316 Hammond rarely, and generally will do your work from a Telnet session, you can embed commands in your ".cshrc" file, so they are executed whenever you login. As a preparation you need to figure out what the character is coming from the key that you want to use for erasures. Run through the standard drill of typing "stty erase" followed by a space followed by hitting the key you want for erasure (usually the Backspace key). Look at the screen to see what pops up when you hit that erasure key. I usually see a caret followed by a question mark (^?). If you see something different replace it below at the point where I include "^?". The next thing you should do is go to your home directory (typing "cd" will do it), and issue the following copy command to protect yourself.

cp .cshrc old.cshrc

You are about to change an important file, so you need the backup. Next open ".cshrc" with vi and position yourself at the end of the last line. Go into insert mode and enter the following lines, including the single and double quotes as I've given them (but adjusting for the question mark if needed).

set term=vt100

stty erase '^?'

alias hamm "set term=xterm;stty erase '^H'"

The last line creates an alias to make your life easier in 316 Hammond. When working directly at the workstation type "hamm" before doing anything else in a given Xterm window. You could mimic this alias for setting needed on a Telnet session, if you normally use Hammond, but occasionally use Telnet sessions.

Up to other Questions and Answers / Home

Maintained by John Mahaffy : jhm@psu.edu