View this PageEdit this Page (locked)Uploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide

unix intro

Introduction to Unix

Introduction to Unix


Contents

Please note that in order to view this page properly, you must use a Web browser which supports tables. For instance, Netscape V1.1 (or better) or Mosaic V2.6 for UNIX (or better).


Unix is a multi-user, multi-tasking operating system that has become a standard on workstations, mainframes and supercomputers. This page provides only an overview of Unix. If you are new to Unix, you should buy a Unix text to help you learn Unix. A list of Unix and Unix related books is available.

If you work on the Goddard campus, you should consider taking introductory Unix courses at the Goddard Learning Center.

Note that there are several flavors of Unix that run on different computers. These include AT&T System V, BSD (Berkeley Software Distribution), and SunOS. This page is geared towards the Unix which runs on the Cray, UNICOS, which is a System V version of Unix with BSD extensions, as well as extensions unique to Cray.

The user interface to the Unix operating system is through a shell. The shell interprets the commands you type and call s the programs needed to execute your commands. Four shells are available on the Crays: the Posix shell, the Korn shell, the C shell, and the Tenex C shell. The Tenex C shell is not supported, which means that the NCCS supplies the software "as is" in /usr/local/ubin (the directory for unsupported binaries). The NCCS will attempt to resolve problems reported on unsupported software, but no guarantees are made, and problems with unsupported software usually have low priority.

Some Unix Basics

Unix is Case Sensitive

Unix is case sensitive; upper and lower case letters have different meanings. Most Unix commands are all lowercase. For example, the command to list files is ls. LS is not equivalent to l s; LS is not a Unix command.

Correcting Your Typing

The stty command defines terminal control settings, in particul ar the definition of the backspace character, referred to as the erase character. To set the backspace character to the key you want to use for this function, type:

stty erase [press the key you want to use for backsp acing].

If you make an error while typing in a command, you can use the backspace key to delete the last characters you typed.

Filename Expansion

Many Unix commands operate on a list of files. You can use special characters (sometimes called metacharacters or wild cards) to automatically generate a list of filenames.

Use the asterisk, *, to match any number, including zero, of characters. For example, making a reference to chap* could ref er to files of the name chap, chap1, c hapter2, etc. It would not refer to (or match) files of the name first.chap or cha1.

Use ? to match a single character in a filename. For example,< STRONG>chap? matches chap1, but does not match chap, chapter2 or first.chap.

Square brackets can be used to make more specific matches. Square brackets define a "character class" (i.e. a set of characters), used to replace a single character within a filename. For example, chap[135] matches chap1, chap3 or chap5, but does not match chap or chap2. Hyphens can be used to represent a range of characters. [0-9] means any digit, [a-z] means any lower case letter, and [a-zA-Z] means any upper or lower case letter.

Special Characters

The following characters have special meaning to Unix shells:

& ; | * ? ' " ` [ ] ( ) $ < > ^ # / \ % !

In addition, the Return or Enter key, the space tab and the tab keys have special meaning to shells. It is best to not use any of these characters in filenames until you understand how the shell interprets them!

Quoting

Quoting removes the special meanings of characters.

You can quote a single character by preceding it with a backslash, \.

For example:

echo \|

results in | being typed at the terminal. If you hadn't quoted the |, but typed:

echo |

the shell would have thought you were trying to send the output from the echo command to a pipe, represented by | (there is more inf ormation on using pipes below), and your sequence of commands would have been incomplete.

Use single quotes, ' ... ', to remove the special meaning of al l characters except ' itself. You cannot include another single quote within a pair of single quotes.

