Linux Tutorials Network Design/Computer vision by Helal Saghir The Command Prompt
• Commands are the way to “do things” in Unix • A command consists of a command name and options called “flags” • Commands are typed at the command prompt • In Unix, everything (including commands) is case- sensitive [prompt]$
Command (Optional) arguments Command Prompt (Optional) flags
Note: Many Unix commands will print a message only if something went wrong. Be careful with rm and mv. Getting help with man • man (short for “manual”) documents commands – man
• Open secure shell from windows. • Enter server name , username and password . You can login remotely. • In linux use this command • ssh [email protected] • Will ask your password and let you enter. yppasswd
• To change the password on linux use • passwd • In lab you have to use yppasswd as we are running NIS. Quota
• Quota limit is 100 MB. • Check quota by using du • du – disk usage. • du –cms • -c : total • -m: megabytes • -s: summary • There is quota command also but it won’t work because of NIS running on clients. It should work on your home computer, if you have Linux installed on it and quota rpm on. Linux Commands
• cd – change directory • cd .. – changes to one up-level • cd / - goes to the root • cd $HOME – takes to you home directory • cd $home – does the same thing • Handy directories to know ~ Your home directory ls command
• ls – shows the list of files/directory • ls –al – shows all files including hidden with detail. • ls –l - shows all files not including hidden. • ls
• cp – copy files • cp [source_file1] [des_file1] • -p : preserve=mode,ownership,timestamp • -R –r : copy directories recursively Move / mkdir
• mv : move files /directory • mv [source] [destination]
• mkdir : make directory – Syntax: mkdir
• Rm –remove non-empty directory or file • -r: recursive • -f : force • -v: verbose Shell Shortcuts • Tab completion – Type part of a file/directory name, hit
• Cat : concatenation • -cat [file1] [file2] • Attach both file and gives the output
• more : views a file, pausing every screenful • more [file1] Redirection
• Standard Input is from keyboard • Standard Output is the monitor
• Able to change this! command < infile > outfile command < infile command > outfile command >> outfile –attach at end Pipes
• Output of one command “piped into” another command1 | command2 • Do the first command command1 • Feed its output as input for command2 Wildcards
Group of files accessed using wildcards
* matches everything ls g* lists all files beginning with g ls go.* lists files named go with any extension ls * lists all files Commands cond.
• history gives a history of your commands • date displays date >date • who who is on the system • finger get more info about another user. • jobs one of a number of job control commands. Gives the job No. of your jobs. – kill kill a specified job number Find command
• find: find the file • find [place to find] –name “filename” –print • . – current directory • ~ home directory • / whole root directory chmod
• chmod : change mode • ugo : user, group, other • rwx: read, write, execute • 777 :read, write, execute for all • chmod 755 test.c printing
• lpr
• gcc –o
• Choice is on you : • emacs • vi • gedit • pico • other Make
• The make utility is a software engineering tool for managing and maintaining computer programs. • make provides most help when the program consists of many component files. As the number of files in the program increases so to does the compile time, complexity of compilation command and the likelihood of human error when entering command lines, i.e. typos and missing file names. make cont.
• By creating a descriptor file containing • dependency rules, • macros and • suffix rules • you can instruct make to automatically rebuild your program whenever one of the program's component files is modified. • make is smart enough to only recompile the files that were affected by changes thus saving compile time. make cont..
• Make goes through a descriptor file starting with the target it is going to create. • Make looks at each of the target's dependencies to see if they are also listed as targets. • It follows the chain of dependencies until it reaches the end of the chain and then begins backing out executing the commands found in each target's rule. • Make builds object files from the source files and then links the object files to create the executable. The Make Command
• make syntax:
make [-f
• argument descriptions: -f
• Now we need a rule to make a toast, and another to make coffee. I won't go through the excercise of writing these rules out: I think you get the idea. Makefile Syntax
• Rule Syntax:
• The
• All variable values default to the shell variable values Simple example for Makefile • This is an example descriptor file to build an executable file called prog1. • It requires the source files file1.cc, file2.cc, and file3.cc. • An include file, mydefs.h, is required by files file1.cc and file2.cc. • You can run it in a normal way by : % CC -o prog1 file1.cc file2.cc file3.cc Descriptor file will run it like this : % make prog1 or if prog1 is the first target defined in the descriptor file % make Simple example of Descriptor file
#top level rule to compile the whole process all:prog1 #program is made of several source files prog1 : file1.o file2.o file3.o gcc -o prog1 file1.o file2.o file3.o #rule for the file “file1.o” file1.o : file1.cc mydefs.h gcc -c file1.cc #rule for the file “file2.o” file2.o : file2.cc mydefs.h gcc -c file2.cc #rule for the file “file3.o” file3.o : file3.cc gcc -c file3.cc #rule for cleaning files generator during compilation clean : rm file1.o file2.o file3.o Making Descriptor file smaller OBJS= file1.o file2.o file3.o Prog1:${OBJS} gcc -o prog1 ${OBJS} file1.o : file1.cc mydefs.h gcc -c file1.cc file2.o : file2.cc mydefs.h gcc -c file2.cc file3.o : file3.cc gcc -c file3.cc clean : rm ${OBJS} Making Descriptor file even smaller #this is macro OBJS= file1.o file2.o file3.o CC=gcc CFLAGS = -g –wall prog1:${OBJS} ${CC} -o $@ ${OBJS} file1.o file2.0: mydefs.h clean : rm ${OBJS} Make Power Features
• “Fake” targets – targets that are not actually files – can do just about anything, not just compile – like the “clean” target • Forcing re-compiles – touch the required files – touch the Makefile to rebuild everything Debugging
• The ideal: do it right the first time • The reality: bugs happen • The goal: exterminate, quickly and efficiently Debugging Methods
• The best way: read the code – If you don’t understand it, it’ll happen again • Consistent use of error checking & handling – most uncaught errors are not where they appear to be • Examine the state at key points in the code – wrap debug output with #ifdef DEBUG – use a debugger, like gdb, to view the state Efficiency in Debugging
• The goal is to isolate the problem and fix it • Don’t try random things, looking for a solution – if you don’t understand it…it’ll be back – half the time, this takes an insanely long time – you don’t learn anything from it • Look for the problem, not the solution – figure out two points in code that the problem is between, and close the gap from there – once you know the line the bug is on, it’s easy Breakpoints
• What are they? – A “break point”: a point in the code where execution pauses, or “breaks”, then continues • What are they for? – To examine the state of the running program • How do you make them? – modifying code to print information and pause – use a debugger to interactively step through code Debuggers • Advantages over the “old fashioned” way: – you can step through code as it runs – you don’t have to recompile to set a breakpoint – you can examine the entire state of the program • call stack, variable values, scope, etc. – you can modify values in the running program – you can view the state of a crash using core files GDB, the GNU DeBugger
• Text-based, invoked with: gdb [
• Argument descriptions:
• General Commands: file [