Introduction to the basics of UNIX Joachim Hein Centre for Mathematical Sciences Lund University Outline 1 Operating system UNIX 2 Interacting with UNIX Manipulating files and directories Editing files: emacs Basic scripting UNIX variables 3 Summary Introduction to UNIX UNIX and UNIX-like operating systems widely for scientific computation platforms Started in 1969 at AT&T Bell Labs Large number of UNIX flavours, including: Various flavours of LINUX AIX by IBM Solaris by Oracle (formerly Sun) Mac OS X by Apple HP-UX by HP To the user of a system the difference between these flavours of UNIX are typically small This lecture: Basic introduction into using UNIX
The UNIX terminal Traditional interaction with a UNIX system: Prompt in terminal Graphical user interfaces are modern alternative, but Not everything you need to do can be done via gui Prompt available on any UNIX system Prompt faster to access a remote computer (e.g. in the US) Just type the commands at the prompt Rem: There is also a DOS prompt on a windows system UNIX command A typical UNIX command looks as follows: command arg1 arg2 arg3 Arguments can be optional or mandatory Examples for UNIX commands: ls mkdir src grep -i input io.c gcc -O3 -c solver.c Getting help on a command To get help on a UNIX command, study the man-pages Example: man gcc Produces the manual page for the gcc command
Directory structure The directory structure of a user fred might look like: fred Simax Epen src obj README doc comp.c io.c simax.c comp.o io.o simax.o ch1.txt ch2.txt comp.h io.h : file The home directory of fred contains: directories and/or files - the directories contain: sub-directories and/or files : directory Basic UNIX usage requires manipulation of your directories and files Listing files and directories To list the files and directories in your present directory, simply type ls There are a number of important options to ls -l Long listing, incl. owner, size, dates, time -t Sort by time -r Reverse the order of the listing -a List all objects objects starting with. are hidden You can also given a file/directory as argument to ls: ls -l input.c Gives the details of the file input.c Example listing Example output for a long time reversed listing on a Linux system: guldin:~/courses/scicompmath/slides> ls -ltr total 55 -rw-r--r-- 1 joachim matte 168 Jun 2 14:40 slidetemplate.tex -rw-r--r-- 1 joachim matte 5025 Jun 3 16:25 LU.JPG -rw-r--r-- 1 joachim matte 48096 Jun 3 16:25 logoonly.pdf drwxr-xr-x 2 joachim matte 512 Jun 3 16:25 CVS/ drwxr-xr-x 4 joachim matte 512 Jun 3 19:37 IntroUnix/ Meaning of key columns: ---------- ------- ----- ----- ------------ ----------------- permission owner group size modif. time object name
Navigating the directory tree Use cd to change the directory Examples: cd src change into the sub-directory: src cd testcode/src change into the sub-sub-directory: testcode/src cd.. move into the parent directory (one level up) cd change in your home directory cd /usr/bin/latex change into the directory: /usr/bin/latex Rem: Leading / gives absolute path, otherwise relative Creating and removing directories mkdir mydir creates a sub-directory mydir in the present directory rmdir olddir removes the sub-directory olddir (needs to be empty) from the present directory Copying and deleting files Use cp to copy a file Different location: cp results.txt Textfiles/. Copies the file results.txt from present directory into the subidrectory Textfiles and keeps the name Different name: cp results.txt results_old.txt Different name in different directory: cp results.txt Archieve/results_old.txt Use rm to delete (remove) a file: Deleting the file oldfile.c rm oldfile.c Depending on setup, this might remove the file without asking any questions!!!
Viewing files One can view the contents of a text file (e.g. listing.txt, program.c, routine.h) with more more program.c Compare two files using diff diff filea.c fileb.c Sample output from diff showing line 13 from first and second file: 13c13 < double result = sq(avec[0]); --- > double result = scalarprod(avec,bvec); Editing files To write and modify C-code you need a text editor There are many text editors available for UNIX vi this is very popular with system administrators pico/nano a simple, easy to use editor emacs extremely powerful many more Use what works best for you you need the job done Next: a quick introduction to emacs Text editor: emacs Emacs has special modes for different types of files Typically switched automatically based on extension Modes include: C-source, Fortran-source, L A TEX-source, UNIX-scripts Emacs is easiest when using the graphical window You normally just type in the main window Full mouse support (change of writing postion, buttons, pull down menues,...) On a remote machine, the graphical window might not open (e.g. no X-connection) or might be slow Run emacs in your terminal window Typically faster Emacs in your terminal can be forced by calling emacs -nw Downside: reduced functionallity
Starting emacs Depending on the installation, emacs can often be started from the graphical user interface of the UNIX system Emacs can also be started from the UNIX prompt Starting emacs form terminal to run in graphical window: emacs programsource.c & Starts emacs and loads file programsource.c into it The & ensures the prompt is not blocked, to e.g. compile and run programs Starting emacs from terminal to run in terminal: emacs -nw programsource.c Starts emacs and loads file programsource.c into it The -nw forces the terminal mode The EMACS graphical window EMACS in a terminal
Controlling Emacs Just type your text (e.g. C-code) in the main window Emacs can be controlled for special action (e.g. storing, reading, finishing,...) Mouse clicks (button, pull down) Writing command in the last line of the window (M-x to get there) Keyboard shortcuts (very fast for things you use regular) Resources Build in tutorials and build in help Reference card for shortcuts (M denotes ESC and C denotes ctrl)