Summary of Useful UNIX Commands for Programmers

(Adapted from a document at Presbyterian College)

Introduction

In order to write and run programs on a UNIX system, you need to know some basic commands for creating and managing files.

Filenames

In our versions of UNIX accessible to students, filenames can be up to 255 characters long. In principle, they can contain just about any combination of letters, digits and special characters such as punctuation marks. In practice, people usually use just letters, digits, periods, dashes, and underscores. Many special characters are difficult to use in filenames, because they require special treatment in commands. For the same reason, you should not use blank spaces in filenames, as you might on a Macintosh. Note in particular that UNIX considers upper case and lower case letters to be different in filenames, so that the names schmoe and Schmoe are different.

Unlike MS-DOS, UNIX does not require filenames to have a three-letter "extension", as in MYPROG.EXE. Nevertheless, many kinds of files customarily do have certain standard suffixes. Some of the ones that you are likely to encounter are:

.txt
generic text file (also known as "ASCII text" or "ASCII file")
.f
Fortran source program (text file)
.c
C source program (text file)
.cc
C++ source program (text file)
.o
executable program (binary file)
.Z
file compressed using the UNIX compress command (binary file)
.gz
file compressed using the UNIX gzip command (binary file)
.tar
UNIX "tape archive" file which bundles a group of files or directories together into a single file (binary file)
.ps
PostScript file for printing on a PostScript printer (text file)

Directories and Pathnames

Files are stored in collections called directories, which are similar to "folders" on a Macintosh or in Microsoft Windows. Directories can also contain other directories (sub-directories), similarly to having folders within folders. In fact, all files and directories on a UNIX system are ultimately "contained" in a single top-level directory called the root directory.

Every file and directory on a UNIX system can be specified completely by listing the chain of directories and sub-directories that contains it, separated by slashes (/). This is called an absolute pathname. Examples:

/
the root directory
/u1
the file or subdirectory named u1, in the root directory
/u1/schmoe
the file or subdirectory named schmoe, in subdirectory u1 of the root directory
/u1/schmoe/program.f
the file program.f in the subdirectory schmoe of the subdirectory u1 of the root directory
Fortunately you don't have to type out the entire absolute pathname every time you need to use a file. At any given moment, you are "attached" to some particular directory, which is called your current directory. You can use any file in your current directory by specifying only the part of the absolute pathname that comes after the path to your current directory, without a slash at the front. This is called a relative pathname. Usually it is just the file name itself. For example, if your current directory is /u1/schmoe, you can use

program.f
instead of /u1/schmoe/program.f
cs301/hw2.cc
instead of /u1/schmoe/cs301/hw2.cc
Note carefully the difference between an absolute pathname and a relative pathname:

Listing the contents of your current directory

If you type a ls command at the $ prompt, you will see a list of the files and subdirectories in your current directory. Think of ls as meaning "list." You can tell the command to give you various kinds of file listings, by specifying options, which are single letters preceded by a space and a dash.
$ ls
file names only, excluding "dot files"
$ ls -a
file names only, all files
$ ls -l
summary info for all files, excluding "dot files"
$ ls -al
summary info for all files.
$ ls -a -l
same as above.
Dot files are files and directories whose names begin with a period. Many programs store configuration information in dot files in your directory. They do not appear in "normal" directory listings so that you can't mess them up accidentally.

Example of summary info

The normal ls listing shows just the names of the files, packed several per line. The ls -a listing shows one line of information for each file. Each line looks something like this:
-rw-------   1 rossa   users     7469 Aug 25 07:53 myprog.c
^^^^^^^^^^     ^^^^^   ^^^^^     ^^^^ ^^^^^^^^^^^^ ^^^^^^^^
     1           2       3         4       5          6
The different items have the following meanings:
  1. The first character is the file type:
    -
    ordinary file
    d
    directory
    The rest of item 1 indicates the access mode of the file. Our default is that only the "user owner" may do anything with the file.
  2. The user who owns the file (the user owner)
  3. The group who owns the file (the group owner)
  4. Size in bytes.
  5. Date of most recent modification.
  6. Name of the file or directory.
