Parker Morse




Дата канвертавання21.04.2016
Памер80.38 Kb.
Tutorial
UNIX – Shell – Emacs

Authors:
Eddie Aftandilian

Parker Morse


Umaa Rebbapragada

Nathan Ricci

Gabe Wachman
January 26, 2007
Department of Computer Science

Tufts University



Command-Line Basics 4

Tab Completion 4

Home Directory 4

Command History 4

Getting Help and man 5

stdin, stdout, stderr 6

pipes 6

I/O redirection 6



Locating Files 6

ls 7


find 7

locate 7


wildcards 7

ln 8


Information About Files 8

cat 8


less 8

wc 9


grep 9

diff and sdiff 9

sdiff 10

Exercise Set 1 10



User Identity 10

groups 10

permissions 10

Job Control 11

Ctrl-Z 11

bg 12

top 12


Connecting to remote systems: 12

ssh and scp 12

Exercise Set 2 13

Shell 14

Shell variables 14

Environment variables 14

Important Shell Variables 14

Important Environment Variables 14

Aliases 14

Startup Scripts 15

Examples 15

Example .cshrc 15

Examine System Defaults 16

Exercise Set 3 16

Emacs 17

Emacs vs. xemacs 17

Saving and exiting: 17

Moving in emacs: 17

Search and Replace: 18

Multiple windows: 18

Exercise Set 4: 18

.Emacs file 18

Undo 19

Kill 19


Yank 19

Kill Ring 19

Deleting 19

Mark 19


Killing a Region 20

Cool Kill/Delete Commands 20

Mini-Buffer 20

M-x 20


Cool Mini-buffer commands: 20

Exercise Set 5 20




Command-Line Basics




Tab Completion


This is probably the most useful feature of the shell. It allows you to type only part of the name of a file or directory and have the shell fill in the rest for you. For example, let’s say I want to change into the really_long_directory_name directory. I can type, cd rea[TAB] and the shell will fill in the rest for me, as long as there is no other file or directory that starts with rea. If there is, I can press TAB twice quickly to show a list of files/directories that match this prefix.

Home Directory


Because UNIX systems are designed to be used by many people at once, every user gets his/her own home directory. This is the directory you start in when you log into the system. If you want to quickly refer to your home directory in a path, you can use the “~” character. For example, if I want to change into my home directory, I can type:

cd ~
You can also use the ~ in a path name:

cd ~/comp15

Which will change into the comp15 directory that is off my home directory.


Finally, if you want to reference someone else’s home directory, you can use ~[username] in the same way. This is useful when you are working with someone else on a project.

Command History


Command history is just a fancy way of saying, “What commands have I typed in the past?” It’s very convenient because you don’t have to type a long command multiple times. There are two ways to access your command history: by using the up-arrow and down-arrow or by using the command history.
First, the up-arrow and down-arrow let you cycle through the last few commands you’ve typed. For example, suppose I am editing a C source file in emacs and trying to compile my program. My command history might look like:

emacs source.c

make

Instead of typing these commands every time, I can simply use the up-arrow key to cycle back to whatever command I want, then press ENTER to run it. It saves me from having to type the same command over and over.


Second, there’s the command “history”, which shows a list of every command you’ve typed at the shell prompt. You can execute a command by typing ![number] where [number] is the number of the command in the history list. For example, let’s say I type “history” and I get this:
1218 fg

1219 ll


1220 java TraceParser Vertex.java

1221 man gzip

1222 gzip -c Vertex.java > Vertex.java.gz

1223 ll


1224 java TraceParser Vertex.java.gz

  1. rm Vertex.java.gz

If I type !1221 it will re-run man gzip for me.


As an aside, if you type !$ at the command prompt, it will repeat the argument to the last command you typed. For example, suppose I am editing a Java program and trying to compile it. I can type:

emacs long/path/to/source/file/Source.java

javac !$

And it will pass the “long/path/to/source/file/Source.java” argument to the javac command without having to type the entire path again.



Getting Help and man


Let’s say you know the name of a command, but you don’t remember how to make it do a specific thing. For example, you want to use ls to list all the files in a directory and order them by size. Most UNIX commands accept the option --help, which prints a brief help page about the command. For example, typing ls --help will tell you to use the -S to make ls sort by file size.
Now suppose you need a more thorough reference for a command; --help just doesn’t provide enough information. You’ll want to use man. man is a command that prints a long manual for any command on the system, as well as C library functions and UNIX system calls. For example, you can type man ls for a long manual on ls. You can even type man man if you need a reference on man.
There’s also the concept of different man sections. These are represented by numbers from 1 to 8, and each section contains man pages for different classes of commands. Typically, the sections are divided as follows:

  1. General commands

  2. System calls

  3. C library functions

  4. Special files (usually devices, those found in /dev) and drivers

  5. File formats and conventions

  6. Games and screensavers

  7. Miscellanea

  8. System administration commands and daemons

