CIS

COMPUTER INSTRUCTIONS

FALL, 1994

UNIX COMMANDS

==== ========

This document describes some basic commands available in the UNIX operating system. The commands covered should be enough to get you started using UNIX.

The contents of this document are by no means exhaustive. Nor are the descriptions of any command in this document. To get more information on any command, refer to the appropriate UNIX reference manual or use the "man" command (described below) to reproduce the pages from the reference manual.

Every command in UNIX corresponds to a file. The file is either an executable program or a shell script (much like a com file in the Vax/Vms). One important note is that file names in UNIX are case sensitive. This means that you must type commands EXACTLY as they appear, paying attention to upper and lower case letters. All parameters to a command are passed on the command line following the command name. Each parameter is separated by a space. The command name is separated from the parameters also by a space. So, for example, to use the "cp" command to copy "file1" to "file2", the following would be entered on the command line:

cp file1 file2 <CR>

The commands covered by this document include the following (in order):

Page Command Description

2     cat    Display contents of file(s)

2     cp     Copy file(s)

3     mv     Rename or move file(s)

3     rm     Delete (remove) file(s)

4     ls    List file(s) in directory(ies)

4     chmod  Change protection/access settings on file(s)

5     cd     Change current working directory

6     pwd    Display current working directory setting

6     mkdir  Create directory

6     rmdir  Delete (remove) directory

7     lp      Print file(s) on line printer

8     lpstat Display status of printer(s)

8     cancel Cancel print request

8     who    Display list of users logged into system

9     mail/mailx Send or receive mail to/from users on system

10    write  Hold interactive conversation with another user

10    vi     Edit a file

11    cc   Compile a C program

11    sdb  Debug a C (or Fortran 77) program

12    man   Reproduce UNIX manual pages

13    passwd Change password

13    exit   / <CTRL>-D Logout

13    date Display the date and time
 

UNIX COMMANDS
==== ========

cat : Display the contents of a text file (or files).

FORM: cat [files]

Parameters: [files] Copies the contents of the specified file(s) to standard

output (generally, the user's terminal). If no files are specified, text is read from standard input (generally, the user's terminal keyboard).

EXAMPLES:

cat sum2.c

Which prints the contents of the file "sum2.c" to the terminal screen.

cat prog1.c prog2.

Prints the contents of the file "prog1.c" to the terminal, followed by the contents of "prog2.c"

cp : Copy a file (or files)

FORM: cp file_1 file_2 or cp file(s) directory

First form:

cp file_1 file_2

Copies contents of file_1 into file_2

Second form:

cp file(s) directory

Copies specified file(s) into specified directory

Parameters:

file_1 For the first form of the command. This is the source file. It's
contents are copied to file_2.

file_2 For the first form of the command. This is destination file. It  becomes an exact copy of file_1.

file(s) For the second form of the command. Copy this file (or these files) into the specified directory.

directory For the second form of the command. The file (or files) are copied into this named directory.

EXAMPLES:

cp sum_2.c sum3.c

Copies the contents of the file sum2.c" to the file "sum3.c"

cp prog1.c prog2.c homework_dir

Copies the files "prog1.c" and "prog2.c" into the directory "homework_dir"

mv : Move or rename a file (or files)

FORM: mv file_1 file_2 or mv file(s) directory

First form: mv file_1 file_2

Renames file_1 to file_2 (the file known by the name file_1 will now be known by the name file_2)

Second form: mv file(s) directory

Moves specified file(s) into specified directory

Parameters: file_1 For the first form of the command. This is the original name. The file known by this name is going to have its name changed to file_2.

file_2 For the first form of the command. This is new file name. The file known by the name file_1 will now be known by this name.

file(s) For the second form of the command. Move this file (or these files) into the specified directory.

directory For the second form of the command. The file (or files) are moved into this named directory.

EXAMPLE:

mv assignment1.c assignment2.c

Changes the name of the file "assignment1.c" to "assignment2.c"

mv prog1.c prog2.c homework_dir

Moves the files "prog1.c" and "prog2.c" into the directory "homework_dir"

rm : Delete a file (or files)

FORM: rm file(s)

Parameters: file(s) Delete (remove) the specified file(s).

NOTE: Once a file has been deleted, it can not be recovered!

EXAMPLES:

rm garbage.c

Delete the file "garbage.c"

rm trash1.c trash2.c

Delete the files "trash1.c" and "trash2.c"

ls : List files in directory

FORM: ls [options] [files]

Options: -a List all files (files with names beginning with a dot are usually not listed).

-l List all directory information on files.

Parameters: [files] List specified files. If no files specified, list all files in current working directory or in directory specified in place of files.

EXAMPLES:

ls

List all the files in the current working directory.

ls -l homework_dir

List all information on all files in the directory "homework_dir"  (which is a sub-directory of the current working directory).

ls -a /usr/src

List all files (including "hidden" files - those whose names begin with a ".") in the absolute directory "/usr/src".

ls -l assignment1.c

List all information on the file "assignment1.c" found in the current working directory

chmod : Change protection mode of file(s)

FORM: chmod {class}{+/-}{access} file(s)

