College of Arts and Sciences

Department of Mathematics

Working with Files

A file is a data collection such as a FORTRAN program, or information needed to print a graph. File names have the format: filename[.ext]where filename is a user-supplied name, ext is the extension usually consisting of characters denoting the file type. The file named odesolve.f is FORTRAN source code named odesolve that resides in the current directory.

Files are associated into groups called directories. Each user begins with a single directory, called the home directory, which contains all subsequent files and subdirectories. When you first log on to a UNIX system, the working directory is your home directory. For instance, /usr1/richards is richards' home directory. Subdirectories are created in a tree-like fashion. Set them off from the parent directory by slashes in the directory specification. Thus, /usr1/richards/papers is a subdirectory of the home directory /usr1/richards. Subdirectories help organize related files; for instance, a subdirectory fortran may contain all FORTRAN source code files. To make a subdirectory called papers off the current directory, type:

eta> mkdir papers

If there is no directory specification before the name papers, this indicates that the subdirectory should branch from the working or current directory. To set /usr1/richards/papers as the current directory, type commands:

eta> cd /usr1/richards/papers

or type

eta> cd ~/papers

where the tilde (~) by itself expands into the name of the home directory /usr1/richards.

eta> cd ~jones

changes the current directory into user jones's home directory. Typing cd without any arguments is the equivalent of clicking Dorothy's heels together: it returns you to your home directory regardless of where you are.

To find out what the working directory or current directory you are under, just type:

eta> pwd

which means print working directory.

Manipulating Files

To list the files and subdirectories contained in the current directory, type the command:

eta> ls

which stands for list. To get a list of files with their creation dates and their sizes, use:

eta> ls -l

The -l is called a flag, and stands for long. In any UNIX system there are a number of files that are usually hidden from your view. To see all files, including hidden files, type ls -a. You may combine flags, so that to see all of the details about all of your files, type ls -al.

To move a file from the current directory to richards' paper directory, type

eta> mv memo.tex /usr1/richards/paper/letter.tex

You may use mv to rename a file within a current directory, or to move it from one to another. Any valid directory specification may be used in front of the file name.

To make a copy of an existing file with the same name, type:

eta> cp odesolver.f fortran

This command could have various results, depending on your directory structure. If you do not have a directory called fortran, this command will create a copy of the file odesolver.f and call it fortran. If you already have a directory called fortran, then a new file called odesolver.f is created in the directory fortran.

The command

eta> cat plusfile >> oldfile

will concatenate the file plusfile to the end of oldfile without affecting plusfile. The command cat plusfile will concatenate plusfile to the standard output. This is an obscure way of saying that cat plusfile will list the contents of the file on the screen.

Deleting Files

Files that have outlived their usefulness should be deleted as a matter of good account housekeeping. To remove a file from a directory, type:

eta> rm directory/filename

The rm command will assume the file is on the current directory when no directory is indicated.

Since rm supports the use of filelists (see below),

eta> rm ode*.f

deletes all files of type .f whose names begin with ode. Use care with wildcards. To manage its effect, use the -i option:

eta> rm -i ode*.f, ode*.o

and UNIX will request confirmation before each file deletion.

It is also possible to delete entire directories.

eta> rmdir papers

will delete the directory papers from the current directory if the directory papers is empty. If the directory papers is not empty, but you want to delete it together with all of its contents, then use the rm -r command option:

eta> rm -r papers

Since no quotas on disk space are imposed, please use rm and rmdir often to free disk space for others who may need extra room. This give-and-take management gives great freedom in terms of disk usage and CPU time; please help support our system by employing good file management habits.

Viewing Files

To view a text file onscreen, use:

eta> cat filename

If there is too much text to fit on the screen, the excess scrolls off the top. To momentarily stop the output, type <Ctrl-S>. Continue the output by pressing <Ctrl-Q>. Most terminals also offer a <Hold Screen> key. Press the key once to halt output, press again to resume scrolling. For large files, a better solution might be to use

eta> more filename

This will type the file a single screenful at a time, awaiting a space bar between each screen. The editor can also be used to view files (see the chapter Using the UNIX Editor or on-line help).

Do not attempt to view binary files, such as files of type .o or a.out, using the above commands. These files contain symbols that can cause terminals to hang up.

File Lists

Many commands accept lists of files in their file specifications. For example, to delete two files, instead of issuing the delete command twice, once with each file, issue it once with the two files separated by spaces, as in:

eta> rm prog1.f prog2.f

The C-shell has very powerful features that may be used to create lists of files. The simplest are wildcard characters. Wildcard characters turn a single file specification into a file list by substituting for alphanumeric characters in a filename. The wildcard character * replaces any alphanumeric string, so ode*.f is equivalent to the list of all files (in the current directory) whose filenames begin with the

characters ode, and which have the extension .f. The command

eta> ls ode*.f

might provide output something like:

odesolve.f odeinit.f ode_to_urn.f ode.f

The character ? acts as a wildcard taking the place of a single character. The list designated by ?solve.f would contain rsolve.f, but not odesolve.f, or solve.f.

Another way of creating lists of files is to use logical expressions. For example, if the files paper1.tex, paper2.tex paper3.tex, and paper4.tex reside in the directory papers, and if one wants to list the contents of only the first three, then the command

eta> cat paper[1-3].tex

lists the contents of the first three files. There are many powerful features in the expansion o f logical expressions, which are beyond the scope of this document. Check the man page for csh.

Department of Mathematics, PO Box 643113, Neill 103, Washington State University, Pullman WA 99164-3113 Phone: 509-335-3926 Fax: 509-335-1188 Contact Us