Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address:...

43
Introduction to Linux / Unix CSCI 156

Transcript of Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address:...

Page 1: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Introduction to Linux / Unix

CSCI 156

Page 2: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

TAs• Name: Eric Denman• Room: tbd• Email address: [email protected]• Office Hours: Wed 6-9 – Tompkins 211

• Name: Fanchun Jin• Room: Phillips 730• Email address: [email protected]• Phone: 994-4217• Office Hours: Mon 1-4 – Tompkins 211

Page 3: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

References

• John R. Levine, “UNIX for Dummies”• Kernighan and Ritchie, “The C Programming Language”• Deitel & Deitel, “How to Program C++”• Deitel & Deitel, "How to Program C"• Eric Roberts, “Programming Abstractions in C”

Page 4: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Why learn C / C++?• Operating Systems written in C / C++ and

Assembly– Why not write OS in Java?

• Lots of other stuff written in C / C++

• Java is a great language... C / C++ is not a replacement for certain programming.

• (Assembly, Perl, Matlab/Fortran, SQL and Lisp/Scheme/Prolog/ML are very appropriate for certain applications. Choose your language wisely.)

Page 5: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Why learn C / C++?• Why not write OS in Java?

• Speed. Java is interpreted. What is interpreted?• Java "safety features" make certain memory

manipulations that the Operating System requires difficult...

–Need direct access to memory

»Read memory location “0x00000004”

–Need direct access to I/O devices and hardware

»Put a “1” on pin 3 of the serial port• It’s easier to write a C compiler than it is to write a

Java Virtual Machine (many JVMs are written in C)

Page 6: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Why C? Why not C++?

• C++ is object oriented version of C

• The trickiest parts of C++ are from C• If you are a Computer Engineering

student.. you'll probably never program at that level... (you will DEFINITELY need C)

• If you are a Computer Science student... if you're good at Java and C... you can learn C++ in a few hours...

Page 7: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

LINUX• We’ll work from the command line (Why?)

– not all computers have a GUI...– commands are similar to Solaris and UNIX– In Red Hat: Menu->"System Tools" -> "Terminal"

• All commands that you type in actually run programs– passwd is a program that allows you change your

password, etc.

• The “shell” is the program that runs to execute the commands that you type in...

– Red Hat automatically runs the “bash” shell when you open the command line.

– On some Solaris systems the “bourne” shell is automatically run- and you can type bash to invoke bash shell (Recommended)

Page 8: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Cool UNIX programs• man

– Don’t know what a program or command does? Use the man command followed by the command or program name

– Example: man ls tells you what ls does• Press ‘Q’ or ‘q’ on Red Hat Linux to get out of man

• ls– List the files in the directory (like dir in DOS)

• cd directory_name– Change Directory = move down in to the

specified directory (folder)• cd ..

– Move up a directory in the hierarchy (to the parent folder/directory)

Page 9: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

More Directory Stuff• cd ~ - to go to your home directory

• cd . – you’re already there! ‘.’ is the directory you’re currently in…

• cd ~/my_stuff - go to the my_stuff directory

that is in my home directory

Page 10: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

More Directory Stuff• Path Names

– the files on your computer are arranged in a tree hierarchy (i.e. files in folders inside other folders)

– The top of the tree is the "root directory" and is

denoted by the / (forward slash) symbol.

– My home directory path is /home/edenman

•cd /home/edenman takes me to my home

•cd /home/tortilla takes you to your home

– Commands/programs may need the full path

Page 11: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

More Directory Stuff• Path Names

-To run my project 1 program from anywhere:/home/edenman/proj1– To run my project 1 executable from the same

directory ./proj1

– This is real important when copying and moving files

– If you get “file not found” errors, this is probably the cause

Page 12: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

More Directory Stuff• Absolute Paths vs Relative Paths

/home/edenman/proj1

~/proj1

• pwd – Print Working Directory – Where am I?

• You can rename directories with mv command

Page 13: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Commands continued…• Commands can have certain options selected

– “Command Line Options” turn certain features on and off – use the man pages to figure it out

– Example: ls –l (that’s an L) gives a more detailed listing of files

• cp old_file new_file – makes an identical copy of old_file called new_file

• rm old_file - removes the old file• mv old_file new_file – moves the old file to

the new file (renames the file – does not copy)• exit – closes the command line window when

you’re finished

Page 14: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Commands continued…• cat my_file – lists the contents of my_file in

ascii text characters (works nicely for code files)• head my_file – lists the first few lines of the

contents of the file• mkdir new_directory – makes a new

directory called “new_directory”• rmdir old_directory – gets rid of a an old

directory called “old_directory” (must be empty)

