SECTION 1                                                       PREV | NEXT


About UNIX
    Logging In and Logging Out
    Changing Your Password
    Shells
    Getting Help
    Viewing Files
    Input/Output
    Pipes
Managing Files
    The ls Command
    The rm Command
    The cp Command
    The mv Command
    The wc Command
    The cmp Command
    The diff Command
    The grep Command
    The sort Command
Printing Files
Managing Directories
    Understanding Directories
    Using Wildcards and Symbolic Substitution
    Basic Directory Commands
        The pwd Command
        The cd Command
        The mkdir Command
        The cp Command
        The rmdir Command
    Protecting Files
        The chmod Command
        Checking Existing Permissions


About UNIX

Logging In and Logging Out
Changing Your Password
Shells
Getting Help
Viewing Files
Input/Output
Pipes

The UNIX operating system was developed by Ken Thompson in 1969 at AT&T Research Division at Bell Laboratories. In 1974, several universities acquired the source code of the UNIX operating system. The UNIX operating system includes many of the features found in the Berkeley Software Distribution (BSD) V4.3.

This document presents some basic UNIX commands and concepts. If you need more information about a particular topic, please use the man command or the dxbook (or answerbook) command (for information on the man command, see "Getting Help"; for information on the dxbook (or answerbook) command, see "Accessing UNIX Documentation Using X Windows").

In all the examples given in this document, items in bold represent what you type at the keyboard. Keys that you press are represented in angle brackets. For example, the Enter key is represented by the symbol <return>. If two keys need to be pressed at the same time, they will both be included inside the angle brackets. For example, <CTRL-L> means to press and hold the control key while pressing the L key. The standard input is the terminal and standard output is the screen. This document is not intended to be a complete guide to UNIX. Many of the commands discussed in this document have several options not included in the discussion.


Logging In and Logging Out

Logging In

To use a UNIX system, you must have a valid User ID and password on that system. You can log in to a UNIX system from facilities on campus or from home.

When you log in, you'll need to type your User ID and password given by the Academic Computing. When typing your User ID or password, be sure to type uppercase letters and lowercase letters as such. Case is significant. When typing your password, notice that the letters don't appear on your screen as you type them. This is for your account's security.

**If you make a mistake while typing your User ID or password, you must use the <delete> key to correct it. The <backspace> key will not work.

After entering your password, the system checks both your User ID and your password. If either your User ID or password is invalid or incorrect, then you'll see the following error message ending with another login: prompt:

login: sheila <return>
Password: xxxxxxxx <return>
Login incorrect
login:

After logging in to a UNIX system, you'll see three things: the date and time of your last login, any sign-on messages from the system's administration (optional), and finally the system prompt.

In the following example, the date and time of the last login (no sign-on message appears) and the system prompt are displayed:

login: sheila <return>
Password: xxxxxxxx <return>
Last login: Thu Oct 26 8:55:28 from 229.177.3.100

When you log in to EAGLE or CSC, you'll see a prompt that includes that system's name. In this document, however, we'll use the generic C shell prompt, $. When you see your system's prompt, you are ready to enter UNIX commands.

The UNIX operating system is case sensitive. You must enter UNIX commands in lowercase letters just as you must enter your User ID in lowercase letters.

Logging Out

To end a session on a UNIX system, type logout, exit, or press <ctrl-d> at the command prompt.

$ logout <return>

or

$ exit <return>

or

$ <ctrl-d>


Changing Your Password

Your password on a UNIX system must follow these rules:

Your password must be at least 6 characters long and contain at least one numeric or special character.

To change your password on a UNIX system, type passwd at the command prompt and enter your old password as instructed. After typing your new password, you must correctly retype it for verification. In the following example, the password is changed correctly:

$ passwd <return>
Changing password for sheila
Old password: xxxxxxxx <return>
New password: yyyyyyyy <return>
Retype new password: yyyyyyyy <return>
$

Passwords are not displayed on the screen.

In the following example, the password does not change because the new password was not retyped correctly:

$ passwd <return>
Changing passwd for sheila
Old password: xxxxxxxx <return>
New password: yyyyyyyy <return>
Retype new password: zzzzzzzz <return>
Mismatch - password unchanged.
$


Shells

After logging in to a UNIX system, you'll see the command prompt. At this point, you can enter shell commands. A shell acts as a translator between you and the UNIX operating system. The shell relays your commands to the operating system and returns any responses to you. There are two well-known shells. The first is called the Bourne shell and is named after its developer. The second shell is called the C shell and was developed at the University of California at Berkeley. All of the commands discussed in this guide are C shell commands (the majority of C shell commands are also the same as Bourne shell commands).