Use double quotes, " ... ", to remove the special meaning of al l characters except $, \, `, and ".

Unix Commands

Unix commands are of the form:

command [argument1] [argument2] ...

The square brackets indicate that the arguments are optional. Arguments are separated by one or more spaces or tabs. The last argument often specifies one or more filenames. An option is an argument that changes the behavior of a command. Most Unix commands use a dash (-) to indicate that an argument is an option. For example:

ls -rlt chap*

Here, ls is the "list files" command, -rlt a re the options to the command (don't worry about what they mean for now), and chap* s pecifies the files to list (all files that start with the letters chap).

Options can usually be strung together, or listed separately, as you prefer. The following is equivalent to the ls command shown a bove:

ls -r -l -t chap*

If you group options together, do NOT put spaces between them. For example, the following is NOT equivalent to the two ls commands given above:

ls -r l t chap*

(This last example would attempt to list the files l and t in addition to the files starting with chap. The only option used in the a bove example is -r ).

File Redirection

You can use file redirection to specify where commands get input from or where they should send output to. Commands get their input from standard input, which by default is usually the terminal. Commands send their output to standard output, which by default is also usually the terminal. Suppose that you wanted to save the output from the ls command in a file (rather than have it displayed at the terminal, which is the default). You can use the > symbol to redirect the o utput from ls :

ls -lt > filename

Then you can look at the file (using an editor like vi, or via the more or cat commands).

Now suppose that you wanted to append one file to another. You can use >> to accomplish this. In the following example, we use the cat command to display the contents of file1. By default, this would be displayed at the terminal. Instead, we use >> to take the output from the cat command ( which is just the contents of file1) and append it to file2 :

cat file1 >> file2

You can also redirect standard input, using the < symbol. S uppose you have a program that needs to read in some data to standard input (in Fortran, standard input is unit 5). You could type:

program_name < input_data

Pipes

Pipes are used to send the output from one command to the input to the next. The symbol | (vertical bar) is used to represent pipes.

For example:

ls -alt * | more

takes the output from the ls command and "pipes it into" the < STRONG>more command, which pauses after each screenful of information.

The "here document"

Another way to redirect I/O (input/output) is to use the "here document". Here documents are often used in scripts (a script is a sequence of commands contained in a file), when you want to say "use the following text (arguments, commands, or whatever) from HERE to HERE as input to my command". The general form of the here document is:

   command <<HERE
      command_input_line1
      command_input_line2
      ....
   HERE 

where HERE is a unique string (i.e. a string that won't be confused with any of the commands in the script) that marks the beginning and end of the input you want to provide to the command. For example, if you use FTP (File Transfer Protocol) within a script, use the here document to tell FTP which commands it should execute. For example:


   ftp -i my_workstation <<END
      get my_program
      get my_data
      quit
   END 

Unix Processes

A process is a command that is being executed by the Unix operating system. Each process is associated with a unique process id. To see the processes you own, type ps . The output from the ps command looks like this:


   PID TTY   TIME COMMAND
 17605 p024  0:01 csh
 10887 p024  0:00 ps

where PID is the process id, TTY identifies the terminal associated with the process, and TIME shows the number of CPU seconds the process has accumulated.

The first command in the above display, csh, shows the shell, i n this case the C shell.

When you type a command, the shell usually forks, or spawns, a child process to execute the command. Some commands are built into the shell. The shell does not have to fork a separate process to run a built-in command.

You can type <Ctrl>c (hold the Control key, then press t he c key) to stop the execution of your current process.

You can use the kill -9 command to kill a process you own. T ype:

kill -9 pid

where pid is the process id of the process you want to kill.

Getting Online Help With the man Command

The Unix man ("man" is derived from "manual") command provides online information about Unix commands, including syntax, function, and options. The NCCS has added site-specific information to the Unix man facility where appropriate. To use man , type:

man command_name

To find out what man pages (and software) are available in a general topic area, type:

man -k topic_name

or:

apropos topic_name

For more information on how to use the man command, type:

man man

Sometimes it is convenient to save the output from a man comman d to a file, so that you can browse through it with your favorite editor. You can remove the underscores and other "funny characters" by piping the output from man to the col -b command and saving the resulting file, as follows:

man command_name | col -b > filename

Note that there are usually no man pages for commands built into the shell. Information for built-in commands is contained within the man page for the shell. For example, type:

man csh

to see the C shell's built-in commands.

The Unix file system

Unix uses a hierarchical system, called a tree structure (it's actually an inverted tree), to store files. The top level of the Unix file system is the root directory called /. Below / are various directories, sub-directories, sub-sub directories, and so forth. A t the end of the "directory branches" are "ordinary files". A part of the Cray's Unix file tree is shown below.


                             /
                             |
     +------+------+------+--+---+-------+-------+--------+
     |      |      |      |      |       |       |        |
    u1     u2     u3     u4     bin     lib     silo     tmp  ...
            |                    |
       +----+---+            +---+---+
       |        |            |       |
     abxyz    acwxy  ...     cp     ls ...
                |
          +-----+-----+
          |           |
       project1   project2  ...
          |
      +---+-----+
      |         |
    test.f  test.data  ...

/u1, /u2, /u3 and /u4 are the user file systems on the Cray. /u2/abxyz and /u2/acwxy are the home directories for users abxyz and acwx y respectively. When you log into the Cray, you are automatically placed in your home directory. The above diagram shows user acwxy having several project directories. In each project directory are files associated with that project.

/bin contains many of the Unix user commands, such as c p (copy file) and ls (list files), shown in the diagra m. The complete pathnames (also called absolute pathnames) of these two commands are /bin/cp and /bin/ls respectively. Absolute pathnames start with th e root directory, /, and include the names of all the directori es you have to traverse downwards from / to get to the final fi le. Note that in a pathname, a / is inserted between the diffe rent directory names. An example would be:

/u2/acwxy/project1/test.f.

Normally, you don't have to type the complete pathnames for these commands, because Unix uses your PATH environment variable (an environment variable is a variable that defines characteristics of your shell environment) to search for the commands you type. The Cray automatically includes /bin, /usr/bin, /usr/ucb, and /usr/local/bin in your path. Introduction to Unix Shell s provides more information on how to include other pathnames in your path.

The term current directory refers to your current location in the directory tree. Unix calls your current directory . (dot). T he parent directory is the directory one level up from the current directory. Unix calls the parent directory .. (two dots). Yo u can use . and .. as command arguments. For example, you can use the change directory (cd) command to go up one directory level by typing:

cd ..

You can refer to files located below your current directory by their relative pathnames (that is, the name relative to the current directory). For example, if user acwxy is in her home directory, /u3/acwxy, she can refer to the file test.f as project1/test.f. If she is in her project1 directory, she can simply call this file test.f.

Note that in Unix, the word file can refer either to a directory or to an ordinary file (directories are just files to the Unix operating system!), although people often loosely use the word file to mean ordinary file.

Unix Filenames

The maximum number of characters you can use in a pathname on the Cray is 1023; the maximum number of characters you can use in a filename (the part of the pathname that appears after the last /) is 256 . Note that some Unix systems may restrict you to 14 characters for the filename (and the POSIX standard is 14 characters).

Although almost any ASCII character can be used in a filename, it's best to restrict yourself to upper and lower case letters (keep in mind that Unix is case sensitive), digits, . and _. This is because many special characters have special meanings to Unix, and the ir use in filenames might be confusing. In particular, you should avoid using < STRONG>* and ? in filenames.

Some Unix files begin with a dot (.); they are often called hid den or dot files, and are usually startup files for various shells and utilities . This means that when the shell or utility starts running, it reads the information in the dot file for startup (or configuration) instructions. For example, the Korn and POSIX shells read your .profile for s tartup instructions. The C and Tenex C shells read your .login and .cshrc file. These files are not displayed when you use t he ls ("list files") command unless you give ls the -a option. To see your dot files, type:

ls -a

By convention, suffixes are used to indicate specific file types to certain Unix utilities. Some common suffixes are:

file.f Fortran source file
file.f90 Fortran 90 source file
file.c C source file
file.h C include file with header data
file.l Fortran listing file
file.o object code (output from a compiler)
file.z compressed file

Unix File Permissions

All files (both ordinary files and directories) have file permissions associated with them. The three types of file permissions are:

Permissions are set for three groups of users: "user" (the userid that created the file), "group", and "other". "group" refers to users who belong to the Unix group with which the file is associated. To see which groups you belong to, type:

groups

"other" means all other users (but does not include the owner or users with group privileges!). To see the file permissions of the files within your current directory, type:

ls -l

You will see output like:


-rw-------   1 abxyz    g123        3743 Dec  7 13:59 memo
-rw-r-----   1 abxyz    g123        7471 Mar 23 17:16 program.f
drwxr-xr-x   1 abxyz    g123        4096 Aug 19  1993 software
...

The first field of 10 columns shows the file permissions:

By looking at the first line of the file permissions, you learn that the ordinar y file, memo, can be read ( r) or written (w) by its owner only. Nobody else has any permissions. The second line shows that the owner can read and modify the file program.f , and that any user belonging to group g123 can read it as well. The third line shows that the owner has all permissions set for the directory called software , and that all users can see the files contained within that directory.

(Note: the above output is the output from the System V Unix ls -l command; the output from the BSD Unix ls -l command would not s how the group name; to see group names with BSD Unix, use ls -lg.)

Changing File Permissions Using Symbolic Permissions< /H3>

To change file permissions, use the chmod command. The syntax of the chmod command is:

chmod what_to_do filename

where what_do_do has 3 fields: who_operator_permissio n:

  • who is one or any combination of:
    • u (for "user")
    • g (for "group")
    • o (for "other")

  • operator is one of:
    • + (adds the permission)
    • - (removes the permission)

  • permission is one or any combination of:
    • r (for "read")
    • w (for "write")
    • x (for "execute")
    • a ( for all permissions)

For example, to remove read and execute permissions for "other" for the software directory listed above:

chmod o-rx software

Changing File Permissions Using Octal Permissions

The chmod command may also be used with what are called octal permissions. Each permission is associated with a number as follows:

  • r is associated with 4
  • w is associated with 2
  • x is associated with 1

Simply add up the permissions you want for "user", "group" and "other" to get a 3 digit octal number, where the first digit indicates the permissions for "user", the second for "group" and the third for "other". The possible digits for each permission group are:

0 for no permissions
1 for x only
2 for w only
3 for x and w
4 for r only
5 for x and r
6 for r and w
7 for all permissions

For example:

  1. chmod 700 filename

    means give all permissions for the file to its owner, and no permissions to anyone else.

  2. chmod 644 filename

    means give read and write permissions to the owner, and read permission only to all other users.

The permissions that are associated with a file when you first create the file depend on your "umask" setting. The default umask on the Cray gives all permissions to the owner of a file, and no permissions to anyone else. This is done by automatically executing the following command whenever you login:

umask 077

The digits used with the umask command are opposite in meaning to the digits used with the chmod command:

7 for no permissions
6 for x only
5 for w only
4 for x and w
3 for r only
2 for x and r
1 for r and w
0 for all permissions

You can change the default umask to your own umask by issuing the umask< /STRONG> command interactively (in which case its effect will only last for your login session) or by putting it in your sh ell startup file.

Some Commands to Use with Files and Directories

The following commands are briefly described below:

ls list files
more display a file one screenful at a time
cat display a file at the terminal
cd change directory
pwd print working directory
mkdir make a new directory
cp copy a file
mv rename or move a file
rm remove a file
head display the first lines in a file
tail display the last lines in a file

ls: list files

Use the ls command to list the names of files and directories t hat match a specified pattern:

ls options [filenames or pattern]

common options:

-l long list (detailed information about the files)
-a include . files
-t sort by date (default is to sort by filename)
-C multi-column output
-F shows the types of files (/ for directories, * for executable files; note that these characters are NOT part of the filenames)

If you don't specify a filename, ls lists all the files in the current directory. The filename is often specified using an *, which represents any group of characters. For example:

ls -l chap*

means "produce a long listing of all files whose filenames start with chap.

(Note that the System V and BSD versions of the ls command are somewhat different.)

more: display a file one screenful at a time

Use the more command to display a file at the terminal, one scr eenful at a time:

more filename

You go to the next screenful by pressing the spacebar. more has its own set of internal commands that allow you to mov e forwards and backwards in the file, and search for keywords. Some of the more commonly used ones are:

spacebar display next screenful
return or enter scroll down 1 line
b scroll back 1 screenful
h display help
q quit out of more
/ string search downwards for string
v enter vi

cat: display a file at the terminal

Use the cat command to type out the entire file at the terminal :

cat filename

cd: change directory

Use the cd command to change to another directory:

cd pathname

Remember that a pathname can be absolute or relative to your current directory. Some examples:

cd /usr/local/lib go to /usr/local/lib
cd .. go up one directory level
cd go from wherever you are to your home directory

pwd: print working directory

Use the pwd command to display the pathname of your current (or working) directory.

mkdir: make a new directory

Use the mkdir command to create a new directory. You must have write privilege in the parent directory:

mkdir pathname

where pathname may be an absolute pathname, or a relative pathname. For example, to make a directory called "project" in your home directory, type the following command from your home directory:

mkdir project

cp: copy a file

Use the cp command to copy an existing file into a new file:

cp existing_filename new_filename

mv: move or rename a file

Use the mv command to move a file from one location to another location, or to rename a file:

mv old_filename new_filename

Examples:

  1. mv program.f project_directory

    moves the ordinary file program.f into the directory project_directory , located one level below the current directory. The filename ( program.f ) doesn't change.

  2. mv program.f newname.f

    renames program.f to newname.f (the file rem ains in the current directory)

rm: remove a file

Use the rm command to permanently delete a file. Once you have deleted the file, you cannot get it back.

rm options filename

Two of the options you can use with rm are:

  1. -i

    Causes rm to prompt you with a message to make sure you really want to remove the file. This is a good option to always use, since it helps prevent you from accidentally deleting a file.

  2. -r

    Removes files "recursively", which means that it removes all of the files in a subdirectory as well as the subdirectory. Use this option when you want to delete a directory.

Example:

rm -i data*

Prompts you about removing all files whose names start with data. Answer y to the prompt if you want the file to be deleted; answer n if you don't want the file to be deleted.

head: display the first lines in a file

Use the head command to display the first count line s in a file:

head -count filename

If you don't specify a count, the default is to show the first 10 lines.

tail: display the last lines in a file

Use the tail command to display the last count lines in a file:

tail -count filename

If you don't specify a count, the default is to show the last 10 lines.

Some Other Useful Unix Commands

The following is a list of Unix and UNICOS commands that you may find useful. For more information, see the man pages or a Unix text.

at Executes commands at a specified time.
awk A pattern-scanning and processing language. It searches a file (or files) for specified patterns and processes lines containing a matched pattern.
chgrp Changes the group associated with a file.
cusage Displays your NCCS CU usage by sponsor code (local NCCS utility).
cut Cuts columns from a file. Example: cat filename | cut -c1-72 > newfile
date Displays the local date and time.
df Displays available physical disk space on file systems.
diff Compares two files and displays the differences.
du Displays the disk usage of a directory. du -s shows how much disk space is being used in the current directory and below.
echo Displays a message.
file Displays the type of file contents (ascii text, directory , fortran program text, executable, ...)
find Locates files located under pathnames that you specify an d that meet the characteristics you specify.
grep Searches a file or files for character strings.
groups Displays the groups you belong to.
kill Terminates processes.
ln Makes a hard or a soft link to a file.
lp, lpr Prints files to a network connected printer.
mailx Sends or receives electronic mail.
make Executes commands in a makefile to update one or more tar get files, which are usually programs. Uses "construction commands" to specify how to "make" the program.
mesg Enables or disables the reception of messages.
newacct Changes the account id to use for charging (UNICOS comman d).
nohup Runs a command that will keep running after you log out.
paste Joins files side by side (converse of cut).
pr Formats a file for printing.
ps Displays information on processes.
quota Displays your file quota and disk usage statistics.
rcp Copies files to or from a remote computer.
rlogin Logs into a remote computer.
sed Edits files according to a script of commands.
showacct Displays your current Cray account (sponsor code); i.e. t he account being used for current charges (local utility).
sleep Sleeps (suspends execution) for a specified number of sec onds.
sort Sorts files.
spell Check a file for spelling errors (not available on charne y).
stty Displays or sets terminal parameters.
tar Stores or retrieves files from an archive file (called a "tar file").
tr Translates specified characters.
uniq Reports repeated lines in a file.
wc Counts lines, words, and characters in a file.
whereis Displays the pathnames of binaries and man pages.
who Reports who is on the system.
write Sends a message to another user.

References

The following were used as references:

  • A Practical Guide to Unix System V by Mark G. Sobell

  • Cray's UNICOS Primer

  • Cray man pages for various Unix commands

Author: NCCS Technical Assistance Group (tag@nccs.gsfc.nasa.gov)
Authorizing Technical Official: Michael S. Seablom, NASA/GSFC Code 931 (Michael.S.Seablom.1@gsfc.nasa.gov)
Authorizing NASA Official: Nancy Palm, NASA/GSFC Code 931 (Nancy.L.Palm.1@gsfc.nasa.gov)
Last Updated: 07/08/99
Reason: Added semicolons to & commands in code



Links to this Page

  • unix system docs last edited on 16 December 2014 at 4:49 pm by dhcp92.hfbk.net
  • enter. last edited on 21 February 2002 at 1:43 am by max2-171.dialin.uni-hamburg.de.