1 joe meehean. log in do basic stuff use the file system access my files from windows edit...

58
Linux 1 Joe Meehean

Upload: michael-ellis

Post on 14-Dec-2015

220 views

Category:

Documents


2 download

TRANSCRIPT

  • Slide 1

1 Joe Meehean Slide 2 Log in Do basic stuff Use the file system Access my files from Windows Edit files Do C++ development Hand in my programs 2 Slide 3 Linux is wide spread smartphones (Android): 64% worldwide web servers: >60% servers (general) : ~13% animation/visual effects: 95% Learning Linux allows you to learn other OSs quickly Solaris OS X (low-level) 3 Slide 4 Remotely Using a tool called MobaXterm like Windows cmd, but connects to machine across network What do I need to know? Linux server name: cake.lynchburg.edu your username & password on cake 4 Slide 5 The text-based interface to Linux 5 Slide 6 The shell is where users type commands see results of those commands 6 Slide 7 Example commands date: prints the date and time who: prints who else is logged in top: shows resource usage like Windows Task Manager man : shows a manual for command cmd e.g., man date grep [file]: search for a word or phrase in input or a file 7 Slide 8 Example commands ps: prints commands you are currently running kill : terminates the process with id pid e.g., 8 meehean_j@mintaka:~$ ps PID TTY TIME CMD 8578 pts/0 00:00:00 zsh 8630 pts/0 00:00:00 bash 8688 pts/0 00:00:00 sleep 8689 pts/0 00:00:00 ps meehean_j@mintaka:~$ kill 8688 meehean_j@mintaka:~$ ps PID TTY TIME CMD 8578 pts/0 00:00:00 zsh 8630 pts/0 00:00:00 bash 8690 pts/0 00:00:00 ps Slide 9 Can run commands in two modes: Foreground shells assumes command is interactive user cannot run another command until foreground command finishes default mode 9 Slide 10 Can run commands in two modes: Background shell assumes command is not interactive shell starts command, doesnt wait for it to finish command output will still be printed to screen command must be followed by & to put into background 10 Slide 11 Interacting with commands while running Ctl-C: kills foreground command Ctl-Z: suspends foreground command fg: run background command in foreground bg: run suspended command in background 11 Slide 12 Chaining commands together cmd1 ; cmd2 ; cmd3 run cmd1, then cmd2, then cmd3 cmd1 | cmd2 | cmd3 run cmd1 run cmd2 using output of cmd1 as input run cmd3 using output of cmd2 as input e.g., who | grep meehean_j 12 Slide 13 File input/output cmd > out.txt run cmd and store its output in out.txt does not print output to screen e.g., who > users.txt cmd < in.txt run cmd and use in.txt as its input grep meehean_j < users.txt 13 Slide 14 Like everything in Linux the shell is really just a program Many different options bash (the default) tcsh zsh 14 Slide 15 15 Slide 16 Log in Do basic stuff Use the file system Access my files from Windows Edit files Do C++ development Hand in my programs 16 Slide 17 Root directory top of the file system located at / Each user gets a directory (folder) called the home directory located at: /home/ Current working directory (cwd) where your shell will look for files can change your current working directory 17 Slide 18 Directory name shortcuts / : root ~ : your home directory./ : your current working directory../ : the directory one up from the cwd../../ : the directory above../ Hidden files start with a . 18 Slide 19 commands pwd: print working directory ls: list the contents of the cwd ls ls a : list hidden files too ls : list the contents of ls /tmp : list all files in /tmp cd : change cwd to cd../ : move up one directory cd /tmp : move to the temporary directory 19 Slide 20 commands touch : make a file called touch test.txt : make a file called test.txt in cwd touch /tmp/a.wav : make a wav file in /tmp rm : remove (delete) rm test.txt : remove file test.txt in cwd rm /tmp/a.wav : remove the file a.wav from /tmp 20 Slide 21 commands mkdir : make a directory called mkdir cs242 : make directory called CS242 in the cwd mkdir ~/cs242 : make cs242 directory in home dir rmdir : remove directory must be empty rmdir cs242 : remove directory cs242 from cwd rmdir ~/cs242 : remove directory cs242 from home rm r : remove directory doesnt need to be empty 21 Slide 22 commands cp : make a copy of called cp a.cpp a.cpp.bkup : create a copy of a.cpp called a.cpp.bkup mv : rename/move to mv a.cpp b.cpp : change a.cpp to b.cpp mv a.cpp.bkup a.cpp : overwrite a.cpp with a.cpp.bkup 22 Slide 23 commands cat : print file contents to screen cat a.cpp : print a.cpp to screen less : open file using file reader less less a.cpp : read a.cpp using less use arrows to move around use q to quit 23 Slide 24 The wildcard: * it means match 0 or more characters e.g., ls *.h list all files that end in.h e.g., ls *foo* list all files that contain foo e.g., rm *.o remove all files that end in.o mv *../ move all the files in cwd up one directory 24 Slide 25 Network file service for Linux maps your home directory in Windows Preparation have system admin add you as samba user log in to cake set your samba password 25 Slide 26 Mapping mintaka home directory in Windows right click on Computer select map network drive folder: \\cake\ select log in as different user user: \ password: 26 Slide 27 Problems with storing files on cake none of the files on cake are backed up hard drives fail all the time, files may be lost cannot map network drive from off campus Dropbox files available on cake and backed up in cloud can reach files from anywhere find instructions here on how to setup 27 Slide 28 28 Slide 29 Log in Do basic stuff Use the file system Access my files from Windows Edit files Do C++ development Hand in my programs 29 Slide 30 30 Slide 31 Simple extensible editor can edit several different languages can run the debugger can read your email can do everything really 31 Slide 32 Terminology buffer: an open file kill: cut yank: paste point: cursor mark: a user-specified location e.g., kill from point to mark region: text between point and mark window: emacs can be split into different buffers open at the same time 32 Slide 33 Starting it up > emacs Emacs documentation abbreviations Alt = M (called meta) Ctrl = C (called command) Commands in emacs C-x C-f: open a file C-x C-s: save the current buffer to its file C-x C-c: close and exit 33 Slide 34 Search and replace C-s: search forward C-s fool : move point to next fool after point C-r: seach backward C-r fool : move point to previous fool before point M-% : find and replace interactive directions provided on screen 34 Slide 35 Kill and yank cut and paste C-k : kill to end of line cut from point to end of line C-y : yank back last thing killed paste at point C-w : kill region multiple sequential kills are yanked back with one yank 35 Slide 36 Windows C-x 2: split into two windows, above and below C-x 3: split window, side-by-side C-x 0: delete this window (unsplit) C-x 1: delete all other windows beside this one C-x o: move to another window Buffers C-x b: select another buffer (open file) C-x C-b: select another buffer (from list) 36 Slide 37 37 Slide 38 Editor runs in terminal cannot use the mouse Two modes command mode for entering commands text entry mode for inserting text into file Starting up > vi file_to_edit 38 Slide 39 Working with files :e file = open file :w = save file :q! = quit without saving changes :x = save changes and quit Inserting text a = switch to text entry mode after cursor i = switch to text entry mode before cursor Esc = switch back to command mode 39 Slide 40 Search /word: seach for word n: search for next appearance of word N: search for previous appearance of word 40 Slide 41 Replace :rs/foo/bar/a: replace foo with bar r is range, can be nothing: works only on current line number: works only on line number % : whole file a is argument, can be any combo of: g: replace all occurrences in line, not just first i,I: ignore case, dont ignore case c: confirm each replace :%s/teem/team/gi 41 Slide 42 Cut/Copy/Past/Undo x: cut current character dd: cut line Xdd: cut X lines 5dd D: cut to end of line yy: copy line p: paste u: undo 42 Slide 43 43 Slide 44 Log in Do basic stuff Use the file system Access my files from Windows Edit files Do C++ development Hand in my programs 44 Slide 45 C++ compilation demystified compile source files into binary object files i.e.,.cpp into.o link together multiple object files into a program i.e., combine multiple.o files into an executable 45 Slide 46 g++ is the Linux compiler for C++ -c compile e.g., g++ -c file1.cpp file2.cpp file3.cpp -o link e.g., g++ file1.o file2.o file3.o o executable Useful options for g++ -g: include debugging information -Wall: print out any warnings 46 Slide 47 make is a program that helps build other programs if you have a lot of files it only builds the ones that changed or the ones that depend on the ones that changed foo.cpp depends on declarations in foo.h changing foo.h requires recompiling foo.cpp 47 Slide 48 How does it determine the dependencies? you tell it using a makefile makefile should be named makefile contains instructions on how to build your program variables: Variable = value dependencies: file.cpp: depedency.h commands: command 48 Slide 49 49 # comment CC = g++ CFLAGS = -g Wall # special default dependency all: hello_world # tab is required hello_world: hello.o world.o $(CC) $(CFLAGS) o hello_world hello.o world.o # to reference a variable you need $() hello.o: hello.h world.h hello.cpp $(CC) $(CFLAGS) c hello.cpp world.o: world.h world.cpp $(CC) $(CFLAGS) c world.cpp Slide 50 50 # special tag to clean up clean: rm f hello_world *.o Slide 51 How do I use make? > make runs make for the default target > make runs make for the make clean make hello_world 51 Slide 52 How do I figure out the dependencies? look at the includes for.h &.cpp files What if a file1.h is dependent on file2.h? file1.h: file2.h only need to list dependency no need for a command 52 Slide 53 How would I make a terrible makefile? 53 CC = g++ CFLAGS = -g Wall HEADERS = hello.h world.h CFILES = hello.c world.c OBJS = hello.o world.o all: hello_world hello_world: $(OBJS) $(CC) $(CFLAGS) o hello_world $(OBJS) $(OBJS): $(HEADERS) $(CFILES) $(CC) $(CFLAGS) c $(CFILES) Slide 54 If you include the same.h file twice g++ will think you are redefining structs, classes, and functions Avoid by using include guards in.h files 54 #ifndef CLASSNAME #define CLASSNAME // contents of your.h file #endif /* CLASSNAME */ Slide 55 Need to make a single file that holds all of your source files and makefile Linux tar command tar options files tar options c: create a tarball v: verbose, list operations as they happen f filename: store the tarball in a filename z: tarball is/should be compressed x: extract files from tarball 55 Slide 56 tar examples tar cvfz alphabet.tgz a.txt b.txt c.txt create a compressed tarball named alphabet.tgz it should contain a.txt, b.txt, and c.txt tar xvfz alphabet.tgz extract the compresed tarball named alphabet.tgz tar cvfz a.txt b.txt c.txt create a compressed tarball named a.txt it should contain b.txt and c.txt common mistake, overwrites a.txt 56 Slide 57 Using tar to hand in assigment make a directory that includes username and program name e.g., granger_s.p0 put program files in new directory including makefile tar up directory into appropriately named tarball e.g., tar cvfz granger_s.p0.tgz granger_s.p0 hand in using Moodle and Dropbox or Samba 57 Slide 58 58