UNIX Tutorial

From PrattWiki
Revision as of 17:26, 7 July 2008 by DukeEgr93 (talk | contribs)
Jump to navigation Jump to search

The following tutorial has been a part of the EGR 53 lab manual for several years. Hopefully, this new web-based version will prove to be a useful resource for students in the course as well as the rest of the Pratt family.


UNIX

Unlike Windows or Mac OS, UNIX is a command-line based operating system. In essence, it means that instructions have to be typed in as commands instead of using the mouse to point, click, and drag. As such, some commands will have to be memorized in order to use the UNIX system.

The following commands - specific to the Linux brand of UNIX deployed on Duke's public clusters - have been divided into Basic, Intermediate and Advanced commands. Basic commands are essential to use the system, and should be memorized as quickly as possible. Intermediate commands will be commonly used by average users, and are recommended to be memorized. Advanced commands will be seldom used by an average user.

When using the public UNIX clusters, under the new Graphical User Interface, a terminal (command-line driven) window may not open automatically when you log in. Therefore, when you first log in, you will merely see a desktop with some icons in the upper left corner. There will also be a menu bar with graphical menus and two text menus called "Applications" and "Actions." One way to open a terminal window is to drag down the "Applications" menu, go into the "System Tools" submenu, and then select "Terminal." Another method is to right-click in the background and select "Open Terminal" from the menu. These two methods of opening the program yield identical results.

If you would like for your menu bar to have an "Open Terminal" icon, you can put one there simply clicking to open the "Applications" menu, navigating to the "Terminal" selection, and instead of letting go of the mouse, drag the item off the menu and up into the bar at the top of the screen. This will work with any of the items in the "Application" menu as long as you remember to click, not click-hold, the word "Applications.'"

When you are finished and your menu bar is the way you like it (also, you can leave terminal windows open if you wish for them to appear automatically when you log in next time), simply select the option "Save Current Setup" right before clicking "OK" to log out.

When you open the Terminal, you will automatically be taken to your home directory. Note that a graphical representation of this can be obtained by double-clicking the house icon that says "YourID's Home" on the desktop; however, it is not advisable that you do things this way (though it may seem easier at first). It is extremely important that you learn to use the UNIX commands in the terminal for many reasons, one of which being that you cannot access this graphical interface remotely (and, therefore, if you want to access files on your account from your personal computer, you must know the UNIX commands or you will not be able to).

Structure of the Command Line - The Sentence

Each command line can be broken down into several parts. There are three primary "parts of speech" to issuing commands - the verb, the object, and the adverb. Not every command will require all parts, but it is important to know when they will and what they are.

The Command - The Action Verb

The first word of the prompt is the specific command of the line, equivalent to an action verb in English. This tells the computer what to do. A few of the commands we will use are listed below.

Command What it does Graphical Users Equivalent
pwd prints the name of the working directory path given in window
ls lists files in a directory viewing a folder's contents
cd changes a directory double clicking on a folder
cp copies a file or folder copying the contents of a file or folder
mv moves/renames a file or directory dragging an item to a new place/renaming it
rm removes a file dragging a file to the trash and emptying it
mkdir creates a new directory creating a new folder
rmdir removes an empty directory dragging folder to the trash and

emptying it

man pulls up manual pages opening help files

Files and Directories - The Objects

Many commands need specific objects in order to carry out their function. If you ask the computer to remove (rm) it also needs to know which files and/or directories it is that you would like to remove. In many graphical interfaces the computer is told to do this by clicking on an item or slick-dragging to select multiple items. In a text based interface, the information is provided by giving a path to the elements to be removed or by providing a pattern.

Paths

A path is where the directory or file is located in relation to other files and folders. On a Windows machine the hard drive can be thought of as the root directory - it is the directory from which all other parts of the file tree flow. If you wanted to open Starcraft, you might first have to open the hard drive, then double click on a Programs folder, followed by a Games folder, then a Starcraft folder, and then finally get to Starcraft.exe. This is the path you took to open Starcraft.

Note that there are two primary types of paths - absolute paths and relative paths. Absolute paths always start from the root directory; their main advantage is an absolute path uniquely identifies a particular element, regardless of where you are in the file tree. The major disadvantage is that absolute paths can get very long. The absolute path represented above, for example, might be:

C:\Programs\Games\Starcraft\Starcraft.exe

In a UNIX system, the root is represented by a single slash - the / symbol. Absolute paths therefore always start with / and go from there. For example, the absolute path to the UNIX command xeyes is

