Difference between revisions of "UNIX Tutorial"

From PrattWiki
Jump to navigation Jump to search
m
Line 278: Line 278:
 
| .tex || LaTeX || the uncompiled LaTeX formatted document
 
| .tex || LaTeX || the uncompiled LaTeX formatted document
 
|-
 
|-
| .dvi || kdvi || compiled LaTeX document, which can be viewed
+
| .dvi || okular || compiled LaTeX document, which can be viewed
 
|-
 
|-
| .ps || kghostview || a postscript file used for printing
+
| .ps || okular || a postscript file used for printing
 
|-
 
|-
| .eps || kghostview || encapsulated postscript (e.g. MATLAB graphs and other pictures)
+
| .eps || okular || encapsulated postscript (e.g. MATLAB graphs and other pictures)
 
|}
 
|}
  
Line 612: Line 612:
 
| latex [filename] ||  Converts a LaTeX file into a device independent (<code>.dvi</code>) file  - files opened with this program generally end in <code>.tex</code>
 
| latex [filename] ||  Converts a LaTeX file into a device independent (<code>.dvi</code>) file  - files opened with this program generally end in <code>.tex</code>
 
|-
 
|-
| kdvi <filename> ||  Opens a DVI viewer (to view device independent files) - files opened with this program generally end in <code>.dvi</code>
+
| okular <filename> ||  Opens a DVI viewer (to view device independent files) - files opened with this program generally end in <code>.dvi</code>
 
|-
 
|-
 
| dvips [filename] ||  Sends a device independent file to the ePrint queue - files opened with this program generally end in <code>.dvi</code>
 
| dvips [filename] ||  Sends a device independent file to the ePrint queue - files opened with this program generally end in <code>.dvi</code>
Line 618: Line 618:
 
| dvips -o [outputfile] [inputfile] ||  Converts a device independent file into a postscript file without sending it to the queue
 
| dvips -o [outputfile] [inputfile] ||  Converts a device independent file into a postscript file without sending it to the queue
 
|-
 
|-
| ghostview <filename> ||  Opens a postscript file reader - files opened with this program generally end in <code>.ps</code> or <code>.eps</code>
+
| okular <filename> ||  Opens a postscript file reader - files opened with this program can also end in <code>.ps</code> or <code>.eps</code>
 
|}
 
|}
  

Revision as of 02:04, 28 August 2012

The following tutorial has been a part of the EGR 103 lab manual for several years, and its contents were predominantly organized and collected by Michael Ehrenfried, Pratt 2004. Hopefully, this new web-based version will continue 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:

Symbol What it means
. Current directory
.. Parent directory (the directory above the current one)
~ Current user's home directory
~ID ID's home directory


Note that these shortcuts can be used as parts of relative or absolute paths and can be strung together. For example,

cd ../../../a/b

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

Symbol What It Does
? one character wildcard
[LIST] one character restricted wildcard
* multi-character wildcard

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 command

ls ~mrg/public/a*ing

will list all files in Dr. G's public directory that start with the letter a, end with the letters ing, and have any number of characters in between.

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. The wildcards can also be used together:

ls [a-z]???.dat

will list all files that begin with a lower case letter, have three characters after that letter, followed by a dot and the extension dat.

ls ~mrg/public/???*