The sections are important if you are looking for a command that appears in more than one section. Let’s say you want the man page for the C function printf. If you just type man printf, you’ll get the man page for the printf command on the command line because man searches section 1 by default. To get the man page for the printf() C function, you need to specify the section:

man 3 printf

stdin, stdout, stderr


In UNIX, programs have standard places to look for input/output. stdin is the standard place where programs look for input. Usually this comes from the keyboard, i.e. if you have a C++ program that reads from cin, it’s reading from stdin. stdout is the standard place where programs output; typically this goes to the screen. It corresponds to cout in C++. stderr is the standard place for programs to output error info; it also usually goes to the screen. It corresponds to cerr in C++.

pipes


Pipes are one of the most powerful constructs in UNIX. They allow you to connect the output of one command to the input of another command. For example, let’s say you want to count the number of files in a directory. You can do this by piping the output of ls into wc (word count):

ls | wc -l

Formally, here you are piping the stdout of ls to the stdin of wc.

I/O redirection


I/O redirection is similar to pipes, except that instead of sending output from one command to another, you send the output to a file. This is useful if a program outputs a large amount of data, and you want to keep it and search it afterward. For example, you could store a listing of the files in a directory by typing:

ls > file_list


Using the > character redirects output from stdout into the file. If you want to redirect stderr instead, you would use 2> instead.

Locating Files




ls


ls lists the files in a directory. Some useful options:

-l displays the list of files with much more information, including owner, group, file size, permissions, and modified date/time. I always use ls with this option. Add the -h option to make the file sizes more readable (instead of displaying everything in bytes, it will display “3.2M” for a 3.2 megabyte file).

-S sorts the files by size, from high to low.

-t sorts the files by time modified, from most to least recent.

-r recursively descends into subdirectories.

-a shows all files, including hidden files (files whose names start with a period).



find


find lets you search a directory tree for a particular file. I mostly use it to search for a file with a particular name. Let’s say I’m working on a big source tree, and I need to find the file named Compiler.java. I would type:

find -name Compiler.java


find can also find files by other criteria. For example, you can find all files that have been modified in the last 10 minutes by typing:

find -mmin -10


Check the man page for many more criteria you can search on.

locate


locate is also used for finding files, but it works in a different way. It builds a database of all the files on the system, and when you type locate, it simply queries the database for the file you’re looking for. The advantage is that locate is much faster than find. The disadvantage is that locate updates the database infrequently (usually one per day), so it won’t pick up recent changes in the filesystem.
Let’s say I want to find all files on the system whose filenames or paths contain “java”. I would type:

locate java



wildcards


Wildcards are used to match inexact filenames. There are a number of these that you can use:

* - Matches any number of characters, including none.

? - Matches any single character.

[ ] - Enclose a set of characters, any one of which may match a character at that position.


Let’s say I want to see all files in a directory that have the extension .c. I would use the * wildcard character to tell ls to match files that contain [any string].c:

ls *.c


ln


ln is used to create links between files. Let’s say you have a large file somewhere in your directory structure, and you want to be able to access it with digging all the way down to it. You could create a link using ln:

ln -s [file] [link]

The link behaves just like the real file. You can use normal commands on it, edit it, etc., and all changes are reflected in the real file.
There are two types of links you can create with this command: hard links and soft links. A hard link behaves exactly like the original file; it is just another filesystem entry pointing at the same data. A symbolic link is more like a Windows shortcut: It’s a separate file that contains a path to the file being linked to. Hard links have more limitations; for example, they cannot cross filesystems, and you can’t create a hard link to a directory. Symbolic links are more flexible and are supported by most modern systems, so you’ll want to use symbolic links if possible. You create a symbolic link by specifying the -s option to ln.

Information About Files

cat


cat prints the contents of a file to standard out. It’s a quick way of viewing a text file. You can also specify multiple files as arguments, and cat will concatenate them and print them to standard out.

less


less is a simple file viewing tool. It won’t let you edit a file, but you can read it and search through it. It also lets you scroll up and down and is very fast. This is my primary tool for viewing text files.
A good use of less is to view output from programs. If you have a program that will output a large amount of text to the screen, pipe the output to less, and you will be able to read it without it scrolling off your screen:

program | less



wc


I mentioned wc earlier but never fully described it. wc stands for “word count”; it lets you count the number of words or lines in a file. It’s useful for things like counting the number of files in a directory, the number of matches in a find search, etc.

grep


grep is an extremely powerful search tool. It searches the content of files for certain strings or patterns. For example, you could search all files in a source tree for a particular class name:

grep -r ClassName *

The -r option tells grep to recurse down subdirectories, and the * tells it to search all files in the current directory.
grep’s real power lies in searching by regular expression. Regular expressions are beyond the scope of this tutorial, but they let you specify a very particular expression that you want matched. For example, the RE “^0x[0-9a-f]{8}” will match any line that starts with a hexadecimal value. For more info, try Google.
Again, grep is often used with pipes. Suppose you want to search a directory listing for all files whose names contain the string “read”. You could use grep for this:

ls -l | grep read



diff and sdiff


diff compares two files or directories for differences. Suppose you’ve been working on a C++ source file, and you want to see what you’ve changed from the original. Use diff:

diff working/file.cpp original/file.cpp


If you want to compare a whole directory (e.g. you’ve been working on a source tree and want to see which files you’ve modified), you probably want to use the --recursive option so that it will compare subdirectories too.
diff is often used to generate a patch for source code that you’ve modified. If I’m working on a large project and I want to send my changes to someone else, I’ll run diff recursively on the project directory and redirect the output to a file, then send that file to the other person:

diff --recursive project/ original/ > patch_file



sdiff


sdiff is similar to diff, but it is interactive and lets you merge two files. Suppose you and a classmate are working on a group project, and you both modify the same source file. You would use sdiff to merge the two modified files into a single file.



Exercise Set 1





  1. Using cp, copy the whole directory tree from /r/uxpdworkshop/eaftan/jikesrvm into your working directory. You’ll have to use help to find the correct option(s) to cp to do this.

  2. Within the jikesrvm source tree, find the file called GCTrace.java.

  3. Within the jikesrvm source tree, count the number of files that have the extension .java. You may have to escape a wildcard with a ‘\’ character.

  4. Within the jikesrvm source tree, list all the files that contain the string “GCTrace”.

  5. Copy the file /r/uxpdworkshop/eaftan/jikesrvm/MMTk/build.xml into your working directory without using cp. You can do this with cat and I/O redirection.

  6. In the directory jikesrvm/MMTk/src/org/mmtk/plan, there are files called Phase.java and Phase2.java. What is the difference between these two files?

  7. In your working directory, create a symbolic link to jikesrvm/MMTk/src/org/mmtk/plan/SimplePhase.java. Using the symbolic link, view the file and list the authors of this file.

User Identity

Unix is a multi-user system: more than one user can be logged in at the same time.



groups