parameters: {class} Specifies the class of users to whom the protection change applies. One or more classes may be specified. There are three (3) classes of users:

u = user (owner of file)

g = group (all users in the same group as owner)

o = others (all other users on the system)

There is also a fourth option available here:

a = includes all three of the user classes listed above

{+/-} Specifies that a type of access will either be granted or revoked.

+ means the access will be granted

- means the access will be revoked

{access} Specifies the type of access to be granted or revoked. One or more types of access may be specified.

r = read permission (user may read/copy file)

w = write permission (user may write/alter file)

x = execute permission (user may execute the file)

file(s) Specifies the name(s) of the file(s) whose access permissions are being altered

EXAMPLES:

chmod g+rx announcement

Allow all users in the same group (as the owner) to read and to execute the file "announcement"

chmod u-w vital.info

Disallow the owner to alter the contents of the file "vital.info"

chmod a+r public bulletin

Allow all users (the owner, all members of owner's group, and all others on the system) to read the files "public" and "bulletin"

cd : Change current working directory

FORM: cd [directory]

Parameter: [directory]

Make named directory the new working directory. (All file references will assume the working directory to be the file's location unless a full directory path is supplied.)

If no directory name is supplied as a parameter, the new working directory will be set as the HOME directory; this is the working directory set at login time. Each user has a HOME directory.

EXAMPLES:

cd homework_dir

Set the sub-directory "homework_dir" as the current working directory

cd /usr/src

Set the absolute directory "/usr/src" as the current working directory

cd

Set the user's "home" directory as the current working directory; this is the current working directory as set when the user first logs in

cd ..

Changes the directory back one level (to the previous directory).

Note: You must have a space before the ..

pwd : Display current working directory

FORM: pwd

Prints the current working directory. This is the directory in which all files referenced in commands are assumed to exist if no directory names are supplied with those file names. All references to directories (or directory trees) are also assumed to start from within this directory unless a full "path name" is provided in the file reference. (A full "path name" is a name that uniquely details the location of a file within the entire file system. It starts with the root directory. So any path name beginning with the character "/" is a full path name.)

mkdir : Create a sub-directory

FORM: mkdir directory-name(s)

parameters: directory-name(s)

Create a directory (or directories) to be known by the given name(s).

EXAMPLES:

mkdir homework_dir

Create a sub-directory (within the current) working directory) to be named "homework_dir"

rmdir : Deletes an empty sub-directory

FORM: rmdir directory-name(s)

Parameters: directory-name(s)

Remove the specified directory (or directories) if the directory(ies) is (are) empty.

EXAMPLE:

rmdir old_dir

Deletes the directory "old_dir" ONLY IF the directory is empty (no files in there)

lp : Print a file (or files)

FORM: lp [options] [files]

Parameters: [files] Places the specified file(s) into the print queue to be printed (for hard-copy). If no file is specified, text is read from standard input (terminal by default).

Options:

-d{printer}

Sends the output to the specified print queue (as specified by {printer}).

-t{text} Includes the string ({text}) on the banner page of the print out.

-m Send mail to indicate that the file has been printed when the printer has completed the print request.

EXAMPLES:

lp assignment1.c

Send the file "assignment1.c" to the default printer queue

lp -dprinter2 assignment2.c

Send the file "assignment2.c" to the printer queue named "printer2"

lp -thomework -m assignment3.c

Send the file "assignment3.c" to the default printer queue, including the string "homework" on the banner page in large block letters and, when the printing is done, have mail sent back to indicate that the printer is completed

NOTE: Due to problems with file protections, for "lp" to print a file, all directories leading to that file must be accessible to everyone. One way around this is to avoid passing the file on the command line. Using pipes, the file can be passed to the "lp" command as input in the following form:

cat file(s) | lp [options]

or

cat sum2.c | lp -tsum2.c -m

By not passing a file name to the "lp" command, "lp" reads the input for a file to print. This input comes from the "cat" command. To better understand pipes, refer to the document "UNIX Concepts" in the section on "pipes".

The above approach is a general approach which will print any file on the system.

lpstat : Display status of print queue(s)

FORM: lpstat [options]

Options: -p{printer}

Displays status of specified print queue {printer})

-p Displays status of all print queues

-o Displays status of all output requests

-t Displays full information about all the print queues

If no options are supplied, status of user's own print requests are given.

EXAMPLES:

lpstat

List all print requests made by user that have not yet completed

lpstat -t

Provide a full report on the status of all the print queues and the entire print spooling system

lpstat -ppr1

Display status of the print queue (and its associated printer) named "pr1"

cancel : Cancel a print request (remove request from print queue)

FORM: cancel request-id

Parameter: request-id

Cancels the queued print job identified by the "request-id". This "id" was supplied when the output was queued for printing (by the "lp" command).

EXAMPLE:

cancel pr1-115

Cancels the print request identified as "pr1-115" (request number 115 on the print queue known as "pr1")

who : List the users currently logged into the system

FORM: who

Lists the users logged into the system, supplying their user names, terminal names, and the date/time when they logged in.

whoami Displays who you are

mail : Send or receive mail to/from other users on the system

mailx : (same; more enhanced version)

