UMUC

Computing Resources

UNIX User's Guide > Basic Commands

This section describes commonly used UNIX commands. UNIX commands may seem cryptic at first, but you'll quickly remember those that are most often used. Most commands accept options (or switches) that begin with a hyphen (-). Options modify the default behavior of the command.

Listing Files: ls

The ls command allows you to list the files and subdirectories in your current working directory. In the example below, Mail and work are subdirectories and july.data and prog1.c are files.

     Nova> ls
     Mail             july.data        prog1.c          work
        

The behavior of the ls command may also be modified by a parameter in the form of a filename, directory name, or wildcard pattern. Only those files or directories that match the parameter will be listed.

     Nova> ls *.*
     july.data        prog1.c
        

The ls command has a number of options. In the example below, the -F option makes it easy to quickly determine file types. Directory names are followed by a slash (/), the names of executable files are followed by an asterisk (*), and link names are followed by the symbol for "at" (@).

     Nova> ls -F
     Mail/      july.data       prog*       prog1.c      work/
        

The -a option adds hidden files to the listing. Hidden files are files with names that begin with a period. These are usually configuration files that are not accessed directly by the user and are not shown in the default ls output. The first of the following examples shows several common hidden files.

The current working directory is indicated by a single period (.), and two periods (..) represent its parent directory. Commands in the .profile, .cshrc, and .tcshrc files are executed when you start the sh, csh, and tcsh shells, respectively. The .login file contains commands to be executed when you log in. The .newsrc file is used by Usenet news readers.

Other applications on the UNIX system may place other files beginning with a period in your home directory. Note in the second example how options can be combined.

     Nova> ls -a
     .                .login          .tcshrc          prog1.c
     ..               .newsrc         Mail             work
     .cshrc           .profile        july.data

     Nova> ls -aF
     ./                .login           .tcshrc           prog1.c
     ../               .newsrc          Mail/             work/
     .cshrc            .profile         july.data
        

The -l option produces a long listing with a lot of information about the files. The fields are (from left to right) permissions, number of links (times the file appears in the file system), owner name, group name, size of the file in bytes, date and time last modified, and filename. The total at the top indicates the number of 512-byte blocks used by the directory (including the hidden files that are not shown unless the -a option is used).

     Nova> ls -l
     total 32
     drwxr-xr-x  2 godzilla  monster   512 Jul 19 12:15 Mail
     -rw-r--r--  1 godzilla  monster  1267 Jul 16 09:25 july.data
     -rw-r--r--  1 godzilla  monster 12144 Jul 18 14:01 prog1.c
     drwxr-xr-x  2 godzilla  monster   512 Jul 19 14:21 work
        

The list of permissions consists of ten characters. The first is a d if the file is a directory or a hyphen (-) if it is a regular file.

Following that are three groups of three characters. The first set of three defines the permissions for the owner of the file or directory ("godzilla" in this example), the second for the group ("monster" in this example), and the third for everyone else on the system.

The first character in each group is an r if read permission is given, or a hyphen (-) if it is not. The next is a w if write permission is given. For files, this gives the user permission to alter and delete the file; for directories, it gives permission to create files and subdirectories within that directory or to delete the directory.

The last character is an x if execute permission is given. For files, this indicates that a file is executable (e.g., a program or a shell script); for directories, it gives permission to search that directory.

Other options that may be used with the ls command are described in its manual page. See Getting Help Using the System for further information on the man command.

top ↑

Copying Files: cp

The cp command is used to copy files. The first parameter is the source file, and the second parameter is the destination. If the destination is the name of a directory, the source file is copied into that directory with its original filename. If the source is a wildcard pattern, the destination must be a directory. All the files that match the pattern will be copied into the destination directory.

     Nova> cp prog1.c prog1.backup
     Nova> ls
     Mail       july.data      prog1.backup     prog1.c      work
        
top ↑

Moving and Renaming Files: mv