• id - some information about your user account• groups - what groups you belong to

Page 15: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Cool BASH stuff• Its all forward slashes in UNIX-> ‘/’ vs DOS ‘\’• Don’t like your program or command-

“Control+C” to quit running it…

• Repeating a command– press the up arrow (can be done repeatedly)– press the down arrow

• Don’t know the exact spelling?– Start with first few letters and then press tab key

Page 16: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Cool BASH stuff

• Want a command to effect multiple files? Use the wildcard = ‘*’ = “anything at all”

– Examples: rm * removes all files from a directory rm *~ removes all files that end in ~

rm *cool* removes all files with “cool” in them

Another wildcard = ‘?’ = “any single letter”

– Examples: rm ?ain.c

Page 17: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Directory Listing details•Let’s do an ls -l -rw-rw-r-- 1 larry larry 4096 Aug 15 21:36 foo drw-rw-r-x 2 moe moe 3024 Aug 11 1995 nachos•The first letter is:

–a dash if it this entry is a file

–a ‘d’ is if it is a directory

–a ‘l’ if it is a link (a "shortcut" / ”reference” to another file). The real file is actually somewhere else…

Page 18: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Directory Listing details•Let’s do an ls -l -rw-rw-r-- 1 larry larry 4096 Aug 15 21:36 foo drw-rw-r-x 2 moe moe 3024 Aug 11 1995 nachos•The next 9

characters show permissions:

–The first group of 3 characters determine owner’s privileges

–The second group of 3 chars determine what other members of the owner’s group can do

–The third group of 3 chars determine what everyone else (generic users) can do

Page 19: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Directory Listing details•Let’s do an ls -l -rw-rw-r-- 1 larry larry 4096 Aug 15 21:36 foo drw-rw-r-x 2 moe moe 3024 Aug 11 1995 nachos•r – denotes privilege to read the file•w – denotes privilege to write to the file•x - denotes privilege to execute the file•In this case (the nachos directory):

– The user can read and write to the directory– Members of his group can read and write to the directory– Other users can read and execute programs in this dir

Page 20: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Directory Listing details•Let’s do an ls -l -rw-rw-r-- 1 larry larry 4096 Aug 15 21:36 foo drw-rw-r-x 2 moe moe 3024 Aug 11 1995 nachos•For files this number denotes the

number of links (shortcuts) pointing at the file•For directories- this is the number of subdirectories + 2 (don’t ask why…)

Page 21: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Directory Listing details•Let’s do an ls -l -rw-rw-r-- 1 larry larry 4096 Aug 15 21:36 foo drw-rw-r-x 2 moe moe 3024 Aug 11 1995 nachos•The left entry is the username of

the owner of the file•The right entry is the group owner of the group to which the file or directory belongs

Page 22: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Directory Listing details•Let’s do an ls -l -rw-rw-r-- 1 larry larry 4096 Aug 15 21:36 foo drw-rw-r-x 2 moe moe 3024 Aug 11 1995 nachos•Size of the file in bytes•Date and time the file was last

modified (year for directories)•The file name

Page 23: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Permissions table(borrowed from Levine)

• 0 None• 1 Execute Only• 2 Write Only• 3 Write and Execute• 4 Read only• 5 Read and Execute• 6 Read and Write• 7 Read, Write, and Execute

Page 24: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

chmod• chmod is used to set the permissions on

a given file• Example: chmod 777 file_name

– The first 7 gives the user read, write and execute permissions

– The second 7 gives the same permissions to group members

– The third 7 gives the same permissions to all other users

• Thus we’ve given everyone permission to mess around with file_name

Page 25: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

My program ran so fast I couldn’t tell what happened!

• Use the ‘>’ command to send program output to a text file which you can then use vim (or your editor of choice) to edit

• ls > my_directory the output of the ls program is put in to a text file called my_directory– Then vim my_directory and there it is!

• Use ‘>>’ to append output to the end of file

Page 26: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

My program ran so fast I couldn’t tell what happened!

Or try the more program– For example: try ls | more– The ‘|’ (vertical bar) is known as the pipe

operator – it sends the output of the ls program as input to the more program

• To display a big file (instead of cat):– Try more really.big.file

• What does ls | sort do?

Page 27: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Processes: Doing Several Things Simultaneously• UNIX, like later versions of Windows, has

several processes running at any given time. This is known as multiprogramming.

• NOTE: not necessarily a 1-to-1 correspondence between processes and programs

• To stop the command line interface process and start another program/process you enter its name at the command line:

– To start the VIM editor: vim

• Inputs to the program (arguments) can be specified after the command. For vim - the file you want to edit can be listed:

vim my_code.c