Shell Command Syntax

The basic syntax of a shell command is:

command [options] objects

where...

command: Is the name of the shell command.
options: Modify the way a command works. The options are usually single characters and are preceded by a dash (-).
objects: Are the objects that the command is applied against. Objects are italicized in this guide when showing the format of a command.

In the following example, the command cat is used with option n on the object file1:

$ cat -n file1 <return>

You can type several commands on the same line; just separate the commands with a semicolon (;). You can also type a command on more than one line; just place a backslash (\) at the end of the current line and continue with the rest of the command on the next line.


Getting Help

Two commands to get help on a UNIX system are help and man. A third command to get help while using X Windows is dxbook (or answerbook), discussed in section "Using X Windows Systems".

Typing help at the command prompt displays a screen of information about several commands. As indicated, use the man command for further information.

Typing man followed by a command displays the information pages for that command. The man command displays only one page at a time at the bottom of the screen, the UNIX system displays the message "--More--" if the information is continued on subsequent screens.

In the following example, the man command is used with the command cat as its object:

$ man cat <return>
cat(1)

NAME
cat - concatenate and print data

SYNOPSIS
cat [-b] [-e] [-n] [-s] [-t] [-u] [-v] file ...

DESCRIPTION
The cat command reads each file in sequence and displays it on the standard output. Therefore, to display the file on the standard output you type:

cat file

To concatenate two files and place the result on the third you type:

cat file1 file2 > file3
.
.
.q
$


Viewing Files

There are several UNIX commands for viewing files without starting an editing session. Some of these commands are listed below. To get a complete description of these commands and the different options used with these commands, use the man command, the dxbook command, or the answerbook command.

The cat Command

The cat command, followed by the name of a file, displays the contents of that file. If you specify more than one file name, then the cat command displays the contents of each file in sequence.

The cat command displays the entire file without stopping. In the following example, the cat command displays the contents of file.test:

$ cat file.test <return>
This is just a test file
This file contains only two lines.
$

The more Command

The more command also displays the contents of a file. The difference between the more command and the cat command is that the more command pauses between screens, indicates the percentage of the file which has been displayed, and waits for your response before continuing. While you are using more, you can use the following commands:

In the following example, the more command displays the contents of file1. The <spacebar> and q commands are also used:

$ more file1 <return>
This is line number 1.
This is line number 2.
This is line number 3.
.
.
.
This is line number 23.
--More--(31%)<spacebar>

This is line number 24.
This is line number 25.
This is line number 26.
.
.
.
This is line number 46.
--More--(81%) q
$

The less Command

The less command also displays the contents of a file on the screen. While you are using less, you can use the following commands:

* Press <return> to scroll forward one line.
* Type b to scroll backward one screen.
* Type f, or press <spacebar>, to scroll forward one screen.
* Type q to quit displaying the file and return to the system prompt.

In the following example, the less command displays the contents of file1. The commands b, f, and q are also used:

$ less file1 <return>
This is line number 1.
This is line number 2.
This is line number 3.
.
.
.
This is line number 23. f

This is line number 24.
This is line number 25.
This is line number 26.
.
.
.
This is line number 46. b

This is line number 1.
This is line number 2.
This is line number 3.
.
.
.
This is line number 23. q
$

The head Command

The head command displays the first 10 lines (by default) of a file. You can specify how many lines the head command shows by following the head command with a hyphen and the number of lines you want displayed. In the following example, the head command displays the first 6 lines in file1:

$ head -6 file1 <return>
This is line number 1.
This is line number 2.
This is line number 3.
This is line number 4.
This is line number 5.
This is line number 6.
$

The tail Command

The tail command displays the last 10 lines (by default) of a file. You can specify how many lines the tail command shows by following the tail command with a hyphen and the number of lines you want displayed. In the following example, the tail command displays the last 3 lines of file1:

$ tail -3 file1 <return>
This is line number 98.
This is line number 99.
This is line number 100.
$


Input/Output

Most of the commands on a UNIX system require input and produce output. For example, the cat command uses any file name following it as input and displays the contents of the file on the screen as output.

On a UNIX system, you can redirect both the source of the input and the destination of the output. This concept is called input/output redirection. Some of the redirection operators and their definitions are:

Operator Meaning
> Redirects standard output.
>> Redirects and appends standard output.
>& Redirects standard output and standard error.
>>& Redirects and appends standard output and standard error.
< Redirects standard input.
<<xxx Reads input up to a line identical with xxx.
| Redirects standard output to another command (see section "Pipes").