The mv command is similar to cp. The first parameter is the source filename, and the second is the destination. If the destination is a new filename, the source file is renamed to the destination name. If the destination is a directory, the source file is moved to that directory. If the source file contains a wildcard pattern, the destination must be a directory. All the files that match the given pattern will then be moved into the destination directory.

     Nova> mv prog1.backup prog1.save
     Nova> ls
     Mail       july.data      prog1.save       prog1.c      work
          

Note that the mv command can rename directories as well as files.

top ↑

Removing Files: rm

To conserve disk space, you should periodically delete the files you no longer need. You can use the rm command to delete any file (or files) you specify. The -i option causes the system to prompt you for confirmation before each file is deleted. If you use wildcards, it might be a good idea to use this option (in case the pattern includes more files than you expected).

     Nova> rm prog1.save
     Nova> ls
     Mail       july.data      prog1.c          work
          
top ↑

Displaying the Current Directory: pwd

The pwd command displays your current working directory.

     Nova> pwd
     /users/monster/godzilla
          
top ↑

Creating Directories: mkdir

You'll probably want to create subdirectories within your account to organize your files. Use the mkdir command to create a subdirectory in the current working directory.

     Nova> ls -F
     Mail/      july.data      prog1.c          work/
     Nova> mkdir homework
     Nova> ls -F
     Mail/      july.data      homework/        prog1.c      work/
          
top ↑

Navigating Through Directories: cd

Use the cd command to change your current working directory. The parameter is the name of the directory to which you want to change. If you use cd without a parameter, you will change to your home directory. Also note that you can specify ".." to change to the parent directory of your current directory.

     Nova> pwd
     /users/monster/godzilla
     Nova> cd work
     Nova> pwd
     /users/monster/godzilla/work
     Nova> ls -aF
     ./          ../         todolist
     Nova> cd ..
     Nova> pwd
     /users/monster/godzilla
          
top ↑

Removing Directories: rmdir

The rmdir command allows you to remove a subdirectory. In order to be removed, the subdirectory must be empty (that is, it must not contain any files or directories).

     Nova> ls -F
     Mail/      july.data      homework/        prog1.c      work/
     Nova> rmdir homework
     Nova> ls -F
     Mail/      july.data      prog1.c      work/
          
top ↑

Displaying the Contents of a File: cat

Use the cat command to display the contents of one or more text files. Simply list the filenames as the arguments. If this produces garbage characters on your screen, the file you specified is most likely a binary file (such as a compiled program) and cannot be displayed.

     Nova> cat july.data
     Credits            $21864.83
     Debits             $38638.25
          
top ↑

Viewing a File One Page at a Time: more and less

The more command displays the contents of a text file one screen at a time. Press the space bar after viewing each screen to prompt display of the next screen. The less command works in the same way but offers a number of additional features, such as the ability to move backward in a file. You can type h while paging through a file to see the commands less offers.

top ↑

Printing Files: lp

The lp command requests that a file be printed on a printer. By default, files are printed on the line printer (lm615) in room 1248 of the Student and Faculty Services Center in College Park. You can use the -d option to specify another printer.

     Nova> lp july.data            (line printer at College Park)
     Nova> lp -d umsgq july.data   (line printer at Shady Grove)
     Nova> lp -d umaaq july.data   (laser printer at Annapolis)
     Nova> lp -d umccq july.data   (laser printer at St. Charles)
          
top ↑

Canceling Print Jobs: cancel

When you submit a print job, you will see a response like the one below:

     Nova> lp -d umsgq prog1.c
     request id is umsgq-121 (1 file(s))
          

This indicates that the request ID for your printout is umsgq-121. To cancel this print job before it prints, you may use the cancel command followed by this ID number.

     Nova> cancel umsgq-121
     request "umsgq-121" cancelled
          

You will need to send the cancel command very soon after having submitted the original print request, because the system will send your printout to the printer within a few minutes of receiving your request.

Computing Resources Home