/usr/bin/xeyes

If you use this as an object of a UNIX command, no matter where you sit in the file tree, UNIX will be able to find that program.

On the other hand, relative paths are just that - the path required to get where you are going based upon where you are in the system. For example, if you were currently in the /usr directory in UNIX, you could access the xeyes program with the path bin/xeyes. NOTE: relative paths do not start with the / because you do not want the path to go back to the root; you want to start where you are and move from there.


Other Ways to Represent Locations

Now that we have a basic understanding of what a path is we can throw in a few shortcuts to make defining to the computer which objects we mean simpler. Sometimes you wish to perform a command on the current directory. Or perhaps you would like to back up a level in the hierarchy to reach another folder. Or finally, you may want to use something in your home directory or another user's home directory. The shortcuts for these tasks are:

\( \begin{tabular}{|c|c|} \hline \textbf{Symbol} & \textbf{What it means} \\ \hline {\tt{.}} & Current directory \\ {\tt{..}} & Parent directory (the directory above the current one)\\ {\tt $\sim$ } & Current user's home directory \\ {\tt $\sim$ID} & ID's home directory\\ \hline \end{tabular} \)

Note that these shortcuts can be used as parts of relative or absolute paths and can be strung together. For example, <sourse lang="bash"> cd ../../../a/b </source> is a relative path - it does not start with a slash - that tells UNIX to change directories by starting wherever the user currently is, then going up a level, going up another level, going up another level, then from there entering the a directory, and from the a directory going into the b directory. As another example,

cd ~mrg/public/EGR53

is an absolute path - the ~mrg is a shortcut for the absolute path /afs/acpub/users/m/r/mrg - that will get into Dr. G's home directory, from there the public directory, and from there the EGR53 directory. Unlike the previous example, this command will work the same from anywhere in UNIX, because it is an absolute path.

Wildcards

Wildcards are a powerful tool when you want to deal with multiple files or directories at once. The first of the wildcard characters is the ?. This will replace any one character. Using the pattern c?t would thus include both the word cat and cot, but not the word cast, because in cast there are two characters between the c and the t. Likewise cots would not be included because there is a letter after the t.

A modified version of the ? involves putting particular characters inside square brackets. This wildcard is allowed to replace any of the characters contained within the brackets and no other. It is still a single-character replacement, but it is restricted. For example,

ls p[aeiou]t

will list all files with three-letter long names that start with p, end in t, and have a, e, i, o, or u in the middle.

ls [a-m, 3-6]?

will list all files with two-letter long names that start with either a through m or the numbers 3 through 6.