If you want to get this information about just a single file, you can use ls -l with a filename. For example, the following command will give you the information shown above, for that file only:

$ ls -l myprog.c

To do this for a group of files with similar names, you can use a "wildcard" in the file name. An asterisk (*) matches any sequence of characters; a question mark (?) matches any single character. For example:

*.txt
matches coldfusion.txt, junk.txt, etc.
cold*
matches coldfusion.txt, cold-medicine, etc.
data.?x
matches data.ax and data.bx, but not data.tax .

Creating and Using Subdirectories

To move around from one directory to another, use the cd command ("change directory").
$ cd cs301
Change your current directory to the subdirectory cs301.
$ cd ..
Change your current directory to the "parent" directory of your current directory.
$ cd /local/classes/cs301
Jump to the specified directory, using an absolute pathname.
$ cd classes/cs301
Jump to the specified sub-subdirectory of the current directory, using a relative pathname.
$ cd ../cs301
Jump to a "sibling" directory (i.e. up to the parent directory, then down into cs301.
To see what your current directory is at any time, use the pwd command ("print working directory").

$ pwd
Show the pathname of your current directory.
To create a new subdirectory of your current directory, use the mkdir command ("make directory").
$ mkdir cs301
Create a subdirectory named cs301 in your current directory.

Creating Source Programs

A source program is a collection of Fortran or C++ statements, stored in an ordinary text file. You can create it using the editor vi. For information on vi, see the vi notes. You can also create the file on a PC and ftp it over to the UNIX system.

Viewing Text Files

One way is to open it with vi. You may need to be careful that you don't accidentally change the file. A better way is to use view, which has the vi positioning commands but cannot write files.

A safer way is to use one of the UNIX file-viewing commands:

$ more filename
Scrolls through the file a screenful at a time. Hit the space bar to see the next screen. Hit h to see other options.
$ page filename
Similar to more, but clears the screen and starts each "page" at the top of the screen.

Copying a File

It's usually a good idea to make a "backup copy" of a file before you make extensive changes to it. That way, if you mess things up bacly, you can go back and start over again. To make a copy of a file, use the cp command. For example:

$ cp myprog.cc myprog.cc.backup

This creates a copy of the file myprog.cc, and puts it in myprog.cc.backup.

Renaming a File

Sometimes you need to just change the name of a file, without creating a new, separate file. For example, you may have mistyped the name when you created it, and now you want to correct it. Or maybe you created a backup file as described above, and now you want to go back to t that file, giving it its original name, after deleting the copy that you bungled. To change the name of a file, use the mv ("move") command:

$ mv myprog.cc.backup myprog.cc

This renames myprog.cc.backup back to myprog.cc. Warning! If you already have a file with the name that you're trying to change to (myprog.cc in this case), Unix will delete the "old" file and not warn you about it! So it's usually a good idea to use ls to check first, before you rename a file, to see if you already have one with the same name.

Deleting Files

Executable program files are big, typically at least 200-250 kilobytes. In comparison, your source programs will usually be no more than a few tens of kilobytes in size. Don't keep executable programs around any longer than necessary. If you want to run the program later, you can always re-compile it.

$ rm filename
Deletes a file in your current directory
$ rm file1 file2 file3
Deletes several files at once.
$ rm *.f
Deletes all files whose names end in .f, in your current directory, using a wildcard.
Be very very very careful if you use * in an rm command, because * all by itself means "all non-dot files in your current directory." If you type rm * .f (which differs only by a blank space from the last example above!), you're saying "remove all the non-dot files, then remove the file .f."

A safer way to use rm with wildcards is to include the -i option:

$ rm -i *.f

This will ask (inquire) individually about each file which matches the wildcard, giving you a chance to rescue some of them from oblivion.

Deleting directories

$ rmdir dirname
Deletes a subdirectory of the current directory; works only if the subdirectory is empty.
$ rm -r dirname
Deletes a subdirectory and all its contents (files and sub-subdirectories). Use with caution!