Page 28: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Processes: Doing several things at same time

• To start a program in the background and keep the command line interface running in the foreground- follow the command and inputs with a space followed by the ampersand ‘&’– For example: gvim my_code.c &

• Sometimes programs require use of the command line ‘console’- may not be able to use ‘&’ when running some programs.

• SOLUTION: open another command line console. (background processes can’t read input from console)

Page 29: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Processes: ps, top and kill• ps – gives you a list of the processes that you

have started (does not include system processes in background)

• top – gives you a list of all processes running on the processor, along with all other sorts of neat information (priority, % cpu utilization, status, time running, PID)

• PID- Process Identification – a number used to identify a particular process

– When you start a program it is printed after prompt (&)– Why not use the programs name?

• kill – use it with the PID to kill a process

Page 30: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

diff and find• diff file_1.c file_2.c - compares two

files. Its output is where the 2 files differ. Usually use this with output redirection, i.e.:

– diff proj1.c solution.c > changes.txt

• find – use it to find a file (several ways to use)– find . -name my_code.c –print

• What to look for• Directory where to look (will search in

subdirectories)• If you use wildcards (* or ?) you have to put name

in “quot*s”

Page 31: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

grep• Need to find all the files that contain the

word “foo” in the current directory?– grep “foo” *

• What to look for• Which files in the current directory to look for it in

– grep –i “foo” * ignore case of foo– grep “C\.I\.A\.” * must escape . * [ ] ^ $ WHAT DOES THIS MEAN?

– grep –r “foo” * search recursively

Page 32: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

gzip, zip, tar

• gunzip / gzip (.gz file extension)

• zip / unzip (.zip file extension)• tar (.tar file extension)

– tar cvf archive.tar file1 file2 file3– tar xvf archive.tar

• tgz (combined tar and gzip)– tar czvf archive.tgz file1 file2 file3– tar xzvf archive.tgz

Page 33: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

VIM

• vi: original unix editor

• vim: vi improved (vimproved)– support for syntax highlighting, ctags, among

many other added features– two modes: insert mode and command mode– to enter insert mode, press 'i'– to enter command mode, press Esc

Page 34: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

VIM modes

• command mode: – “:w” to save your document– “:q” to quit vim (save and quit is “:wq”)– search “/string” or replace “:%s/orig/new”– “dd” to cut/delete a line– “yy” to copy a line, “p” to paste– “w” to skip to the next word, “b” to go back

• insert mode: type normally– windows shortcuts like ctrl-right will not work

Page 35: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Compiling, Linking, AssemblingTraditionally, the main body of your C

source resides in a file called main.cTo compile your code into an executable

called test:

gcc -o test main.c

To run the program we type ./test

(If the -o flag isn't added, the executable will be ./a.out )

Page 36: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Compiling, Linking, AssemblingHeader files contain the "interfaces" to all of

your functions. They typically end in .hfoo.h

C++ files end in .cc or .cpp

foo.cc or foo.cpp

Object files (formed after compilation to be used for linking) end in .o

foo.o

Page 37: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Compiling, Linking, Assembling• Makefile is a batch file we will use on

most projects to compile and link.

• What is a batch file?

Page 38: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Compiling, Linking, AssemblingA batch file contains a sequence of

commands for the computer to carry out.

The Makefile is a batch file for compilation and linking. Batch files are very powerful features of UNIX.

• To invoke the Makefile you issue the make command from inside your project directory.

– This “runs” the batch file which runs the compiler and the linker for you on files

– You’ll either get errors or a executable

Page 39: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Compiling, Linking, Assembling• After your make you’ll have some

unnecessary files around. Prior to your next attempt at “building” your executable (after a revision) do a “make clean” to clean up these object files, etc. so that you are certain you start from scratch.

• If you use extra source files (more .c files) then you’ll have to edit your Makefile

Page 40: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Some extra stuff• lp or lpr commands can be used to print• Use chgrp to change a file’s group• On Solaris xterm command can be used to

start the Graphical User Interface (GUI)

Page 41: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Exercise 1

• make a new directory called fajita in the /home directory

• inside that directory, create a file called foo and make it executable by all users

• delete the /home/fajita directory

Page 42: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Exercise 2

• Write a c program to print “Hello World” to the screen. Use VIM to edit a c file, and gcc to compile it.

Page 43: Introduction to Linux / Unix CSCI 156. TAs Name: Eric Denman Room: tbd Email address: edenman@gwu.edu Office Hours: Wed 6-9 – Tompkins 211 Name: Fanchun.

Exercise 3

• Write a program that will produce the output: *

*** ************ ***** *** *

Use as much code re-use as possible (loops!)