Each user can belong to one or more groups, you can see what groups you belong to with the command:
groups
Each file on the system is associated with one user (the file's user) and one group (the files group).

permissions


For each file, there are three sets of permissions, one for the owner, one for the members of the file's group, and one for every one else. Each set of permissions has three flags: read, write, and execute. For example, if the user's write flag is set, the user may write to that file.
Permissions may be manipulated with the command chmod (change mode).

Only the owner of a file may chmod that file.


For example, to give the a file's user write permission:
chmod u+w
Here, u indicates that we are changing the user permissions, the plus sign indicates we are adding capabilities, and the w indicates the write permission is the one being added. Instead of u, we could have used g for group, or o for others (ie. every one else on the system). The plus sign can be replaced with a minus to revoke permissions, and instead of w one may use r for read, or x for execute.
One can examine the permissions for files using the ls -l command, for example
ls -l testfile.txt
produces output like:
-rw-r--r-- 1 nricci01 students 8 Jul 13 2006 testfile.txt
The string '-rw-r--r--' shows the permissions, each position in the string representing a different permission. A - indicates the permission is not set, while a letter indicates it is. The first position is for the setuid bit (don't worry about it). The next three are for the read,write,excute permission of the user (nricci01) in this case, then for the group (students), and then for every one else. In this case, user nricc01 has read and write perissmion for the file, members of the group students have read permission, and every one else also has read permission. No one has execute permission on testfile.txt.

Job Control




Ctrl-Z


One can start a process and pause it, or let it it run in the background. To do this, first start any process. Then hit CTRL+Z to stop it.

bg

To make the process continue to run in the background, type bg. Or, to return it to the foreground, type fg.You can also make a process start in the background by ending its command like with &.



top

You can see a continuously updated list of running processes by running top.


Intially, top will display the top processes, sorted in desending order of processor usage, however, its display can be changed. Some more useful top options can be accesed by hitting the following keys (note: case sensitive):
u : display only processes own by the given user

M: Sort by memory usage

P: Sort by processor usage

k : kill the process with the given id

q: quit top
One can also see a non-updating list of processes using ps
By default, ps will produce a list of all processes the current user is running on the current terminal, but it has many options that let you select what processes you wish to see. One useful option is to show all processes owned by a particular user:
ps -U
Substitute in your own user name to see all processes running with your username.

Connecting to remote systems:

ssh and scp

One can use ssh (secure shell) to connect to remote machines in one of two ways:


ssh -l
or

ssh user@remote_host


scp (secure copy) can be used to transfer files to a remote machine:
scp user@host:remote_file

Exercise Set 2

For this exercise, you will download a file from the web, place it in your public_html directory, and then change the permissions so that it is viewable from the web.


1. Copy the file: /r/uxpdworkshop/nricci01/index.html into your home directory.
2. Move the file to the public_html subdirectory in your home directory. Any html files placed in the public_html directory will be accessible from the web at http://www.eecs.tufts.edu/~, but only if their permissions are set to be world readable.
3. Change the permissions to be world readable.
4. Check the address http://www.eecs.tufts.edu/~ to make sure the file is viewable from the web.

Shell


The shell is the primary interface between the user and the computer. Shells differ in their behavior and syntax, but the general ideas remain the same. Being comfortable in a shell environment is a key skill for a computer scientist.

Shell variables

Shell variables are available only to the shell. They affect the shell’s behavior (history length, auto-completion, etc.).


Environment variables

Environment variables are available to every program run by the shell and Contains information that other programs need (path, home directory, username, etc.).


Important Shell Variables

prompt


autolist

history


autologout

edit


time

Important Environment Variables

PATH


HOME

MAIL


USER

SHELL


MANPATH

LD_LIBRARY_PATH



Aliases


Aliases are shortcuts to commands. Saves having to re-type switches and flags. The shell interprets aliases only, so an alias in one shell does not mean anything to a separate shell.

Startup Scripts

Immediately after you enter your password, the system starts a shell for you. Startup scripts set the initial values of environment and shell variables. You can change the startup script to set up the shell the way you like it. The system startup scripts are /etc/csh.login, /etc/csh.cshrc.


Personal startup scripts are ~/.login and ~/.cshrc.

Examples





  1. Show all shell variables using set

  2. Show all environment variables using setenv

  3. Change the PATH variable

  4. Change the autolist variable

  5. Show the value of prompt, then change it




  1. Show current aliases using alias

  2. Change the ls alias

  3. Make a new alias for the string lls

  4. Make a new alias for the command rm



Example .cshrc

if (-f /local/etc/standard.cshrc ) then

source /local/etc/standard.cshrc

endif
# Put your own custom commands here --

# uncomment the following ones for some common abbreviations

# alias a alias

# alias h history

# alias m more

# alias e emacs

alias ls ls --color

unset autologout

set savehist=500

set autolist

setenv PATH ${HOME}/bin:/usr/dt/bin:${PATH}

setenv MOZ_PRINTER_NAME hp107

setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:${HOME}/lib

setenv MANPATH ${MANPATH}:/usr/X11R6/man


Examine System Defaults


Review /etc/csh.login

Review /etc/csh.cshrc


Exercise Set 3

Modify your .cshrc by:




  1. Changing your prompt

  2. Add an alias for rm

  3. Make sure autocomplete is set

  4. Anything else?

BEFORE YOU LOGOUT Make sure your .cshrc runs correctly, otherwise you MAY NOT BE ABLE TO LOG BACK IN




Emacs

"I use emacs, which might be thought of as a thermonuclear word processor. ... It is colossal, and yet it only edits straight ASCII text files, which is to say, no fonts, no boldface, no underlining. In other words, the engineer-hours that, in the case of Microsoft Word, were devoted to features like mail merge, and the ability to embed feature-length motion pictures in corporate memoranda, were, in the case of emacs, focused with maniacal intensity on the deceptively simple-seeming problem of editing text. ... emacs outshines all other editing software in approximately the same way that the noonday sun does the stars. It is not just bigger and brighter; it simply makes everything else vanish."



Emacs vs. xemacs

* Emacs comes in two flavors: emacs (within the terminal, no mouse, keyboard only, use this if you're SSHing) or xemacs (new window, mouse commands, etc.)

* X is the windowing system

* "client" and "server" are backwards: your machine is the server (because it provides window-displaying services) and the remote machine running xemacs is the client (sending data to your X windows.)

* You *can* run X on machines other than the Suns:

* Start X server command TK

* Open SSH x-forwarding command TK

* but right now we're going to use not-X emacs.




Saving and exiting:


* C-x C-c: Exit

* C-x s: Save without exit

* C-x C-S: Save without exit or prompt


Moving in emacs:


* Obvious: arrow keys

* Command keys: on the reference card:

* C-b and C-f move back or forward one char

* M-b and M-f move back or forward one "word"

* C-p and C-n move back or forward one line (back, next)

* C-a and C-e move to the beginning or end of the line you're on

! This works in the shell too!

* M-a and M-e move to the beginning or end of current paragraph

* C-M-a and C-M-e move to beginning of previous or next function !!

* Scrolling: C-v (next screen)

M-v (previous screen)

M-< (beginning of buffer/file)

M-> (end of buffer/file)

Search and Replace:


* Search forward: C-s

* Search backward: C-r (think "reverse")

* Regexp search: C-M-s and C-M-r (if you know regexps)

* "Select previous/next search string" - M-p or M-n (like "find next")

* C-g aborts search

* Replace: M-% puts you in an interactive replace mode.

You're prompted for a string to search for,

a string to replace with,

Then it finds them and asks you what to do.

: Space to replace

: Delete to skip

: ^ to back up

: ! to replace all remaining

: Return for exit



Multiple windows:


* Split a window: C-x 2 (above & below)

C-x 3 (side by side)

* Close all windows except current: C-x 1

* Close current window: C-x 0

* Switch to other window: C-x o

* Grow window height: C-x ^

* Grow window wider: C-x }

* Shrink window narrower: C-x {

* Multiple buffers:

* Select another buffer: C-x b

* List buffers: C-x C-b

* Kill a buffer: C-x k




Exercise Set 4:


Open /r/uxpdworkshop/pmorse01/exercise.txt and follow instructions.

.Emacs file


Formats your display

Modes for different types of files C, C++, Java, Perl, Latex, etc.

You can copy my .emacs file from /r/uxpdworkshop/urebbapr/.emacs into your home directory. Now quit out of emacs (if you already haven’t) and reopen it.

Undo


C-x u

Kill

Kill is used for deleting or cutting text

C-k cuts/deletes text from the cursor to the end of the lines

Saves text into the kill ring for pasting later




Yank

Pastes the text you just killed

Move the cursor to where you want to paste the text

C-y pastes the text where the cursor is



Kill Ring

Enables multiple cut and pastes!

C-y pastes the first thing in the kill ring

IMPORTANT: Without moving the cursor, M-y replaces the first thing with the second thing in the kill ring

Each additional M-y replaces what was just pasted with the next item in the kill ring

Deleting

C-d deletes a character

Does not save into the kill ring

Mark


Allows you to operate on a section of text within the current buffer.

You select this region of text by MARKING one end with C-sp or C-@, and moving the cursor to the other end.

The text in the middle is the REGION.

The cursor can be either before or after the mark. The region is simply what's in the middle.

The mark and region allows you to cut and paste portion of a line, or multiple lines at a time.



Killing a Region

Mark a region using C-sp or C-@ and moving the cursor to the end of the text

Cut the text with C-w

Paste out of the kill ring with C-y / M-y as described above.



Cool Kill/Delete Commands

M-d kill word

M-z char kill through next occurrence of char

C-x C-o deletes blank lines around current line




Mini-Buffer

Facility used by Emacs to read complicated arguments

Basic movement, killing, yanking works in the mini-buffer

C-x o will also toggle between the regular buffers and mini-buffer




M-x


Enables you to run commands by name

Cool Mini-buffer commands:


M-x goto-line

M-x replace-string

M-x replace-regexp

M-x compile



Exercise Set 5

Use auto-indent for this exercise:



M-C-\


  1. Open file /r/uxpdworkshop/urebbapr/workshop.c. It is a completely unformatted file and the main method is empty.

  2. Cut and paste lines 4-7 into the empty main procedure. That will allow the program to compile.

  3. Change the indenting of the file

  4. Compile using M-x compile and run.








База данных защищена авторским правом ©shkola.of.by 2016
звярнуцца да адміністрацыі

    Галоўная старонка