Resource Seminar 2007: Introduction to

Eun-seo Choi

Oct 11, 2007

1 Resources

• Senior grads

• System administrators ([email protected])

• Documents in /home/datalib/Resource Class

• Internet and magazines

– Google, Wikipedia, etc.

(http://www.linuxjournal.com/), (http://www. linux-mag.com/), (http://www.linuxformat.co.uk/), and many others (http://en.wikipedia.org/wiki/List_of_Linux_magazines).

– Source of the up-to-date information.

• http://tldp.org

– The Llinux Documentation Project.

– Guides and FAQs are excellent introductions.

• http://www.ibm.com/developerworks/linux

– Contains a vast collection of toturials on many Linux-related subjects.

– Go to “How do I get started with Linux?” (http://www.ibm.com/developerworks/ linux/newto/#6) to get started with linux.

1 2 Accessing a remote linux server

2.1 SSH ssh is a remote login program, meaning “secure shell”. ssh and the associated programs such as scp and sftp are the only allowed methods to remotely access division servers. There are many different ways of using ssh, but a real basic one is ssh -X -lusername server or ssh -X username @server For example, ssh -X -lces74 narsil.gps.caltech.edu or ssh -X [email protected] where -X option forwards X windows programs (running on the remote host) to a local dis- play. For more about ssh, type ‘man ssh’ on a terminal’s command line or visit http://www. openssh.org/manual.html. There are numerous on-line documents including the 3-part tu- torial on IBM DeveloperWorks (http://www.ibm.com/developerworks/library/l-keyc. html). Several ssh clients for MS Windows are available. Putty is quite popular (http://www. chiark.greenend.org.uk/~sgtatham/putty/).

2.2 X server on Microsft Windows

You might want to have an “X windows system” available on your MS Windows desktop or laptop. Then, you can keep working as if you locally logged in a linux server. Let’s imagine a situation where you are wokring on your MS Windows laptop at home but want to finish up your Ge161 homework using GMT wihtout going back to the office. Moreover, you left some partially working scripts in your home directory on the division’s linux system. You can log in the linux server through the normal terminal emulators and edit the GMT scripts. However, those terminal programs cannot pop up a window showing the EPS files you made. To see the results in this particular case or to run other X windows programs, you need an X server running on your local computer. You can set up an X windows system on MS Windows using Cygwin (http://www. cygwin.com/), Xming (http://www.straightrunning.com/XmingNotes/), or X-Win32 (http: //www.starnet.com/products/xwin32/). The first two are free; the latter is commercial but is freely downloadable from http://software.caltech.edu.

2 2.3 Virtual machines and dual booting

Instaling a linux system on your computer is an obvious way of getting a linux-compatible working environment. You have two options for installing linux while keeping your current non-linux OS. One is to partition your physical hard drive, install linux on one of the parti- tions, and use “dual booting”. Further information on dual booting is available on wikipedia (http://en.wikipedia.org/wiki/Dual_boot). The other is to install linux on a “virtual machine”. If interested, try out one of these: VMWare (commercial, http://www.vmware. com/), Parallel Workstation (commercial, http://www.parallels.com), Microsoft Virtual PC (free, http://www.microsoft.com/windowsxp/virtualpc/). Dual booting can be an- noying because you need to reboot everytime you want to switch to a different OS, while the virtual machine softwares suffer performance overhead. These techniques might be an overkill if you just want to work on linux servers.

3 Frequently used programs

For complete documentation of any of the following programs, try man command , or info command (‘q’ to quit while reading.)

3.1 Overview of file system

Excerpted from Wikipedia’s “Filesystem Hierarchy Standard” (http://en.wikipedia.org/ wiki/Filesystem_Hierarchy_Standard).

[ces74@elendil]/% cd [ces74@elendil]~% cd / [ces74@elendil]/% ls bin/ etc/ lib/ mnt/ proc/ scratch1@ sys/ var/ boot/ export/ lost+found/ net/ root/ selinux/ tmp/ dev/ home/ media/ opt/ sbin/ srv/ usr/

/ Primary hierarchy root and root directory of the entire file system hierarchy.

/bin/ Essential command binaries that need to be available in single user mode; for all users (e.g., cat, ls, cp).

/boot/ Boot loader files (e.g., kernels, initrd). Often a separate partition.

/dev/ Essential devices (e.g., /dev/null).

3 /etc/ Host-specific system-wide configuration files (the name comes from et cetera).

/etc/opt/ Configuration files for /opt/.

/etc/X11/ Configuration files for the X Window System, version 11.

/etc/sgml/ Configuration files for SGML.

/etc/xml/ Configuration files for XML.

/home/ Users’ home directories - containing saved files, personal settings etc. Often a sep- arate partition.

/lib/ Libraries essential for the binaries in /bin/ and /sbin/.

/media/ Mount points for removable media such as CD-ROMs (appeared in FHS-2.3).

/mnt/ Temporarily mounted filesystems.

/opt/ Optional application software packages.

/proc/ Virtual filesystem documenting kernel and process status as text files (e.g., uptime, network).

/root/ Home directory for the root user.

/sbin/ Essential system binaries (e.g., init, route, ifup).

/srv/ Site-specific data which is served by the system.

/tmp/ Temporary files (see also /var/tmp).

/usr/ Secondary hierarchy for user data; contains the majority of (multi-)user utilities and applications.

/usr/bin/ Non-essential command binaries (not needed in single user mode); for all users.

/usr/include/ Standard include files.

/usr/lib/ Libraries for the binaries in /usr/bin/ and /usr/sbin/.

/usr/sbin/ Non-essential system binaries (e.g. daemons for various network-services).

/usr/share/ Architecture-independent (shared) data.

/usr/src/ Source code (e.g. the kernel source code with its header files).

4 /usr/X11R6/ X Window System, Version 11 Release 6.

/usr/local/ Tertiary hierarchy for local data, specific to this host. Typically has fur- ther subfolders, e.g. bin/, lib/, share/.

/var/ Variable files, such as logs, spool files, and temporary e-mail files.

/var/lock/ Lock files. Files keeping track of resources currently in use.

/var/log/ Log files. Various logs.

/var/mail/ Users’ mail-boxes.

/var/run/ Information about the running system since last boot (e.g. currently logged- in users and running daemons).

/var/spool/ Spool for tasks waiting to be processed (e.g. print queues and unread mail).

/var/spool/mail/ Deprecated location for users’ mail-boxes.

/var/tmp/ Temporary files to be preserved between reboots.

3.2 Getting system information uname : Print system information. df : Report file system disk space usage. du : Estimate file space usage. top : Display Linux tasks. While running, hit ’u’ for a user id; ’k’ to kill a process; ’q’ to quit. w : Show who is logged on and what they are doing. finger : User information lookup program. date : Print or set the system date and time. time : Time a simple command or give resource usage.

echoi@echoi-laptop:~$ uname -a Linux echoi-laptop 2.6.20-16-generic #2 SMP Sun Sep 23 19:50:39 UTC 2007 i686 GNU/Linux

5 echoi@echoi-laptop:~$ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 38G 18G 19G 49% / varrun 1013M 144K 1013M 1% /var/run varlock 1013M 0 1013M 0% /var/lock procbususb 1013M 120K 1013M 1% /proc/bus/usb udev 1013M 120K 1013M 1% /dev devshm 1013M 0 1013M 0% /dev/shm lrm 1013M 15M 999M 2% /lib/modules/2.6.20-16-generic/volatile /dev/sda1 67G 42G 26G 62% /media/sda1 /dev/sda2 5.9G 5.1G 808M 87% /media/sda2 echoi@echoi-laptop:~$ du -sh 13G . echoi@echoi-laptop:~$ top top - 17:39:06 up 9:58, 5 users, load average: 0.15, 0.16, 0.14 Tasks: 140 total, 2 running, 137 sleeping, 0 stopped, 1 zombie Cpu(s): 0.0%us, 0.0%sy, 0.0%ni, 99.2%id, 0.0%wa, 0.0%hi, 0.8%si, 0.0%st Mem: 2074620k total, 970440k used, 1104180k free, 205656k buffers Swap: 489972k total, 19428k used, 470544k free, 136692k cached

PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 3606 echoi 15 0 2320 1184 880 R 1 0.1 0:00.04 top 1 root 15 0 2912 1808 488 S 0 0.1 0:01.79 init 2 root RT 0 0 0 0 S 0 0.0 0:00.00 migration/0 3 root 34 19 0 0 0 S 0 0.0 0:00.00 ksoftirqd/0 4 root RT 0 0 0 0 S 0 0.0 0:00.00 watchdog/0 8 root 10 -5 0 0 0 S 0 0.0 0:00.73 events/0 10 root 10 -5 0 0 0 S 0 0.0 0:00.00 khelper 11 root 10 -5 0 0 0 S 0 0.0 0:00.00 kthread 35 root 10 -5 0 0 0 S 0 0.0 0:00.00 kblockd/0 37 root 10 -5 0 0 0 S 0 0.0 0:01.44 kacpid

[ces74@narsil]~% w 17:40:08 up 3 days, 1:40, 4 users, load average: 0.00, 0.00, 0.00 USER TTY FROM LOGIN@ IDLE JCPU PCPU WHAT danb pts/0 rockpile Wed15 25:04m 0.69s 0.69s -tcsh carltape pts/1 denali Tue08 2days 0.03s 0.03s -bash

6 ces74 pts/2 dhcp-65-221 17:40 0.00s 0.37s 0.00s w carltape pts/4 denali 06:53 10:45m 0.02s 0.02s -bash

[ces74@narsil]~% finger danb Login: danb Name: Daniel Bower Directory: /home/danb Shell: /bin/tcsh Office: 358 South Mudd On since Wed Oct 3 15:56 (PDT) on pts/0 from rockpile 1 day 1 hour idle No mail. No Plan.

bash-3.1$ date Sat Oct 6 19:35:42 PDT 2007

bash-3.1$ cat > myprogram #!/bin/bash

find . -maxdepth 2 -name ’.csh*’

bash-3.1$ chmod +x myprogram

bash-3.1$ time ./myprogram ./.cshrc% ./.cshrc

real 0m1.907s user 0m0.005s sys 0m0.022s

3.3 Navigating file system ls : List directory contents. cd : Change to another directory. cd - : Go back to the previous directory. find : Search for files in a directory hierarchy by file name, modified time, accessed time, permission, etc. pwd : Print name of current/working directory.

7 cd : Change to another directory. which : Locate a command.

echoi@echoi-laptop:~$ ls -tl 합계 232 -rw-r--r-- 1 echoi echoi 7 2007-10-04 17:16 cron.log drwxr-xr-x 5 echoi echoi 4096 2007-09-30 20:00 Desktop -rw------1 echoi echoi 8155 2007-09-20 15:31 walkthrough_detailed_weak.svg drwxr-xr-x 3 echoi echoi 4096 2007-08-29 17:21 data drwxr-xr-x 2 echoi echoi 4096 2007-08-27 14:21 roms drwxr-xr-x 2 echoi echoi 4096 2007-07-02 23:27 Pictures drwx------2 echoi echoi 4096 2007-06-28 15:50 amsn_received -rw-r--r-- 1 echoi echoi 87423 2007-06-06 10:06 latest_address.ldif lrwxrwxrwx 1 echoi echoi 26 2007-04-24 06:01 Examples -> /usr/share/example-content

echoi@echoi-laptop:~$ pwd /home/echoi echoi@echoi-laptop:~$ cd opt/stgSnac/Snac/ echoi@echoi-laptop:~/opt/stgSnac/Snac$ ls ~- echoi@echoi-laptop:~/opt/stgSnac/Snac$ echo ~- /home/echoi echoi@echoi-laptop:~/opt/stgSnac/Snac$ echo ~+ /home/echoi/opt/stgSnac/Snac echoi@echoi-laptop:~/opt/stgSnac/Snac$ cd - /home/echoi echoi@echoi-laptop:~$

echoi@echoi-laptop:~/opt$ find ~/opt -name ’deal.II’ /home/echoi/opt/deal.II /home/echoi/opt/deal.II/deal.II

echoi@echoi-laptop:~/opt$ find . -mmin -60 . ./test.sh

echoi@echoi-laptop:~$ find . -size +1G ./opt/Snac_new/tests/hydroPressure.0 ./opt/deal.II.projects/vishunu.tar.bz2

echoi@echoi-laptop:~$ which paraview

8 /home/echoi/opt/paraview-3.0.2-Linux-x86/bin/paraview

3.4 Manipulating files and directories cp : Copy files or directories. rm : Remove files or directories. mv : Rename or move files or directories to another location. ln -s : Create a symbolic link to an existing file or directory. mkdir : Make a directory. rmdir : Remove a directory. touch : Change file timestamps. Create file if it does not exist yet.

3.5 Acess permission and mode

Every file or folder in Linux has access permissions. There are three types of permissions (what allowed to do with a file):

• read access

• write access

• execute access

Permissions are defined for three types of users:

• user (owner).

• group that the owner belongs to.

• other user.

A“mode” is one of the possible combinations of permissions and users. These commands are used to change the mode and the ownership of files and directories. chmod : Change the mode. chown : Change the owner.

9 chgrp : Change the group. ls -l : Print long list including permission.

The following examples are taken from http://www.tldp.org/LDP/intro-linux/html/ sect_03_04.html#sect_03_04_01 .

asim:~> ./hello bash: ./hello: bad interpreter: Permission denied

asim:~> cat hello #!/bin/bash echo "Hello, World"

asim:~> ls -l hello -rw-rw-r-- 1 asim asim 32 Jan 15 16:29 hello

asim:~> chmod u+x hello

asim:~> ./hello Hello, World

asim:~> ls -l hello -rwxrw-r-- 1 asim asim 32 Jan 15 16:29 hello*

asim:~> chmod u+rwx,go-rwx hello

asim:~> ls -l hello -rwx------1 asim asim 32 Jan 15 16:29 hello*

3.6 Viewing and manipulating contents of files cat : Write each FILE to standard output, first line first. tac : Write each FILE to standard output, last line first. less : Displays text one screen at a time. Similar but superior to more. ’q’ to quit. wc : Print the number of newlines, words, and bytes in files. paste : Write lines consisting of the sequentially corresponding lines from each file, sepa- rated by TABs, to standard output.

10 →paste puts together two or more files column by column; cat puts together files one after another. cut : Remove sections from each line of files. head : Output the first part of files. tail : Output the last part of files.

3.7 Manipulating text files with awk and sed sed : Strem editor. Read a file line by line, manipulate strings in the line, and print them to standard output. awk : Pattern scanning and processing language. It is a programming language.

Examples (from /home/datalib/Resource Class/Resource Class 2005/UNIXtips.txt by Kevin Lewis):

> ls U*.txt UNIXtips.txt > ls U*.txt | sed ’s/txt/doc/g’ UNIXtips.doc > cat UNIXtips.txt | sed ’s/UNIX/Windows/g’ > Windowstips.txt > cat UNIXtips.txt | sed ’s/\ //g’ > Unixtips_spacedeleted.txt

> seq -s\ 4 1 2 3 4 > seq -s\ 4 | awk ’{print $1","$2","$3","$4}’ 1,2,3,4 > seq -s\ 4 | awk ’{print $1*10","$2/10","cos($3)","$4-7}’ 10,0.2,-0.989992,-3

3.8 Managing processes ps : Report a snapshot of the current processes. kill : Send a signal to a process (to kill it). Ctrl-c for killing a running process. nice : Run a program with modified scheduling priority.

11 top : See Section 3.2.

echoi@echoi-laptop:~/opt$ ps PID TTY TIME CMD 1331 pts/2 00:00:00 bash 2863 pts/2 00:00:00 ps echoi@echoi-laptop:~/opt$ ps uxwf | grep emacs echoi 944 0.0 0.4 13808 9632 pts/1 T 16:22 0:00 | | \_ emacs echoi 957 0.0 0.4 13796 9616 pts/1 T 16:22 0:00 | | \_ emacs echoi 2867 0.0 0.0 2988 760 pts/2 S+ 17:18 0:00 | | \_ grep emacs echoi@echoi-laptop:~/opt$ kill -9 957 944 echoi@echoi-laptop:~/opt$ nice -19 myBigProgram

3.9 Printing

Printing in modern linux systems is as easy as, if not easier than, in MS Windows. In case you still need to send files to printers on command line, use these programs: lp : Print a file. lp -d 366sm hp 2side -n 2 file1 Print 2 copies of le1 to 366sm hp 2side. lpstat : Check the print queue. lpstat -p 366sm hp 2side Check the queue of 366sm hp 2side cancel : Cancel a print job. cancel 366sm hp 2side-36 Cancel job number 36 on 366sm hp 2side. cancel -u username : Cancel all your jobs on all the printers. enscript : Convert a text file to a postscript file. enscript -2 -r file1 Prints file1 with 2 columns and in landscape mode. enscript -2 -r file1 -o tmp.ps Prints file1 to tmp.ps with 2 columns and in land- scape mode.

3.10 Archiving and Compression/Uncompression

• Creating an archive file (.tar) from obj/ directory: tar -cvf obj.tar obj/

12 • Creating an archive file with compression: tar -czvf obj.tar.gz obj/ compressing with gzip tar -cjvf obj.tar.bz2 obj/ compressing with bzip2

• Extracting (and uncompressing) an archive file: tar -xvf obj.tar , tar -xzvf obj.tar.gz , tar -xjvf obj.tar.bz2

• Viewing the contents of an archive file: tar -tvf obj.tar , tar -tzvf obj.tar.gz , tar -tjvf obj.tar.bz2

4 bash Basics

To learn about bash in a systematic fashion, it is really recommended to read “Bash Ref- erence Manual” (http://www.gnu.org/software/bash/manual/bashref.html). The following is a short list of things that are used in the examples of this document but might look mysterious at first:

. : Current directory. cp test/test.txt . As a command, equal to source (read and execute). . .bashrc = source .bashrc

.. : Upper directory by one level. To represent multi-lelvel, ../.., ../../.., etc. cd .. , mv test.txt ../../../test dir

~ : Equal to /home/username . cp /test.txt~ /test2.txt~

; : Commands separated by ; are executed sequentially. cp /test.txt~ /test2.txt;~ rm /test.txt

& : When put after a command, the process runs in the background. emacs & = emacs → Ctrl+z → bg

| : When a sequence of commands are separated by ’|’, the standard output of a command becomes the standard input for the next command. find ~ -name ’.*’ | grep bash

${} : Variable expansion. echo ${PATH} , cd ${GMTHOME}

13 ‘‘ (backquotes) or $() : Command substitution.

echoi@echoi-laptop:~$ ls -l $(which python) lrwxrwxrwx 1 root root 9 2007-05-31 11:05 /usr/bin/python -> python2.5 echoi@echoi-laptop:~$ ls -l ‘which python‘ lrwxrwxrwx 1 root root 9 2007-05-31 11:05 /usr/bin/python -> python2.5

4.1 Redirection

Redirecting outputs can be useful when you want to save or throw away texts printed on command line by shell commands or programs. Texts printed on command line have the type of either stdout or stderr. These are two of the three standard “file descriptor” (see http://en.wikipedia.org/wiki/File_descriptor). One of the differences between stdout and stderr is that outputs sent to stdout are “buffered” before acutally shown while outputs sent to stderr are displayed without delay. Integer value of 1 and 2 are assigned to stdout and stderr. The following has been excerpted from “BASH Programming - Introduction HOW-TO” (http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-3.html).

1. stdout to file This will cause the ouput of a program to be written to a file. Note that use of ’>’ will overwrite ls-l.txt if it already exists. To append, use ‘>>’. to create or overwrite, ls -l > ls-l.txt to append, ls -l >> ls-l.txt

2. stderr to file This will cause the stderr ouput of a program to be written to a file. to create or overwrite, grep da * 2> grep-errors.txt to append, grep da * 2>> grep-errors.txt

3. stdout to stderr This will cause the stderr ouput of a program to be written to the same filedescriptor than stdout. grep da * 1>&2

4. stderr to stdout This will cause the stderr ouput of a program to be written to the same filedescriptor

14 than stdout. grep * 2>&1

5. stderr and stdout to file This will place every output of a program to a file. rm -f $(find / -name core) &> removedcore.log This will throw away all the outputs. rm -f $(find / -name core) &> /dev/null

4.2 Globbing

Globbing is another name for ”filename expansion”. The next examples are from ”Advanced Bash-Scripting Guide” by Medel Cooper (http://www.faqs.org/docs/abs/HTML/globbingref. htm).

bash$ ls -l total 2 -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1 -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1 -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1 -rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh -rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt

bash$ ls -l t?.sh -rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh

bash$ ls -l [ab]* -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1 -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1

bash$ ls -l [a-c]* -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 a.1 -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1 -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1

bash$ ls -l [^ab]* -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1 -rw-rw-r-- 1 bozo bozo 466 Aug 6 17:48 t2.sh -rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt

15 bash$ ls -l {b*,c*,*est*} -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 b.1 -rw-rw-r-- 1 bozo bozo 0 Aug 6 18:42 c.1 -rw-rw-r-- 1 bozo bozo 758 Jul 30 09:02 test1.txt

bash$ echo * a.1 b.1 c.1 t2.sh test1.txt

bash$ echo t* t2.sh test1.txt

4.3 Shell programming

Good readings to learn bash programming would be ”Bash guide for beginners” (http:// www.tldp.org/LDP/Bash-Beginners-Guide/html/index.html) and ”Advanced bash-scripting guide” (http://www.tldp.org/LDP/abs/html/index.html) for more advanced techniques. The example script given below shows how to use a few important and frequently used features such as processing command line arguments, using a for loop, and command sub- stitution. Condition test using if/then is also very useful (see the example in Section 4.5). To run this script, save the boxed code as rename.sh and run ’chmod u+x rename.sh’. Then, you can run it like a regular program. An easy way of making a text file on command line is using cat. For example, ’cat > rename.sh’ → ’copy and paste or type the code’ → ’Ctrl-d’.

#!/bin/bash # rename.sh # Basic file renamer

criteria=$1 re_match=$2 replace=$3

for i in $( ls *$criteria* ); do src=$i tgt=$(echo $i | sed -e "s/$re_match/$replace/")

16 mv $src $tgt done

4.4 Hierarchy of setup files

/etc/profile ~/.bash_profile ~/.profile

The above files, if exist, are “sourced” (read and executed) in this order when you log in. They seldom need touching.

/etc/bashrc ~/.bashrc

These files are sourced whenever you run a new (non-login) interactive shell. You do not have permission to modify the system files in /etc. So, all your personal settings such as aliases and environment variables should be put in ${HOME}/.bashrc

4.5 Setting up user environment

You can either put everything in a single, ${HOME}/.bashrc, or divide commands into two (or more) separate files. To take the latter approach, add the code in the box below whatever is in your current ${HOME}/.bashrc:

if [ -f ~/.envvars ]; then . ~/.envvars fi

if [ -f ~/.aliases ]; then . ~/.aliases fi

This style is just a suggestion, but I personally find it convenient because setting a per- sonal environment is usually composed of two things: Defining aliases and setting environ- ment variables.

17 Now, if you create two files, ${HOME}/.envvars and ${HOME}/.aliases, they will be sourced everytime you start a new shell. Nothing breaks even if you don’t. In case you al- ready have many environment-setting commands in the current ${HOME}/.bashrc, you can simply move all the ’alias ...’ commands to ${HOME}/.aliases and all the ’export ...’ to ${HOME}/.envvars. Be careful to group related commands together. The next section will clarify what it means.

Setting environment variables

Contents of ${HOME}/.envvars would look like the following:

# GMT export GMTHOME=/opt/GMT export PATH=${GMTHOME}/bin:${PATH}

# Matlab export MATLABDIR=/opt/matlab export PATH=${MATLABDIR}/bin:${PATH}

# mpich export CXX=g++ export MPI_DIR=${HOME}/opt/mpich-1.2.7p1 export MPI_BINDIR=${MPI_DIR}/bin export MPI_INCDIR=${MPI_DIR}/include export MPI_LIBDIR=${MPI_DIR}/lib export PATH=${MPI_BINDIR}:${PATH} export LD_LIBRATY_PATH=${MPI_LIBDIR}:${LD_LIBRATY_PATH}

The environment variables should be “exported” using export, a shell command, so that all the programs run from the shell can “see” them.. The most important variables are PATH and LD LIBRARY PATH. PATH contains directories where executables you want to run reside. By maintaining this variable properly, you can run programs without remembering where they are or typing the whole paths to them ev- ery time. LD LIBRARY PATH contains directories which a compiler automatically looks into to find libararies required for building an application. This becomes really important when you write a big program that depends on codes someone else wrote. These environment variables have a default values when you log in. For example, PATH usually include /usr/bin during the login process. That is why you can run commands in

18 /usr/bin like ls, cd, or rm, without adding it to PATH manually. To add new directories to these variables, you should follow the syntax of export command given in the above example so as not to overwrite those default values. The same for LD LIBRARY PATH. Some programs require to export their own set of variables. For example, SAC requires do the following:

SACAUX = /foo/sac/aux export SACAUX

So, refer to the program’s documentation when thigns do not work as expected.

Defining aliases

Defining aliases is straight foward. Just follow the syntax in the below example. Note that “unalias” command is available to temporarily disable an alias.

echoi@echoi-laptop:~$ cat /home/echoi/.aliases alias ll=’ls -l’ alias la=’ls -A’ alias l=’ls -CF’ alias cp=’cp -i’ alias mv=’mv -i’ alias rm=’rm -i’ alias em=’xemacs’ alias pangu=’\ssh -XA -lces74 pangu.gps.caltech.edu’ alias alfred=’\ssh -XA -lces74 alfred.gps.caltech.edu’

5 Text editors

Whatever your choice is, it is critical for efficient editing to get used to keyboard shorcuts (see the attached reference cards.)

5.1 vim

Light and fast. Once you get used to its peculiar “mode” concept, any editing job can be done very efficiently. Best on command line.

19 5.2 Emacs/XEmacs

Powerful in a sense that they are IDE (Integrated Development Environment). In other words, you can compile, run, and debug programs without going back to a terminal.

5.3 gedit/Kate

Similar to Wordpad of MS Windows, but can do a lot more. Not IDEs, but provide good support for programming.

6 Other useful applications

Inkscape : SVG (a vector graphics format) editor. Good for drawing cartoons without wor- rying about resolution. Similar to Adobe Illustrator. Cannot read but can write EPS files.

ImageMagick, Gimp : Editing images and converting their format.

Thunderbird, KMail, Evolution : Desktop email clients.

OpenOffice : An alternative to MS Office. Can make PDFs of good quality.

LATEX : Can quickly make equation-laden documents with pre-defined formats. Slides and posters, too.

20 XEmacs Reference Card Motion Multiple Windows (for version 21.0+) entity to move over backward forward delete all other windows C-x 1 character C-b C-f delete this window C-x 0 Starting Emacs word M-b M-f split window in two vertically C-x 2 line C-p C-n split window in two horizontally C-x 3 To enter XEmacs, just type its name: xemacs go to line beginning (or end) C-a C-e scroll other window C-M-v To read in a file to edit, see Files, below. sentence M-a M-e switch cursor to another window C-x o paragraph M-{ M-} shrink window shorter M-x shrink-window page C-x [ C-x ] grow window taller C-x ^ Leaving Emacs sexp C-M-b C-M-f shrink window narrower C-x { function C-M-a C-M-e grow window wider C-x } suspend Emacs (or iconify frame under X) C-z go to buffer beginning (or end) M-< M-> exit Emacs permanently C-x C-c select buffer in other window C-x4b scroll to next screen C-v display buffer in other window C-x 4 C-o scroll to previous screen M-v find file in other window C-x4f Files scroll left C-x < find file read-only in other window C-x4r scroll right C-x > run Dired in other window C-x4d read a file into Emacs C-x C-f scroll current line to center of screen C-u C-l find tag in other window C-x4. save a file back to disk C-x C-s save all files C-x s Killing and Deleting Formatting insert contents of another file into this buffer C-x i replace this file with the file you really want C-x C-v indent current line (mode-dependent) TAB entity to kill backward forward write buffer to a specified file C-x C-w indent region (mode-dependent) C-M-\ character (delete, not kill) DEL C-d indent sexp (mode-dependent) C-M-q word M-DEL M-d indent region rigidly arg columns C-x TAB Getting Help line (to end of) M-0 C-k C-k sentence C-x DEL M-k insert newline after point C-o The Help system is simple. Type C-h and follow the directions. sexp M-- C-M-k C-M-k move rest of line vertically down C-M-o If you are a first-time user, type C-h t for a tutorial. delete blank lines around point C-x C-o kill region C-w join line with previous (with arg, next) M-^ quit Help window q copy region to kill ring M-w delete all white space around point M-\ scroll Help window space kill through next occurrence of char M-z char put exactly one space at point M-SPC apropos: show commands matching a string C-h a yank back last thing killed C-y fill paragraph M-q show the function a key runs C-h c replace last yank with previous kill M-y set fill column C-x f describe a function C-h f set prefix each line starts with C-x . get mode-specific information C-h m Marking Case Change Error Recovery set mark here or C-@ C-SPC uppercase word M-u exchange point and mark C-x C-x abort partially typed or executing command C-g lowercase word M-l words recover a file lost by a system crash M-x recover-file set mark arg away M-@ capitalize word M-c paragraph recover files from a previous Emacs sessionM-x recover-session mark M-h uppercase region C-x C-u mark page undo an unwanted change C-x u or C-_ C-x C-p lowercase region C-x C-l sexp restore a buffer to its original contents M-x revert-buffer mark C-M-@ capitalize region M-x capitalize-region function redraw garbaged screen C-l mark C-M-h mark entire buffer C-x h The Minibuffer Incremental Search Query Replace The following keys are defined in the minibuffer. search forward C-s complete as much as possible TAB search backward C-r interactively replace a text string M-% complete up to one word SPC regular expression search C-M-s using regular expressions M-x query-replace-regexp complete and execute RET reverse regular expression search C-M-r Valid responses in query-replace mode are show possible completions ? fetch previous minibuffer input M-p select previous search string M-p replace this one, go on to next SPC or y fetch next later minibuffer input M-n select next later search string M-n replace this one, don’t move , regexp search backward through history M-r exit incremental search RET skip to next without replacing DEL or n regexp search forward through history M-s undo effect of last character DEL replace all remaining matches ! abort command C-g abort current search C-g back up to the previous match ^ Type C-x ESC ESC to edit and repeat the last command that exit query-replace ESC Use C-s or C-r again to repeat the search in either direction. used the minibuffer. The following keys are then defined. If Emacs is still searching, C-g cancels only the part not done. enter recursive edit (C-M-c to exit) C-r delete match and enter recursive edit C-w previous minibuffer command M-p c 1998 Foundation, Inc. Permissions on back. v2.0 XEmacs next minibuffer command M-n 1 2 3 XEmacs Reference Card Regular Expressions Keyboard Macros

any single character except a newline . (dot) start defining a keyboard macro C-x ( Buffers zero or more repeats * end keyboard macro definition C-x ) one or more repeats + execute last-definedkeyboardmacro C-x e select another buffer C-x b zero or one repeat ? edit keyboard macro C-x C-k list all buffers C-x C-b any character in the set [ ... ] append to last keyboard macro C-u C-x ( kill a buffer C-x k any character not in the set [^ ... ] name last keyboard macro M-x name-last-kbd-macro beginning of line ^ insert Lisp definition in buffer M-x insert-kbd-macro Transp osing end of line $ quote a special character c \c alternative (“or”) \| Commands Dealing with Emacs Lisp characters transpose C-t grouping \( ... \) transpose words M-t nth group \n eval sexp before point C-x C-e transpose lines C-x C-t beginning of buffer \‘ eval current defun C-M-x sexps transpose C-M-t end of buffer \’ eval region M-x eval-region word break \b eval entire buffer M-x eval-current-buffer Spelling Check not beginning or end of word \B read and eval minibuffer M-ESC beginning of word \< re-execute last minibuffer command C-x ESC ESC end of word \> read and eval Emacs Lisp file M-x load-file check spelling of current word M-$ any word-syntax character \w load from standard system directory M-x load-library check spelling of all words in region M-x ispell-region any non-word-syntax character \W check spelling of entire buffer M-x ispell-buffer character with syntax c \sc character with syntax not c \Sc Simple Customization Tags Here are some examples of binding global keys in Emacs Lisp. Registers find a tag (a definition) M-. (global-set-key [(control c) g] ’goto-line) find next occurrence of tag C-u M-. (global-set-key [(control x) (control k)] ’kill-region) save region in register C-xrs specify a new tags file M-x visit-tags-table (global-set-key [(meta #)] ’query-replace-regexp) insert register contents into buffer C-xri regexp search on all files in tags table M-x tags-search An example of setting a variable in Emacs Lisp: save value of point in register C-xrSPC run query-replace on all the files M-x tags-query-replace jump to point saved in register C-xrj (setq backup-by-copying-when-linked t) continue last tags search or query-replace M-, Writing Commands Shells Info enter the Info documentation reader C-h i (defun command-name (args) execute a shell command M-! "documentation" run a shell command on the region M-| Moving within a node: (interactive "template") filter region through a shell command C-u M-| scroll forward SPC body ) start a shell in window *shell* M-x shell scroll reverse DEL An example: beginning of node . (dot) (defun this-line-to-top-of-window (line) Rectangles Moving between nodes: "Reposition line point is on to top of window. next node n With ARG, put point on line ARG. copy rectangle to register C-xrr previous node p Negative counts from bottom." kill rectangle C-xrk move up u (interactive "P") yank rectangle C-xry select menu item by name m (recenter (if (null line) open rectangle, shifting text right C-xro select nth menu item by number (1–5) n 0 blank out rectangle M-x clear-rectangle follow cross reference (return with l) f (prefix-numeric-value line)))) prefix each line with a string M-x string-rectangle return to last node you saw l select rectangle with mouse M-button1 The argument to interactive is a string specifying how to get return to directory node d the arguments when the function is called interactively. Type gotoanynodebyname g C-h f interactive for more information. Abbrevs Other: Copyright c 1998 Free Software Foundation, Inc. run Info tutorial h designed by Stephen Gildea, April 1998 v2.0 XEmacs add global abbrev C-xag list Info commands ? for GNU Emacs version 19 on Unix systems add mode-local abbrev C-xal quit Info q Updated for XEmacs in February 1995 by Ben Wing add global expansion for this abbrev C-xaig search nodes for regexp s Permission is granted to make and distribute copies of this card pro- add mode-local expansion for this abbrev C-xail vided the copyright notice and this permission notice are preserved on explicitly expand abbrev C-xae all copies. expand previous word dynamically M-/ For copies of the GNU Emacs manual, write to the Free Software Foun- dation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 4 5 6 Vi Reference Card Yanking text Regular Expressions Like deletion, almost all yank commands are performed by any single character except newline . (dot) typing y followed by a motion. For example y$ yanks to zero or more repeats * Modes the end of line. Two other yank commands are: any character in set [...] Vi has two modes: insertion mode, and command mode. any character not in set [^ ...] line yy The editor begins in command mode, where cursor move- beginning, end of line ^ , $ line :y ment and text deletion and pasting occur. Insertion mode beginning, end of word < , > begins upon entering an insertion or change command. grouping \ (...\ ) [ESC] returns the editor to command mode (where you can Changing text contents of n th grouping \ \n quit, for example by typing :q!). Most commands execute The change command is a deletion command that leaves \ as soon as you type them except for “colon” commands the editor in insert mode. It is performed by typing c fol- Counts which execute when you press the return key. lowed by a motion. For example cw changes a word. A few Nearly every command may be preceded by a number that other change commands are: specifies how many times it is to be performed. For exam- Quitting to end of line C ple 5dw will delete 5 words and 3fe will move the cursor exit, saving changes :x line cc forward to the 3rd occurance of the letter e. Even inser- quit (unless changes) :q tions may be repeated conveniently with this method, say quit (force, even if unsaved) :q! Putting text to insert the same line 100 times. put after position or after line p Inserting text put before position or before line P Ranges insert before cursor, before line i , I Ranges may precede most “colon” commands and cause append after cursor, after line a , A Buffers them to be executed on a line or lines. For example :3,7d open new line after, line before o , O Named buffers may be specified before any deletion, would delete lines 3 7. Ranges are commonly combined replace one char, many chars r , R change, yank, or put command. The general prefix has with the :s command− to perform a replacement on several the form "c where c may be any lower case letter. For lines, as with :.,$s/pattern/string/g to make a replace- Motion example, "adw deletes a word into buffer a. It may there- ment from the current line to the end of the file. left, down, up, right h , j , k , l after be put back into the text with an appropriate put lines n-m :n ,m next word, blank delimited word w , W command, for example "ap. beginning of word, of blank delimited word b , B current line :. end of word, of blank delimited word e , E last line :$ Markers marker c :’c sentence back, forward (,) Named markers may be set on any line of a file. Any lower paragraph back, forward , all lines :% { } case letter may be a marker name. Markers may also be all matching lines :g/pattern / beginning, end of line 0 , $ used as the limits for ranges. beginning, end of file 1G , G line n n G or :n set marker c on this line mc Files forward, back to char c fc ,Fc goto marker c ‘c write file (current file if no name given) :w file top, middle, bottom of screen H,M,L goto marker c first non-blank ’c read file after line :r file next file :n Deleting text Search for Strings previous file :p Almost all deletion commands are performed by typing d search forward /string edit file :e file followed by a motion. For example dw deletes a word. A search backward ?string replace line with program output !!program few other deletions are: repeat search in same, reverse direction n , N Other character to right, left x , X Replace toggle upper/lower case ~ to end of line D The search and replace function is accomplished with the join lines J line dd :s command. It is commonly used in combination with repeat last text-changing command . line :d ranges or the :g command (below). undo last change, all changes on line u , U replace pattern with string :s/pattern /string /flags flags: all on each line, confirm each g , c repeat last :s command &