This brings us to the * wildcard. The * wildcard replaces as many characters as necessary - including no character. Therefore typing c*t would still represent cat and cot, but it would also include {careerist and ct. It would not include crate, however, since nothing in the pattern replaces anything after the t.

The ? and * are the most used wildcards, and are often seen at the beginning and end of patterns. For example, EGR* would include all files beginning with EGR, while *.m would include all files that end in '.m'. In summary:

\( \begin{tabular}{|c|c|c|} \hline \textbf{Symbol} & \textbf{What It Does} \\ \hline {\tt{?}} & one character wildcard \\ {\tt [LIST]} & one character restricted wildcard\\ {\tt{*}} & multi-character wildcard\\ \hline \)

Options - The Adverbs

Commands often have options that are associated with them that allow them to change their functionality based on user preference. Usually they are represented by a character or a word that is preceded by a dash. A list of options can almost always be found by typing man [name of command] where [name of command] is the command whose list of options is desired. A few of these options are mentioned as each command is described in the Basic Commands section below.

Arguments - Everything Else

Sometimes other information that the computer needs to know to perform the command successfully will be required of the user. As with the options the man pages explain all needed arguments.

File Extensions

Often times attached to the end of a file name is a file extension. This is usually represented by a dot (.) followed by an identification tag. Each program, or type of file will generally have a file extension traditionally associated with them (eg. Microsoft Word uses .doc). In this course we will be using primarily MATLAB and LaTeX. The table below identifies the extensions with which you should become familiar.

\( \begin{tabular}{|c|c|c|} \hline \textbf{Extension} & \textbf{Program Association} & \textbf{Type of File} \\ \hline {\tt{.m}} & MATLAB & MATLAB script or function \\ {\tt{.tex}} & \LaTeX & the uncompiled \LaTeX ~formatted document \\ {\tt{.dvi}} & kdvi & compiled \LaTeX ~document, which can be viewed \\ {\tt{.ps}} & kghostview & a postscript file used for printing\\ {\tt{.eps}} & Graphics & MATLAB graphs and other pictures \\ \hline \end{tabular} \)

Note, however, that UNIX is not nearly as dependent on extensions as other operating systems. UNIX will allow you to change extensions without warning, to use extensions of more than three characters, and to use extensions of extensions. Some programs - MATLAB in particular - do depend on having the correct extensions to translate the purpose of a file.

Basic Commands

This section describes some of the essential UNIX commands that we will be using in this course. The [ ] are meant to be replaced by the type of objects that are described inside of them. Note that only select options are shown; browsing the man pages yourself will show you a complete list of options. Much of the information below is either copied from or adapted from the man pages for the particular command.

Print Working Directory - pwd

pwd

This command prints the absolute path of the current directory.

List contents - ls

ls [options] [directory path to list]

This command will list the directory contents.

\( \begin{center} \begin{tabular}{|c|c|} \hline \bf{Option} & \bf{What it does} \\ \hline {\tt -a} & lists all files and directories (including hidden ones)\\ {\tt -l} & lists in long format giving more information \\ {\tt -p} & puts a slash (/) after each name if the entry is a directory \\ {\tt -r} & reverses (alphabetical or date) order of display of the files \\ {\tt -R} & recursively lists subdirectories encountered \\ {\tt -t} & orders the files according to time rather than alphabetic order\\ \hline \end{tabular} \)

Note: These options can be combined. For example,

ls -lart EGR53

will use the long format to list all files in reverse order sorted by time.

Change Directory - cd

cd [directory path]

This command changes the directory to the directory defined by the path.

\( \begin{tabular}{l|l} {\bf Example} & {\bf What it does} \\ \hline {\tt cd EGR53} & changes to the directory called EGR53 (if present) in your current directory \\ {\tt cd ..} & changes to the parent directory (directory above the current one) \\ {\tt cd $\sim$} & changes to your root directory \\ {\tt cd $\sim$/EGR53} & changes to the directory called EGR53 (if present) in your root directory \\ {\tt cd} & changes to your home directory \end{tabular} \)

Copy - cp

cp  [options] [source directories/files] [destination directory/file]

\( \begin{tabular}{|c|c|} \hline \textbf{Option} & \textbf{What it does} \\ \hline {\tt -i} & copies interactively - asks before overwriting\\ {\tt{-r}} & copies recursively - must use when copying a directory\\ \hline \end{tabular} \)

\( \begin{tabular}{l|p{10cm}} {\bf Example} & {\bf What it does} \\ \hline {\tt{cp -r $\sim$mje7/public/lab1/ .}} & Copies the directory {\it{lab1}} and all its contents from {\it{$\sim$mje7/public/}} into the current directory; the new copy is called {\tt lab1} \\ \hline {\tt{cp $\sim$mje7/public/lab1/* .}} & Copies all the files from {\it{$\sim$mje7/public/lab1}} into the current directory. Note the subtle difference from the previous example. Above, the directory and the files were copied together. Here, only the files are copied meaning the files would be in the current directory. \\ \hline {\tt{cp $\sim$mje7/public/intro* EGR53}} & Copies all the files beginning with {\tt intro} from mje7's {\tt{public}} directory to an {\tt{EGR53}} directory that is inside the current directory. \\ \end{tabular} \)

Move (or Rename) - mv

mv [options] [source directories/files] [destination directory/file]

\( \begin{tabular}{|c|c|} \hline \textbf{Option} & \textbf{What it does} \\ \hline {\tt{-f}} & move the file(s) without prompting even if it overwrites a \\ & file (default for file input) \\ \hline {\tt{-i}} & will prompt for conformation whenever the move would overwrite \\ & a file (default for console input)\\ \hline \end{tabular} \)

\( \begin{tabular} {l|p{12.5cm}} {\bf Example} & {\bf What it does} \\ \hline {\tt{mv foo.m lab1/}} & Moves a file {\it{foo.m}} into the {\tt{lab1}} directory, which is found in the current directory.\\ \hline {\tt{mv ../lab1/*.tex .}} & Move all files with the file extension {\tt{.tex}} from the lab1 directory (which is found in the parent of the current directory) into the current directory. \\ \hline {\tt mv -f foo.m bar.m} & Moves the file {\tt{foo.m}} to a file {\tt{bar.m}}. This is similar to renaming the file. If {\tt{bar.m}} already exists the {\tt{-f}} will cause this file to be overwritten automatically. \\ \end{tabular} \)

Remove a file (Delete) - rm

rm [options] [directories/files]

Warning: These options are powerful and permanent.

\( \begin{center} \begin{tabular}{|c|c|} \hline \textbf{Option} & \textbf{What it does} \\ \hline {\tt{-r}} & removes recursively all directories and subdirectories in the argument list\\ {\tt{-i}} & prompts the user before removing any files (interactive)\\ {\tt{-f}} & remove all files without prompting the user (default setting)\\ \hline \end{tabular} \)

\( \begin{tabular}{l|p{12.5cm}} {\bf Example} & {\bf What it does} \\ \hline {\tt{rm foo.m}} & Removes (deletes) {\it{foo.m}} from the current directory \\ \hline {\tt{rm -i *.m ../bar.m}} & Removes all files ending in {\it{.m}} from the current directory, as well as {\it{bar.m}} from the parent directory. The {\it{-i}} will cause UNIX to prompt the user for permission to remove these files. \\ \hline {\tt{rm -ir lab1/}} & Removes all of the contents of the the current directory's {\it{lab1/}} including all subdirectories and their files. It will also prompt the user for permission to remove these files. \\ \end{tabular} \)


Make a Directory - mkdir

mkdir [new directory paths]

\( \begin{center} \begin{tabular}{l|l} {\bf Example} & {\bf What it does}\\ \hline {\tt{mkdir lab1}} & Makes a directory called {\it{lab1}} in the current directory. \\ {\tt{mkdir ../junk}} & Makes a directory called {\it{junk}} in the parent of the current directory. \\ {\tt{mkdir $\sim$/foobar}} & Makes a directory called {\it{foobar}} in the user's home directory.\\ \end{tabular} \)

Remove a Directory - rmdir

rmdir [directories]

Note: In order for this command to work the directories must first be empty.

Help Manual - man

man [options] [command name/keyword]

This command brings up the manual pages for UNIX commands.

\( \begin{tabular}{|c|c|} \hline \textbf{Option} & \textbf{What it does} \\ \hline {\tt{-k [keyword]}} & brings up descriptions of all manual pages containing the {\tt keyword}\\ \hline \end{tabular} \)

This command is the key to learning more about UNIX. To exit out of the manual without toggling to the end, just press q. To move down the various pages, press the spacebar.

\( \begin{tabular}{l|l} {\bf Example} & {\bf What it does} \\ \hline {\tt man latex} & Brings up the manual pages for the {\tt latex} command\\ {\tt man -k latex} & Brings up the summary descriptions of manual pages containing the word {\tt latex} \end{tabular} \)

Intermediate Commands

A summary of commonly-used commands are included in the table below. In the long run, these commands will be relatively often-used by the average user.

\( \begin{tabular}{|l|l|} \hline {\bf Command} & {\bf What it does} \\ \hline {\tt CTRL + Z} & Suspends the most recently executed program \\ \hline {\tt bg} & runs the most recently executed program in the background \\ \hline {\tt fg} & runs the most recently executed program in the foreground. \\ \hline {\tt ps -u ID} & lists running processes for user ID\\ \hline {\tt kill [Job number]} & terminates the program with the job number entered. \\ & {\footnotesize Note: The job number is on the line following the one where the command was given} \\ \hline {\tt logout} & logs out of the computer that you have remotely logged into.\\ \hline {\tt lpr [filename]} & Prints the file to the ePrint queue\\ \hline {\tt passwd} & Changes the user's password \\ \hline {\tt [program name] \&} & Runs the program in the background, so it does not lock up the xterm window \\ \hline {\tt du -ks [file/directory]} & Lists how much space a particular file or directory takes\\ \hline {\tt quota} & Shows how much disk space you have used of your quota \\ \hline {\tt ssh [IP address]} & logs into another computer \\ & {\footnotesize (eg. {\tt ssh teer14.oit.duke.edu} logs into the teer 14 computer)} \\ \hline {\tt w} & Lists the users logged into the same computer and the programs they are running \\ \hline {\tt who} & Lists of users logged into the same computer and their IP addresses \\ \hline {\tt xterm \&} & Call up an additional xterm window without locking the current one\\ \hline \end{tabular} \)


Advanced Commands

For more information, check the online manual within the UNIX system using the

man [command name]
man -k [keyword]

commands.

Set Permissions - fs setacl

fs setacl [directory path] [user] [permission type]

Users

\( \begin{tabular}{|c|c|} \hline \textbf{User} &\textbf{Who is given rights} \\ \hline {\it{[any login id]}} & the user associated with the login ID \\ {\tt{system:anyuser}} & any Duke user \\ {\tt{system:administrators}} & system administrators \\ {\tt{mrg:egr53ta}} & EGR53 TAs and professors \\\hline \end{tabular} \)

Permission Types

\( \begin{center} \begin{tabular}{|c|c|c|} \hline \textbf{Permission} &\textbf{What It Stands For} & \textbf{What It Allows} \\ \hline {\tt{r}} & read & ability to read files \\ {\tt{l}} & lookup & ability to lookup directory information \\ {\tt{i}} & insert & ability to add a file \\ {\tt{d}} & delete & ability to delete a file \\ {\tt{w}} & write & ability to write to files \\ {\tt{k}} & lock & ability to have processes lock files \\ {\tt{a}} & administer & ability to change permissions \\ {\tt{none}} & & no rights given \\ {\tt{read}} & & {\tt{rl}} rights given\\ {\tt{write}} & & {\tt{rlidwk}} rights given \\ {\tt{all}} & & all rights given ({\tt{rlidwka}})\\ \hline \end{tabular} \)

\( \begin{tabular}{l|p{9.5cm}} {\bf Example} & {\bf What it Does} \\ \hline {\tt{fs setacl . mfs7 rl}} & Gives the user {\it{mfs7}} read and lookup permissions \\ & to the current directory. Using the permission {\it{read}} \\ & instead of {\it{rl}} would have accomplished the same task.\\ \hline {\tt{fs setacl $\sim$/EGR53/ mrg:egr53ta all}} & Gives EGR53 TAs and professors all access rights \\ & to an EGR53 directory, which is located in the \\ & current user's home directory. \\ \hline {\tt{fs setacl .. system:anyuser w}} & Gives any user on the system write access \\ & to the parent of the current directory.\\ \end{tabular} \)

View Permissions - fs listacl

fs listacl [directory path]

\( \begin{tabular}{l|l} {\bf Example} & {\bf What it Does} \\ \hline {\tt{fs listacl .}} & Lists who has access to the current directory. The {\tt{.}} is optional in this case, as typing\\ & {\tt{fs listacl}} defaults to the current directory. \\ \hline {\tt{fs listacl lab1/}} & Lists who has access to a directory called {\it{lab1}} which is inside the current directory.\\ \end{tabular} \)

Commonly Used Programs in UNIX

These commands are broken up into two groups. The first is the group most closely associated with processing LaTeX documents while the second group is more generic.

LaTeX Commands

\( \begin{tabular}{|l|l|} \hline {\bf Command} & {\bf What it Does} \\ \hline {\tt emacs {\it [filename]}} & Basic word processing software. \\ \hline {\tt latex [filename]} & Converts a .tex file into a .dvi file \\ \hline {\tt kdvi {\it [filename]}} & Opens a DVI viewer (to view .dvi files) \\ \hline {\tt dvips [filename]} & Prints a .dvi file \\ & {\footnotesize ~ {\tt dvips -o [outputfile] [inputfile]} $\rightarrow$ Converts a .dvi file into a PostScript file without printing}\\ \hline {\tt kghostview {\it [filename]}} & Opens a PostScript file reader. \\ \hline \end{tabular} \)

Other Commands

\( \begin{tabular}{|l|l|} \hline {\bf Command} & {\bf What it Does} \\ \hline {\tt acroread {\it [filename]}} & Opens an Adobe Acrobat viewer. \\ \hline {\tt firefox} & Opens a web browser\\ \hline {\tt gimp {\it [filename]}} & Opens a graphics viewer. \\ \hline {\tt mathematica} & Starts symbolic math package Mathematica\\ \hline {\tt matlab} & Starts MATLAB program \\ \hline {\tt ooffice {\it [filename]}} & Starts OpenOffice\\ \hline {\tt more [filename]} & Types the contents of the file to the screen\\ \hline {\tt xeyes} & Initiates 1984 protocol\\ \hline {\tt xlogo} & Initiates shameless self-promotion\\ \hline {\tt xmaple} & Starts symbolic math package Maple\\ \hline \end{tabular} \)