will list all files that contain at least three characters.

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 (e.g. 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.

Extension Program Association Type of File
.m MATLAB MATLAB script or function
.mat MATLAB MATLAB-encoded data file
.tex LaTeX the uncompiled LaTeX formatted document
.dvi okular compiled LaTeX document, which can be viewed
.ps okular a postscript file used for printing
.eps okular encapsulated postscript (e.g. MATLAB graphs and other pictures)

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. They are listed in the table above in the The Command - The Action Verb section.

In the examples below, the [ ] and < > are meant to be replaced by the type of objects that are described inside of them. The square brackets represent required arguments while the angle brackets represent optional ones. The symbol in the example listings represents a space.

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.

Option What it does
-a lists all files and directories (including hidden ones)
-l lists in long format giving more information
-p puts a slash (/) after each name if the entry is a directory
-r reverses (alphabetical or date) order of display of the files
-R recursively lists subdirectories encountered
-t orders the files according to time rather than alphabetical order

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.

Example Outcome
cdEGR53 changes to the directory called EGR53 (if present) in your current directory
cd.. changes to the parent directory (directory above the current one)
cd~ changes to your root directory
cd~/EGR53 changes to the directory called EGR53 (if present) in your root directory
cd changes to your home directory

Copy - cp

cp <options> [source directories/files] [destination directory/file]
Option What it does
-i copies interactively - asks before overwriting
-r copies recursively - must use when copying a directory


Example Outcome
cpFile1File2 Copies the file File1 and makes a new file called File2 in the same directory.
cpMyFileFolderThatExists Takes the file MyFile and makes a copy of it in a file also called MyFile which is inside the directory FolderThatExists. Note that if the folder does not, in fact, exist, then there will be a new file called FolderThatExists which is a duplicate of MyFile. To avoid that mistake, use:
cpMyFileFolderThatExists/ Takes the file MyFile and makes a copy of it in a file also called MyFile which is inside the directory FolderThatExists. Note that if the folder does not, in fact, exist, then there be an error
cp-r~mje7/public/lab1/. Copies the directory lab1 and all its contents from ~mje7/public/ into the current directory; the new copy is called lab1.
cp~mje7/public/lab1/*. Copies all the files from ~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.
cp~mje7/public/intro*EGR53 Copies all the files beginning with intro from mje7's public directory to an EGR53 directory that is inside the current directory.

Move (or Rename) - mv

mv <options> [source directories/files] [destination directory/file]
Option What it does
-f move the file(s) without prompting even if it overwrites a file (default for file input)
-i will prompt for conformation whenever the move would overwrite a file (default for console input)


Example Outcome
mvfoo.mlab1/ Moves a file foo.m into the lab1 directory, which is found in the current directory.
mv../lab1/*.tex. Move all files with the file extension .tex from the lab1 directory (which is found in the parent of the current directory) into the current directory.
mv-ffoo.mbar.m Moves the file foo.m to a file bar.m. This is similar to renaming the file. If bar.m already exists the -f will cause this file to be overwritten automatically.

Remove a File (Delete) - rm

rm <options> [directories/files]

Warning: These options are powerful and permanent.

Option What it does
-r removes recursively all directories and subdirectories in

the argument list

-i prompts the user before removing any files (interactive)
-f remove all files without prompting the user (default setting)
Example Outcome
rmfoo.m Removes (deletes) foo.m from the current directory
rm-i*.m../bar.m Removes all files ending in .m from the current directory, as well as bar.m from the parent directory. The -i will cause UNIX to prompt the user for permission to remove these files.
rm-irlab1/ Removes all of the contents of the the current directory's lab1/ including all subdirectories and their files. It will also prompt the user for permission to remove these files.

Make a Directory - mkdir

mkdir [new directory paths]
Example Outcome
mkdirlab1 Makes a directory called lab1 in the current directory.
mkdir../junk Makes a directory called junk in the parent of the current directory.
mkdir~/foobar Makes a directory called foobar in the user's home directory.

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 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.

Option What it does
-k [keyword] brings up descriptions of all manual pages containing the keyword
Example Outcome
manlatex Brings up the manual pages for the latex command
man-klatex Brings up the summary descriptions of manual pages containing the word latex

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.

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

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]
fs setacl -dir [directory path] -acl [user] [permission type]

The latter case must be used if the directory path portion includes multiple paths (for example, with a wildcard).

Users

User Who is given rights
[any login id] the user associated with the login ID
system:anyuser any Duke user
system:administrators system administrators
mrg:egr53ta EGR53 TAs and professors


Permission Types

Code Meaning What right is given
r read ability to read files
l lookup ability to list files
i insert ability to add a file
d delete ability to delete a file
w write ability to write to files
k lock ability to have processes lock files
a administer ability to change permissions
none none no rights given
read read rl rights given
write write rlidwk rights given (all but administer)
all all all rlidwka rights given
Example Outcome
fs setacl . mfs7 rl Gives the user mfs7 read and lookup permissions to the current directory. Using the permission read instead of rl would have accomplished the same task.
fs setacl ~/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.
fs setacl .. system:anyuser w Gives any user on the system write access to the parent of the current directory.
fs setacl -dir ~/public/* -acl mrg read Gives mrg read and lookup rights on all directories inside the current user's public directory.

View Permissions - fs listacl

fs listacl [directory path]
Example Outcome
fs listacl . Lists who has access to the current directory. The . is optional in this case, as typing fs listacl defaults to the current directory.
fs listacl lab1/ Lists who has access to a directory called lab1 which is inside the current directory.

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

Command What it does
emacs <filename> Opens basic word processing program; if a filename is given, it will open that file if it exists or create it if it does not
latex [filename] Converts a LaTeX file into a device independent (.dvi) file - files opened with this program generally end in .tex
okular <filename> Opens a DVI viewer (to view device independent files) - files opened with this program generally end in .dvi
dvips [filename] Sends a device independent file to the ePrint queue - files opened with this program generally end in .dvi
dvips -o [outputfile] [inputfile] Converts a device independent file into a postscript file without sending it to the queue
okular <filename> Opens a postscript file reader - files opened with this program can also end in .ps or .eps

Other Commands

Command What it does
acroread <filename> Opens an Adobe Acrobat viewer
firefox Opens a web browser
gimp <filename> Opens a graphics viewer
mathematica Starts symbolic math package Mathematica
matlab Starts MATLAB program
ooffice <filename> Starts OpenOffice
more [filename] Types the contents of the file to the screen
xeyes Initiates 1984 protocol
xlogo Initiates shameless self-promotion
xmaple Starts symbolic math package Maple


Questions

Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.

External Links

References


Class Document Protection