FORM: mail or mailx or mail user(s) or mailx user(s)

First and second forms: mail (mailx)

Enters mail (or enhanced mail) utility IF there is mail to be read

Third and fourth forms: mail user(s) ( mailx user(s) ) Sends mail (via mail of enhanced mail utility) to specified user(s)

Once in mail, there are several commands that can be executed to manipulate the mail messages. To find out more about them, simply enter a "?" for a list of the commands.

When entering a letter to be sent, the letter is terminated and sent to the specified user(s) by entering on a line by itself a CONTROL-D.

Parameters: user(s) Send the letter to the specified users

EXAMPLES:

mail snoopy

Send mail to the user "snoopy"

mail

Read any mail sent to user (if there is any)

Note: At question mark prompt in mail, you can enter anyone of these mail commands followed by a <CR>.

n : Read next message

# : Display message #

+ : Next (w/o delete)

dq : Delete current message and exit

a : Position at and read newly recieved mail

d : Delete current message d[#]

p : Print message

q : Quit

r[args] : Reply to (and delete) current message

s[file] : Save and delete current message

u[#] : Undelete message #

y[file] : Save and delete current message

write : Initiate an interactive conversation with another user logged into the system

FORM: write user [terminal-name]

parameters: user User with whom to attempt a conversation

[terminal-name] optionally specified terminal name if user is logged on at more than one terminal

The conversation ends when each user types a CONTROL-D.

NOTE: Information about who is logged in and at which terminal(s) can be obtained by invoking the "who" command.

EXAMPLE:

write snoopy

Attempt an interactive conversation with the user "snoopy" currently logged in

vi : Edit a file

FORM: vi file-name

parameter: file-name

Name of file to edit via the screen oriented editor "vi".

NOTE: Before invoking "vi", the user's terminal type must be known to the system. This information is kept in the shell variable TERM. (To find out the current value of this variable, enter the command "echo $TERM".) The name of the terminal type must be placed in this variable and must be a name that UNIX is familiar with. To define the terminal type, enter the command:

"TERM={name}" where {name} is the name of the terminal type (e.g. "vt200", "vt100", "wyse50"). Rather than supplying the name for your terminal, you may have to specify a terminal type with which your terminal is compatible. After specifying the value for TERM, enter the command "export TERM".

(For convenience, these two commands for defining the terminal could be placed in the file ".profile" in the HOME directory, which executes every time the user logs in.)

For details on using the "vi" editor,

(see the document "How to use 'vi' under UNIX").

EXAMPLE:

TERM=vt200 export TERM

vi assignement1.c

Identifies the terminal as a VT200 series terminal and initiates an editing session for the file "assignment1.c"

cc : Compile a C program

FORM: cc [options] file

parameter:

file Compile this file containing C source code.

This file name must end with ".c" to compile. The result is, by default, a file named "a.out" which can be executed simply by entering its name as a command.

Options:

-o{file} Place the executable code into the file specified by {file} rather than in "a.out". Once again, to execute the  program, simply enter the name of the executable file as a command.

-g Provide in the object code additional information that will be useful for the Symbolic Debugger ("sdb").

-c Compile only (no linking).

EXAMPLES:

cc assignment1.c

Compiles the program in "assignment1.c" leaving the executable version in "a.out"

cc -g -o assignment2 assignment2.c

Compiles the program in "assignment2.c" leaving the executable version in the file "assignment2" with additional information included for the debugger

sdb : Invoke the Symbolic Debugger to debug a C (or FORTRAN 77) program

FORM: sdb object-file core-file directory-list

Parameters:

object-file

The name of the executable file which is to be debugged. The default is "a.out".

core-file

The name of a file containing a snap-shot dump of memory (if created) as it was when (and if) the program bombed. The default is "core".

directory-list

A colon-separated list of directories containing the source files that were compiled to generate the executable (object) file.

Note: Depending on the type of UNIX system you are on, the Compilers and debuggers may take on a different (flavors) names. Please see your instructor or lab assistant for the appropriate Compiler you are to be using!

man : Reconstruct pages from the UNIX reference manuals

FORM: man topic [section]

Parameters:

topic

Name of topic as listed in the reference manuals (usually a command or library function name).

[section]

An optional section name (digit) specifying which chapter the topic is found.

1 = User commands

2 = Low level system calls

3 = High level function calls

4 = File formats

5 = Miscellaneous

6 = Demonstration programs

7 = Special files (device files)

8 = System maintenance procedures

EXAMPLES:

man cc

Send to the terminal a copy of the manual pages describing the "cc" (C compile) command

man write > write.doc

Send to the file "write.doc" a copy of the pages describing the "write" command only (as opposed to including the "write" system call, which is in section 2, as well)

man ls | lp

Send a copy of the manual pages describing the "ls" command to the default printer

man man

Displays the use and descriptions of man.

passwd : Change password

FORM: passwd

After invoking the command, the user is prompted for the old password (for security), and the new password (twice, for verification).

exit : Log off of the UNIX system

FORM: exit

Enter this command to log out of UNIX. Another way to log out is to type CONTROL-D at the terminal.

date : Prints the current date and time on standard output

FORM: date