UNIX Tutorial

From PrattWiki
Jump to: navigation, search

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. In 2015, the tutorial was re-written to reflect the fact that Duke no longer has any physical Linux machines but rather provides access to a Linux environment through a virtual cluster.


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.

Note that when you connect to a Linux server through ssh, you will automatically be taken to your home directory.

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.

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. Note that as of 2013, however, users are not allowed access to any part of any other user's directory, and so on the Duke system, the command above will return either "Permission Denied" or "No such file or directory."

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 evince compiled LaTeX document, which can be viewed
.ps evince a postscript file used for printing
.eps evince encapsulated postscript (e.g. MATLAB graphs and other pictures)
.pdf evince a PDF file used for viewing and printing

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
cdEGR103 changes to the directory called EGR103 (if present) in your current directory
cd.. changes to the parent directory (directory above the current one)
cd~ changes to your root directory
cd~/EGR103 changes to the directory called EGR103 (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.


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
dvipdf [filename.dvi] Converts a DVI file into a PDF file; uses letter size
evince [filename.pdf] Views the PDF file; you can also print from here.

Other Commands

Command What it does
firefox Opens a web browser
gimp <filename> Opens a graphics viewer
mathematica9 Starts symbolic math package Mathematica
matlab Starts MATLAB program
ooffice <filename> Starts LibreOffice
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