In the following example, the output of the cat command is redirected to file3:

$ cat file1 <return>
This is a line in file 1.
$ cat file2 <return>
This is a line in file 2.
$ cat file1 file2 > file3 <return>
$ cat file3 <return>
This is a line in file 1.
This is a line in file 2.
$

In the following example, the output of the cat command is appended to file3:

$ cat file3 <return>
This is line 1 in file 3.
This is line 2 in file 3.
$ cat file1 <return>
This is line 1 in file 1.
$ cat file1 >> file3 <return>
$ cat file3 <return>
This is line 1 in file 3.
This is line 2 in file 3.
This is line 1 in file 1.
$

In the following example, the input is read from the terminal until the pattern EOF is encountered. All information received is stored in the file file1.

$ cat > file1 << EOF <return>
? This is the first line in file1. <return>
? EOF <return>
$ cat file1 <return>
This is the first line in file1.
$

When you want to redirect the input of a program, use the standard input redirection operator (<). The input of the program is then read from a file rather than from the keyboard.


Pipes

A pipe makes the output of one command become the input of another command without creating an intermediate file. A vertical bar (|) separates the commands forming a pipeline.

In the following example, the output of the cat command becomes the input to the sort command with the help of a pipe:

$ cat test <return>
wc
more
head
cat
$ cat test | sort <return>
cat
head
more
wc
$

Managing Files

The ls Command
The rm Command
The cp Command
The mv Command
The wc Command
The cmp Command
The diff Command
The grep Command
The sort Command

There are many UNIX commands to help you manage your files. Several of these commands are discussed in this chapter. To get a complete description of each command and its options, please use the man command or the dxbook (or answerbook) command (for more information on the man command, see "Getting Help"; for information on the dxbook command, see section "Using X Windows Systems").

The ls Command

The ls (list) command displays the files in a specified directory. The ls command with no object displays the files in the current working directory. The format of the ls command is:

ls [options] directory

In the following example, the ls command displays the contents of user jsmith's home directory and the /home/dept directory (for information on directories, see "Managing Directories"):

$ ls <return>
file1 file2 file3 letters
$ ls /home/dept <return>
jsmith bcarson
$

The rm Command

The rm (remove) command is used to delete a file. The format for the rm command is:

rm [options] file

Use the rm command with caution. Once a file is deleted, you cannot recover it.

In the following example, the rm command is used to delete file1:

$ cat file1 <return>
This is the first line in file1.
$ rm file1 <return>
$ cat file1 <return>
file1: No such file or directory
$

Add the option -i (for interactive) to the rm command when you want to be asked to confirm deleting any files.

The cp Command

The cp (copy) command copies files. The format of the cp command is:

cp [options] source.file destination.file

Use the cp command with caution. If the destination file already exists, then the copy command replaces that file with the new file.

In the following example, the cp command copies file1 to file2:

$ cat file1 <return>
abc def ghi
$ cp file1 file2 <return>
$ cat file2 <return>
abc def ghi
$

Add the option -i (for interactive) to the cp command when you want to be asked to confirm replacing any file that already exists.

The mv Command

The mv (move) command renames files. The format of the mv command is:

mv [options] old.filename new.filename

Use the mv command with caution. If the destination file already exists, then the move command replaces that file with the new file.

In the following example, the mv command renames file1 as file2:

$ cat file1 <return>
abc 123 45678
$ mv file1 file2 <return>
$ cat file2 <return>
abc 123 45678
$ cat file1 <return>
file1: No such file or directory
$

Add the option -i (for interactive) to the mv command when you want to be asked to confirm replacing any file that already exists.

The wc Command

The wc (word count) command displays the number of lines, words, and characters in a file. In the following example, the wc command is used on file1:

$ cat file1 <return>
This is line number 1.
$ wc file1 <return>
1 5 23 file1
$

Thus file1 contains 1 line, 5 words, and 23 characters.

The cmp Command

The cmp (compare) command reports the differences between two files. The cmp command reports the first difference found between the two files. In the following example, the cmp command compares letter1 and letter2:

$ cat letter1 <return>
abc
cde
fgh
ijk
$ cat letter2 <return>
abc
fgh
ijj
$ cmp letter1 letter2 <return>
letter1 letter2 differ: char 5, line 2
$

The diff Command

The diff (differences) command reports all the differences found between two files. Moreover, the diff command also displays the commands necessary to make the two files identical to each other using the ex editor (the ex editor is a standard UNIX editor). The format of the diff command is:

diff [options] file1 file2

The option -e used with the diff command produces a script of a, c, and d commands for the ex editor.

In the following example, the diff command compares letter1 and letter2; the -e option is also used:

$ cat letter1 <return>
abc
cde
fgh
ijk
$ cat letter2 <return>
abc
fgh
ijj
$ diff letter1 letter2 <return>
2d1
< cde
4c3
< ijk
---
> ijj
% diff -e letter1 letter2 <return>
4c
ijj
.
2d
$

The grep Command

The grep (get regular expression) command searches for a particular string of text in a file. The format of the grep command is:

grep string file

If grep finds the string, then the string is displayed on the screen and you'll return to the command prompt. If grep does not find the string, you'll simply return to the command prompt. In the following example, the grep command searches for the strings efg and abc def in the file letter1:

$ cat letter1 <return>
abc
cde
efg
ijk
$ grep efg letter1 <return>
efg
$ grep "abc def" letter1 <return>
$

The sort Command

The sort command sorts lines of the specified file and displays the sorted output on the screen. The default sort key is the entire line. The sort command does not alter the specified input file.

In the following example, the sort command sorts the contents of file1:

$ cat file1 <return>
hello
help
elephant
$ sort file1 <return>
elephant
hello
help
$


Printing Files

The lpr command sends a file to a default printer.

$ lp_filename <return>
$

Files form Eagle can only be printed in the HSB Lab. You can  use the UNIX command lp_filename to print  your file.  Go to the mailboxes next to the consultant's desk for your document.  Documents are sorted by the first letter of your login name. 


Managing Directories

Using Wildcards and Symbolic Substitution

Understanding Directories

On a UNIX system, a directory contains a list of files and subdirectories. After logging in, you are placed in a directory called your home directory. Your home directory is the first level directory where you can save your files.

At the very top of the directory structure is the root directory, denoted by a single slash (/). Subdirectories are denoted by a slash followed by the subdirectory's name. Figure 1 represents a sample directory structure. In Figure 1, the home directory is a subdirectory of the root directory, departmental directories are subdirectories of the home directory, and individual user's directories are subdirectories of their departmental directory.

Understanding UNIX Directories

Figure 1. A Sample Directory Tree Structure

According to Figure 1, three files, file1, file2, and file3, as well as the subdirectory /letters are in user jsmith's home directory. (User jsmith's home directory is /home/dept/jsmith which is a subdirectory under the dept directory which is a subdirectory under the home directory).

Two hidden files exist in each subdirectory, one designated by a single period (.) and the other designated by two periods (..). The single period file (.) is a pointer to the current working directory, whereas the double period file (..) is a pointer to its parent directory. A tilde (~) always represents your home directory.

Using Wildcards and Symbolic Substitution

You can use special characters known as wildcards to help you find file names that match a certain pattern. The two most common wildcards are the question mark (?) and the asterisk (*). The rules for using ? and * are:

? Matches any one character.
* Matches any group of zero or more characters.

You can also use metasequences to find file names. A metasequence is simply a list of characters, any one of which can be matched. To use a metasequence, simply place the characters to be matched between the brackets [ ].

The following table lists examples of wildcards and metasequences:

*.* Filenames containing a period (.).
*.com Filenames ending with .com.
?.out Filenames with one character preceding the period and ending with .out.
a[bc] Filenames ab and ac.
pq[a-z] Filenames containing 3 characters in the range pqa through pqz.

 


Basic Directory Commands

The pwd Command
The cd Command
The mkdir Command
The cp Command
The rmdir Command

All of the examples given in this section refer to the directory structure described in Figure 1 of section "Understanding Directories". As shown in Figure 1, user jsmith's home directory is /home/dept/jsmith.

The pwd Command

The pwd (print working directory) command displays the working directory on the screen. In the following example, user jsmith's home directory is displayed:

$ pwd <return>
/home/dept/jsmith
$

The cd Command

The cd (change directory) command changes the current working directory. In the following example, user jsmith uses the cd command to change directories:

$ pwd <return>
/home/dept/jsmith
$ cd letters <return>
$ pwd <return>
/home/dept/jsmith/letters
$

In the following example, user jsmith returns to the directory above the current working directory:

$ pwd <return>
/home/dept/jsmith/letters
$ cd .. <return>
$ pwd <return>
/home/dept/jsmith
$

In the following example, user jsmith changes to the root directory:

$ cd / <return>
$ pwd <return>
/
$

You can change to your home directory from any other directory by using the cd command with no object or by using the cd command followed by a tilde (~). In the following example, user jsmith uses the cd command with no object:

$ pwd <return>
/
$ cd <return>
$ pwd <return>
/home/dept/jsmith

The mkdir Command

The mkdir (make directory) command creates a new directory. The format of the mkdir command is:

mkdir directory

In the following example, user jsmith uses the mkdir command to create a subdirectory, newdir:

$ pwd <return>
/home/dept/jsmith
$ mkdir newdir <return>
$ ls <return>
file1 file2 file3 letters newdir
$

The cp Command

The cp command copies files. In the following example, user jsmith uses the cp and mv commands to copy file1 and move file3 from the current working directory to the newdir directory:

$ pwd <return>
/home/dept/jsmith
$ ls <return>
file1 file2 file3 letters newdir
$ cp file1 newdir <return>
$ mv file3 newdir <return>
$ ls <return>
file1 file2 letters newdir
$ ls newdir <return>
file1 file3
$

The rmdir Command

The rmdir (remove directory) command is used to delete existing subdirectories. The rmdir command only removes subdirectories if they are empty. The format of the rmdir command is:

rmdir directory

In the following example, user jsmith uses the rmdir command to remove the subdirectory newdir:

$ pwd <return>
/home/dept/jsmith
$ rmdir newdir <return>
rmdir: newdir: Directory not empty
$ cd newdir <return>
$ ls <return>
file1 file3
$ rm file1 <return>
$ rm file3 <return>
$ ls <return>
$ cd <return>
$ pwd <return>
/home/dept/jsmith
$ rmdir newdir <return>
$


 

Protecting Files

The chmod Command
Checking Existing Permissions

On a UNIX system, the user's world is divided into three categories: User, Group, and Other.

The User (u) refers to the user who created and controls access to the file.

The Group (g) refers to the users who have the same group number as the owner (for example, all the students in the same class as the owner).

The Other (o) refers to the rest of the UNIX users.

You may grant users in any of these categories permission to access your files. There are three kinds of permission:

Read permission (r) Permission to read a file.

Write permission (w) Permission to modify or delete a file.

Execute permission (x) Permission to run an executable file.

The chmod Command

The chmod (change mode) command allows you to change the protection assigned to a file or a directory. The chmod command has the following format:

chmod [mode] file

The mode field consists of three parts:

The category of users to be affected: user (u), group (g), or other (o).

The action to be performed: add (+); remove (-); or absolute (=), which replaces all existing permissions for that group.

The permission to be affected: read (r), write (w), or execute (x). 

Checking Existing Permissions

You can check what permissions are currently in effect by using the -l (long) option with the ls command. In the following example, user jsmith uses the ls -l command to check existing permissions:

$ pwd <return>
/home/dept/jsmith
$ ls -l <return>
total 8

-rw------- 1 jsmith 1865 Nov 19 12:00 file1
-rw-rw---- 1 jsmith 256 Nov 01 09:05 file2
-rw----rw- 1 jsmith 865 Dec 25 05:00 file3
drw-rw-rw- 2 jsmith 512 May12 11:23 letters

$

The first character in the file description describes the type of file. A "d" in the first column means that the file is a directory (the name of a subdirectory) and a "-" in the first column means the file is a plain file containing ASCII or binary information.

The next nine columns list the file protection for user, group, and other, in that order.

The information after the list of file protection describes the number of links to the file, the owner of the file, the size of the file in bytes (a byte = 1 char), the last date and time the file was modified, and the name of the file.

A link is a logical connection between files and directories, or between directories. A file has at least one link to the directory file in which it resides. A directory file has at least two links, one to itself and one to its parent directory file.

In the following example, user jsmith uses the chmod command with the subdirectory letters and the file file3 (note that the absolute action performed on letters replaces any existing permissions):

$ pwd <return>
/home/dept/jsmith
$ ls -l <return>
total 8

-rw------- 1 jsmith 1865 Nov 19 12:00 file1
-rw-rw---- 1 jsmith 256 Nov 01 09:05 file2
-rw----rw- 1 jsmith 865 Dec 25 05:00 file3
drw-rw-r-- 2 jsmith 512 May12 11:23 letters

$ chmod o=w letters <return>
$ chmod g+r file3 <return>
$ ls -l <return>
total 8

-rw------- 1 jsmith 1865 Nov 19 12:00 file1
-rw-rw---- 1 jsmith 256 Nov 01 09:05 file2
-rw----rw- 1 jsmith 865 Dec 25 05:00 file3
drw-rw--w- 2 jsmith 512 May12 11:23 letters

$