Upload
jessica-angelina
View
80
Download
3
Embed Size (px)
DESCRIPTION
h
Citation preview
1FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
TheUnixandGNU/Linux
commandline
MichaelOpdenackerThomasPetazzoniFreeElectrons
Copyright2009,FreeElectrons.CreativeCommonsBYSA3.0licenseLatestupdate:Jul15,2010,Documentsources,updatesandtranslations:http://freeelectrons.com/docs/commandlineCorrections,suggestions,contributionsandtranslationsarewelcome!
2FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Commandmementosheet
Itisausefulcompaniontothispresentation.
Examplesforthemostusefulcommandsaregiveninjustonesheet.
SuggestionsforuseStickthissheetonyourwall,useitasdesktopwallpaper,makeitamousemat,printitonclothing,sliceitintobookmarks...
CautionStoreawayfrommice!
Getitonhttp://freeelectrons.com/docs/commandline
3FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TrainingContents(1)
Shells,filesystemandfilehandling
Everythingisafile
GNU/Linuxfilesystemstructure
Commandlineinterpreters
Handlingfilesanddirectories
Displaying,scanningandsortingfiles
Symbolicandhardlink
Fileaccessrights
4FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Trainingcontents(2)
StandardI/O,redirections,pipes
Standardinputandoutput,redirectingtofiles
Pipes:redirectingstandardoutputtoothercommands
Standarderror
5FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TrainingContents(3)
Taskcontrol
Fullcontrolontasks
Executinginbackground,suspending,resumingandaborting
Listofactivetasks
Killingprocesses
Environmentvariables
PATHenvironmentvariables
Shellaliases,.bashrcfile
6FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Trainingcontents(4)
Miscellaneous
Texteditors
Compressingandarchiving
Printingfiles
Comparingfilesanddirectories
Lookingforfiles
Gettinginformationaboutusers
7FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
Unixfilesystem
8FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Everythingisafile
Regularfiles
DirectoriesDirectoriesarejustfileslistingasetoffiles
SymboliclinksFilesreferringtothenameofanotherfile
DevicesandperipheralsReadandwritefromdevicesaswithregularfiles
PipesUsedtocascadeprogramscat*.log|greperror
SocketsInterprocesscommunication
AlmosteverythinginUnixisafile!
9FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Filenames
FilenamefeaturessincethebeginningofUnix
Casesensitive
Noobviouslengthlimit
Cancontainanycharacter(includingwhitespace,except/).Filetypesstoredinthefile(magicnumbers).Filenameextensionsnotneededandnotinterpreted.Justusedforuserconvenience.
Filenameexamples:README .bashrc WindowsBuglistindex.htm index.html index.html.old
10FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Filepaths
Apathisasequenceofnesteddirectorieswithafileordirectoryattheend,separatedbythe/character
Relativepath:documents/fun/microsoft_jokes.htmlRelativetothecurrentdirectory
Absolutepath:/home/bill/bugs/crash9402031614568
/:rootdirectory.Startofabsolutepathsforallfilesonthesystem(evenforfilesonremovabledevicesornetworkshared).
11FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
GNU/Linuxfilesystemstructure(1)
Notimposedbythesystem.Canvaryfromonesystemtotheother,evenbetweentwoGNU/Linuxinstallations!
/ Rootdirectory/bin/ Basic,essentialsystemcommands/boot/ Kernelimages,initrdandconfigurationfiles/dev/ Filesrepresentingdevices
/dev/hda:firstIDEharddisk/etc/ Systemconfigurationfiles/home/ Userdirectories/lib/ Basicsystemsharedlibraries
12FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
GNU/Linuxfilesystemstructure(2)
/lost+found Corruptfilesthesystemtriedtorecover/media Mountpointsforremovablemedia:
/media/usbdisk,/media/cdrom/mnt/ Mountpointsfortemporarilymountedfilesystems/opt/ Specifictoolsinstalledbythesysadmin
/usr/local/oftenusedinstead/proc/ Accesstosysteminformation
/proc/cpuinfo,/proc/version.../root/ rootuserhomedirectory/sbin/ Administratoronlycommands/sys/ Systemanddevicecontrols
(cpufrequency,devicepower,etc.)
13FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
GNU/Linuxfilesystemstructure(3)
/tmp/ Temporaryfiles/usr/ Regularusertools(notessentialtothesystem)
/usr/bin/,/usr/lib/,/usr/sbin.../usr/local/ Specificsoftwareinstalledbythesysadmin
(oftenpreferredto/opt/)/var/ Datausedbythesystemorsystemservers
/var/log/,/var/spool/mail(incomingmail),/var/spool/lpd(printjobs)...
TheUnixfilesystemstructureisdefinedbytheFilesystemHierarchyStandard(FHS):http://www.pathname.com/fhs/
14FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
Shellsandfilehandling
15FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Commandlineinterpreters
Shells:toolstoexecuteusercommands
Calledshellsbecausetheyhidethedetailsontheunderlyingoperatingsystemundertheshell'ssurface.
Commandsareinputinatextterminal,eitherawindowinagraphicalenvironmentoratextonlyconsole.
Resultsarealsodisplayedontheterminal.Nographicsareneededatall.
Shellscanbescripted:providealltheresourcestowritecomplexprograms(variable,conditionals,iterations...)
16FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Wellknownshells
Mostfamousandpopularshells
sh:TheBourneshell(obsolete)Traditional,basicshellfoundonUnixsystems,bySteveBourne.
csh:TheCshell(obsolete)OncepopularshellwithaClikesyntax
tcsh:TheTCshell(stillverypopular)ACshellcompatibleimplementationwithevolvedfeatures(commandcompletion,historyeditingandmore...)
bash:TheBourneAgainshell(mostpopular)Animprovedimplementationofshwithlotsofaddedfeaturestoo.
17FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
fish:agreatnewshell
TheFriendlyInteractiveSHellhttp://www.fishshell.org/
Standardfeatures:history,commandandfilecompletion...
Brandnewfeatures:commandoptioncompletion,commandcompletionwithshortdescription,syntaxhighlighting..
Easiertoanyopenfiles:openbuiltincommand.
Muchsimplerandconsistentsyntax(notPOSIXcompliant)Makesiteasiertocreateshellscripts.
Commandlinebeginnerscanlearnmuchfaster!Evenexperiencedusersshouldfindthisshellveryconvenient.
18FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
lscommand
lsa(all)Listsallthefiles(including.*files)
lsl(long)Longlisting(type,date,size,owner,permissions)
lst(time)Liststhemostrecentfilesfirst
lsS(size)Liststhebiggestfilesfirst
lsr(reverse)Reversesthesortorder
lsltr(optionscanbecombined)Longlisting,mostrecentfilesattheend
Liststhefilesinthecurrentdirectory,inalphanumericorder,exceptfilesstartingwiththe.character.
19FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Filenamepatternsubstitutions
Betterintroducedbyexamples!
ls*txtTheshellfirstreplaces*txtbyallthefileanddirectorynamesendingbytxt(including.txt),exceptthosestartingwith.,andthenexecutesthelscommandline.
lsd.*Listsallthefilesanddirectoriesstartingwith.dtellslsnottodisplaythecontentsofdirectories.
cat?.logDisplaysallthefileswhichnamesstartby1characterandendby.log
20FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Specialdirectories(1)
./
Thecurrentdirectory.Usefulforcommandstakingadirectoryargument.Alsosometimesusefultoruncommandsinthecurrentdirectory(seelater).
So./readme.txtandreadme.txtareequivalent.
../
Theparent(enclosing)directory.Alwaysbelongstothe.directory(seelsa).Onlyreferencetotheparentdirectory.
Typicalusage:cd..
21FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Specialdirectories(2)
~/
Notaspecialdirectoryindeed.Shellsjustsubstituteitbythehomedirectoryofthecurrentuser.
Cannotbeusedinmostprograms,asitisnotarealdirectory.
~sydney/
Similarly,substitutedbyshellsbythehomedirectoryofthesydneyuser.
22FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thecdandpwdcommands
cdChangesthecurrentdirectoryto.
cdGetsbacktothepreviouscurrentdirectory.
pwdDisplaysthecurrentdirectory("workingdirectory").
23FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thecpcommand
cpCopiesthesourcefiletothetarget.
cpfile1file2file3...dirCopiesthefilestothetargetdirectory(lastargument).
cpi(interactive)Asksforuserconfirmationifthetargetfilealreadyexists
cpr(recursive)Copiesthewholedirectory.
24FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
mvandrmcommands
mv (move)Renamesthegivenfileordirectory.
mvi(interactive)Ifthenewfilealreadyexits,asksforuserconfirm
rmfile1file2file3... (remove)Removesthegivenfiles.
rmi(interactive)Alwaysaskforuserconfirm.
rmrdir1dir2dir3(recursive)Removesthegivendirectorieswithalltheircontents.
25FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Creatingandremovingdirectories
mkdirdir1dir2dir3...(makedir)Createsdirectorieswiththegivennames.
rmdirdir1dir2dir3...(removedir)RemovesthegivendirectoriesSafe:onlyworkswhendirectoriesandempty.Alternative:rmr(doesn'tneedemptydirectories).
26FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Displayingfilecontents
Severalwaysofdisplayingthecontentsoffiles.
catfile1file2file3...(concatenate)Concatenatesandoutputsthecontentsofthegivenfiles.
morefile1file2file3...Aftereachpage,askstheusertohitakeytocontinue.Canalsojumptothefirstoccurrenceofakeyword(/command).
lessfile1file2file3...Doesmorethanmorewithless.Doesn'treadthewholefilebeforestarting.Supportsbackwardmovementinthefile(?command).
27FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Theheadandtailcommands
head[]Displaysthefirstlines(or10bydefault)ofthegivenfile.Doesn'thavetoopenthewholefiletodothis!
tail[]Displaysthelastlines(or10bydefault)ofthegivenfile.NoneedtoloadthewholefileinRAM!Veryusefulforhugefiles.
tailf(follow)Displaysthelast10linesofthegivenfileandcontinuestodisplaynewlineswhentheyareappendedtothefile.Veryusefultofollowthechangesinalogfile,forexample.
Examplesheadwindows_bugs.txttailfoutlook_vulnerabilities.txt
28FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thegrepcommand
grepScansthegivenfilesanddisplaysthelineswhichmatchthegivenpattern.
greperror*.logDisplaysallthelinescontainingerrorinthe*.logfiles
grepierror*.logSame,butcaseinsensitive
greprierror.Same,butrecursivelyinallthefilesin.anditssubdirectories
grepvinfo*.logOutputsallthelinesinthefilesexceptthosecontaininginfo.
29FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thesortcommand
sortSortsthelinesinthegivenfileincharacterorderandoutputsthem.
sortrSame,butinreverseorder.
sortruu:unique.Same,butjustoutputsidenticallinesonce.
Morepossibilitiesdescribedlater!
30FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thesedcommand
sedisaStreamEDitor
Itparsestextfilesandimplementsaprogramminglanguagetoapplytransformationsonthetext.
Oneofthemostcommonusageofsedistextreplacement,whichreliesonregularexpressions
sede's/abc/def/'testfilewillreplaceeverystringabcbydefinthefiletestfileanddisplaytheresultonthestandardoutput.
sed's/^[\t]*//'testfilewillremoveanytabulationorspaceatthebeginningofaline
sed's/^|\([^|]*\)|\([^|]*\)|$/\1>\2/'testfilereplacelineslike |string1|string2|by string1>string2
31FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
sed:regularexpressions
RegularexpressionsareusefulinmanyUnixtools,notonlysed.Theyallowtomatchtheinputtextagainstanexpression.
.matchesanycharacter
[]matchesanycharacterlistedinsidethebrackets
[^]matchesanycharacternotlistedinsidethebrackets
^matchesthebeginningoftheline
$matchestheendoftheline
*matchesthepreviouselementzeroormoretimes,+matchesthepreviouselementoneormoretimes,?matchesthepreviouselementzerooronetime
\(\)definesasubexpressionthatcanbelaterrecalledbyusing\n,wherenisthenumberofthesubexpressionintheregularexpression
Moreathttp://www.regularexpressions.info/
32FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Symboliclinks
Asymboliclinkisaspecialfilewhichisjustareferencetothenameofanotherone(fileordirectory):
Usefultoreducediskusageandcomplexitywhen2fileshavethesamecontent.
Example:anakin_skywalker_biography>darth_vador_biography
Howtoidentifysymboliclinks:
lsldisplays>andthelinkedfilename.
GNUlsdisplayslinkswithadifferentcolor.
33FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Creatingsymboliclinks
Tocreateasymboliclink(sameorderasincp):lnsfile_namelink_name
Tocreatealinkwithtoafileinanotherdirectory,withthesamename:lns../README.txt
Tocreatemultiplelinksatonceinagivendirectory:lnsfile1file2file3...dir
Toremovealink:rmlink_nameOfcourse,thisdoesn'tremovethelinkedfile!
34FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Hardlinks
Thedefaultbehaviorforlnistocreatehardlinks
Ahardlinktoafileisaregularfilewithexactlythesamephysicalcontents
Whiletheystillsavespace,hardlinkscan'tbedistinguishedfromtheoriginalfiles.
Ifyouremovetheoriginalfile,thereisnoimpactonthehardlinkcontents.
Thecontentsareremovedwhentherearenomorefiles(hardlinks)tothem.
35FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Filesnamesandinodes
Makeshardandsymbolic(soft)linkseasiertounderstand!
Inode
File HardlinkSoftlink
Inodeinterface
Filenameinterface
Filesystem
Users
rmrm
Inode
36FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
Commanddocumentation
37FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Commandhelp
SomeUnixcommandsandmostGNU/Linuxcommandsofferatleastonehelpargument:
h(ismostlyusedtointroduce1characteroptions)
help(isalwaysusedtointroducethecorrespondinglongoptionname,whichmakesscriptseasiertounderstand)
Youalsooftengetashortsummaryofoptionswhenyouinputaninvalidargument.
38FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Manualpages
manDisplaysoneorseveralmanualpagesfor
manman
MostavailablemanualpagesareaboutUnixcommands,butsomearealsoaboutCfunctions,headersordatastructures,orevenaboutsystemconfigurationfiles!
manstdio.h
manfstab(for/etc/fstab)
ManualpagefilesarelookedforinthedirectoriesspecifiedbytheMANPATHenvironmentvariable.
39FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Infopages
InGNU,manpagesarebeingreplacedbyinfopages.Somemanualpageseventelltorefertoinfopagesinstead.
info
infofeatures:
Documentationstructuredinsections(nodes)andsubsections(subnodes)
Possibilitytonavigateinthisstructure:top,next,prev,up
InfopagesgeneratedfromthesametexinfosourceastheHTMLdocumentationpages
40FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
SearchingtheInternetforresources(2)
Lookingfordocumentation
Lookfororpagetofindthetoolorprojecthomepageandthenfindthelatestdocumentationresources.
Lookfordocumentationormanualinyourfavoritesearchengine.
Lookingforgenerictechnicalinformation
WikiPedia:http://wikipedia.orgLotsofusefuldefinitionsincomputerscience.Arealencyclopedia!Opentoanyone'scontributions.
41FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
SearchingtheInternetforresources(1)
Investigatingissues
Mostforumsandmailinglistarchivesarepublic,andareindexedonaveryfrequentbasisbyGoogle.
Ifyouinvestigateanerrormessage,copyitverbatiminthesearchform,enclosedindoublequotes(errormessage).Lotsofchancesthatsomebodyelsealreadyfacedthesameissue.
Don'tforgettouseGoogleGroups:http://groups.google.com/Thissiteindexesmorethan20yearsofnewsgroupsmessages.
42FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
Usersandpermissions
43FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Fileaccessrights
3typesofaccessrights
Readaccess(r)
Writeaccess(w)
Executerights(x)
3typesofaccesslevels
User(u):fortheownerofthefile
Group(g):eachfilealsohasagroupattribute,correspondingtoagivenlistofusers
Others(o):forallotherusers
Uselsltocheckfileaccessrights
44FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Accessrightconstraints
xissufficienttoexecutebinariesBothxandrandrequiredforshellscripts.
Bothrandxpermissionsneededinpracticefordirectories:rtolistthecontents,xtoaccessthecontents.
Youcan'trename,remove,copyfilesinadirectoryifyoudon'thavewaccesstothisdirectory.
Ifyouhavewaccesstoadirectory,youCANremoveafileevenifyoudon'thavewriteaccesstothisfile(rememberthatadirectoryisjustafiledescribingalistoffiles).Thisevenletsyoumodify(remove+recreate)afileevenwithoutwaccesstoit.
45FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Accessrightsexamples
rwrrReadableandwritableforfileowner,onlyreadableforothers
rwrReadableandwritableforfileowner,onlyreadableforusersbelongingtothefilegroup.
drwxDirectoryonlyaccessiblebyitsowner
rxFileexecutablebyothersbutneitherbyyourfriendsnorbyyourself.Niceprotectionsforatrap...
46FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
chmod:changingpermissions
chmod2formatsforpermissions:
Octalformat(abc):a,b,c=r*4+w*2+x(r,w,x:booleans)Example:chmod644(rwforu,rforgando)
Orsymbolicformat.Easytounderstandbyexamples:chmodgo+r:addreadpermissionstogroupandothers.chmoduw:removewritepermissionsfromuser.chmodax:(a:all)removeexecutepermissionfromall.
47FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Morechmod(1)
chmodRa+rXlinux/Makeslinuxandeverythinginitavailabletoeveryone!
R:applychangesrecursively
X:x,butonlyfordirectoriesandfilesalreadyexecutableVeryusefultoopenrecursiveaccesstodirectories,withoutaddingexecutionrightstoallfiles.
48FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Morechmod(2)
chmoda+t/tmp
t:(sticky).Specialpermissionfordirectories,allowingonlythedirectoryandfileownertodeleteafileinadirectory.
Usefulfordirectorieswithwriteaccesstoanyone,like/tmp.
Displayedbylslwithatcharacter.
49FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Fileownership
Particularlyusefulin(embedded)systemdevelopmentwhenyoucreatefilesforanothersystem.
chownRsco/home/linux/src(R:recursive)Makesuserscothenewownerofallthefilesin/home/linux/src.
chgrpRempire/home/askywalkerMakesempirethenewgroupofeverythingin/home/askywalker.
chownRborg:aliensusss_entreprise/chowncanbeusedtochangetheownerandgroupatthesametime.
50FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Bewareofthedarksideofroot
rootuserprivilegesareonlyneededforveryspecifictaskswithsecurityrisks:mounting,creatingdevicefiles,loadingdrivers,startingnetworking,changingfileownership,packageupgrades...
Evenifyouhavetherootpassword,yourregularaccountshouldbesufficientfor99.9%ofyourtasks(unlessyouareasystemadministrator).
Inatrainingsession,itisacceptabletouseroot.Inreallife,youmaynotevenhaveaccesstothisaccount,orputyoursystemsanddataatriskifyoudo.
51FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Usingtherootaccount
Incaseyoureallywanttouseroot...
Ifyouhavetherootpassword:su(switchuser)
Inmoderndistributions,thesudocommandgivesyouaccesstosomerootprivilegeswithyourownuserpassword.Example:sudomount/dev/hda4/home
52FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
StandardI/O,redirections,pipes
53FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Standardoutput
Moreaboutcommandoutput
Allthecommandsoutputtingtextonyourterminaldoitbywritingtotheirstandardoutput.
Standardoutputcanbewritten(redirected)toafileusingthe>symbol
Standardoutputcanbeappendedtoanexistingfileusingthe>>symbol
54FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Standardoutputredirectionexamples
ls~saddam/*>~gwb/weapons_mass_destruction.txt
catobiwan_kenobi.txt>starwars_biographies.txtcathan_solo.txt>>starwars_biographies.txt
echoREADME:Nosuchfileordirectory>READMEUsefulwayofcreatingafilewithoutatexteditor.NiceUnixjoketoointhiscase.
55FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Standardinput
Moreaboutcommandinput
Lotsofcommands,whennotgiveninputarguments,cantaketheirinputfromstandardinput.
sortwindowslinux[Ctrl][D]linuxwindows
sort
56FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Pipes
Unixpipesareveryusefultoredirectthestandardoutputofacommandtothestandardinputofanotherone.
Examples
cat*.log|grepierror|sort
greprierror.|grepvignored|sortu\>serious_errors.log
cat/home/*/homework.txt|grepmark|more
ThisoneofthemostpowerfulfeaturesinUnixshells!
57FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Theteecommand
tee[a]file
Theteecommandcanbeusedtosendstandardoutputtothescreenandtoafilesimultaneously.
make|teebuild.logRunsthemakecommandandstoresitsoutputtobuild.log.
makeinstall|teeabuild.logRunsthemakeinstallcommandandappendsitsoutputtobuild.log.
58FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Standarderror
Errormessagesareusuallyoutput(iftheprogramiswellwritten)tostandarderrorinsteadofstandardoutput.
Standarderrorcanberedirectedthrough2>or2>>
Example:catf1f2nofile>newfile2>errfile
Note:1isthedescriptorforstandardoutput,so1>isequivalentto>.
Canredirectbothstandardoutputandstandarderrortothesamefileusing&>:catf1f2nofile&>wholefile
59FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Theyescommand
Usefultofillstandardinputwithalwaysthesamestring.
yes|Keepsfillingthestandardinputofwith(ybydefault).
Examplesyes|rmrdir/bank>yesno|credit_applicantyes""|makeoldconfig(equivalenttohitting[Enter]toacceptalldefaultsettings)
60FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Specialdevices(1)
Devicefileswithaspecialbehaviororcontents
/dev/nullThedatasink!Discardsalldatawrittentothisfile.Usefultogetridofunwantedoutput,typicallyloginformation:mplayerblack_adder_4th.avi&>/dev/null
/dev/zeroReadsfromthisfilealwaysreturn\0charactersUsefultocreateafilefilledwithzeros:ddif=/dev/zeroof=disk.imgbs=1kcount=2048
Seemannullormanzerofordetails
61FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Specialdevices(2)
/dev/randomReturnsrandombyteswhenread.Mainlyusedbycryptographicprograms.Usesinterruptsfromsomedevicedriversassourcesoftruerandomness(entropy).Readscanbeblockeduntilenoughentropyisgathered.
/dev/urandomForprogramsforwhichpseudorandomnumbersarefine.Alwaysgeneratesrandombytes,evenifnotenoughentropyisavailable(inwhichcaseitispossible,thoughstilldifficult,topredictfuturebytesequencesfrompastones).
Seemanrandomfordetails.
62FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Specialdevices(3)
/dev/fullMimicsafulldevice.Usefultocheckthatyourapplicationproperlyhandlesthiskindofsituation.
Seemanfullfordetails.
63FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
Taskcontrol
64FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Fullcontrolontasks
Sincethebeginning,Unixsupportstruepreemptivemultitasking.
Abilitytorunmanytasksinparallel,andabortthemeveniftheycorrupttheirownstateanddata.
Abilitytochoosewhichprogramsyourun.
Abilitytochoosewhichinputyourprogramstakes,andwheretheiroutputgoes.
65FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Processes
EverythinginUnixisafileEverythinginUnixthatisnotafileisaprocess
Processes
Instancesofarunningprograms
Severalinstancesofthesameprogramcanrunatthesametime
Dataassociatedtoprocesses:Openfiles,allocatedmemory,stack,processid,parent,priority,state...
66FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Runningjobsinbackground
Sameusagethroughoutalltheshells
Useful
Forcommandlinejobswhichoutputcanbeexaminedlater,especiallyfortimeconsumingones.
Tostartgraphicalapplicationsfromthecommandlineandthencontinuewiththemouse.
Startingatask:add&attheendofyourline:
find_prince_charmingcutecleverrich&
67FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Backgroundjobcontrol
jobsReturnsthelistofbackgroundjobsfromthesameshell
[1]Running~/bin/find_meaning_of_lifewithoutgod&[2]+Runningmakemistakes&
fgfg%Putsthelast/nthbackgroundjobinforegroundmode
Movingthecurrenttaskinbackgroundmode:[Ctrl]Zbg
kill%Abortsthenthjob.
68FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Jobcontrolexample
>jobs[1]Running~/bin/find_meaning_of_lifewithoutgod&[2]+Runningmakemistakes&
>fgmakemistakes
>[Ctrl]Z[2]+Stoppedmakemistakes
>bg[2]+makemistakes&
>kill%1[1]+Terminated~/bin/find_meaning_of_lifewithoutgod
69FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Listingallprocesses
...whatevershell,scriptorprocesstheyarestartedfrom
psuxListsalltheprocessesbelongingtothecurrentuser
psaux(Note:psedfonSystemVsystems)Listsalltheprocessesrunningonthesystempsaux|grepbart|grepbashUSERPID%CPU%MEMVSZRSSTTYSTATSTARTTIMECOMMANDbart30390.00.259161380pts/2S14:350:00/bin/bashbart31340.00.253881380pts/3S14:360:00/bin/bashbart31900.00.263681360pts/4S14:370:00/bin/bashbart34160.00.000pts/2RW15:070:00[bash]
PID: ProcessidVSZ: Virtualprocesssize(code+data+stack)RSS: Processresidentsize:numberofKBcurrentlyinRAMTTY: TerminalSTAT: Status:R(Runnable),S(Sleep),W(paging),Z(Zombie)...
70FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Liveprocessactivity
topDisplaysmostimportantprocesses,sortedbycpupercentage
top15:44:33up1:11,5users,loadaverage:0.98,0.61,0.59Tasks:81total,5running,76sleeping,0stopped,0zombieCpu(s):92.7%us,5.3%sy,0.0%ni,0.0%id,1.7%wa,0.3%hi,0.0%siMem:515344ktotal,512384kused,2960kfree,20464kbuffersSwap:1044184ktotal,0kused,1044184kfree,277660kcached
PIDUSERPRNIVIRTRESSHRS%CPU%MEMTIME+COMMAND3809jdoe250625639321312R93.80.80:21.49bunzip22769root160157m80m90mR2.716.05:21.01X3006jdoe1503092815m27mS0.33.00:22.40kdeinit3008jdoe16056248924468S0.30.20:06.59autorun3034jdoe1502676412m24mS0.32.50:12.68kscd3810jdoe16028929161620R0.30.20:00.06top
YoucanchangethesortingorderbytypingM:Memoryusage,P:%CPU,T:Time.
Youcankillataskbytypingkandtheprocessid.
71FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Killingprocesses(1)
killSendsanabortsignaltothegivenprocesses.Letsprocessessavedataandexitbythemselves.Shouldbeusedfirst.Example:kill3039313431903416
kill9Sendsanimmediateterminationsignal.Thesystemitselfterminatestheprocesses.Usefulwhenaprocessisreallystuck(doesn'tanswertokill1).
kill91Killsalltheprocessesofthecurrentuser.1:meansallprocesses.
72FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Killingprocesses(2)
killall[]Killsallthejobsrunning.Example:killallbash
xkillLetsyoukillagraphicalapplicationbyclickingonit!Veryquick!Convenientwhenyoudon'tknowtheapplicationcommandname.
73FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Recoveringfromstuckgraphics
Ifyourgraphicalsessionisstuckandyoucannolongertypeinyourterminals,don'treboot!
Itisverylikelythatyoursystemisstillfine.Trytoaccessatextconsolebypressingthe[Ctrl][Alt][F1]keys(or[F2],[F3]formoretextconsoles)
Inthetextconsole,youcantrytokilltheguiltyapplication.
Oncethisisdone,youcangobacktothegraphicsessionbypressing[Ctrl][Alt][F5]or[Ctrl][Alt][F7](dependingonyourdistribution)
Ifyoucan'tidentifythestuckprogram,youcanalsokillallyourprocesses:kill91Youarethenbroughtbacktotheloginscreen.
74FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Sequentialcommands
Cantypethenextcommandinyourterminalevenwhenthecurrentoneisnotover.
Canseparatecommandswiththe;symbol:echoIlovethee;sleep10;echonot
Conditionals:use||(or)or&&(and):moreGod||echoSorry,Goddoesn'texistRunsechoonlyifthefirstcommandfails
ls~sd6&&cat~sd6/*>~sydney/recipes.txtOnlycatsthedirectorycontentsifthelscommandsucceeds(meansreadaccess).
75FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Quoting(1)
Double(")quotescanbeusedtopreventtheshellfrominterpretingspacesasargumentseparators,aswellastopreventfilenamepatternexpansion.
>echo"HelloWorld"HelloWorld
>echo"Youareloggedas$USER"Youareloggedasbgates
>echo*.logfind_prince_charming.logcosmetic_buys.log
>echo"*.log"*.log
76FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Quoting(2)
Singlequotesbringasimilarfunctionality,butwhatisbetweenquotesisneversubstituted
>echo'Youareloggedas$USER'Youareloggedas$USER
Backquotes(`)canbeusedtocallacommandwithinanother
>cd/lib/modules/`unamer`;pwd/lib/modules/2.6.91.6_FC2
Backquotescanbeusedwithindoublequotes
>echo"YouareusingLinux`unamer`"YouareusingLinux2.6.91.6_FC2
77FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Measuringelapsedtime
timefind_expensive_housingnear
real0m2.304s(actualelapsedtime)user0m0.449s(CPUtimerunningprogramcode)sys0m0.106s(CPUtimerunningsystemcalls)
real=user+sys+waitingwaiting=I/Owaitingtime+idletime(runningothertasks)
78FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Environmentvariables
Shellslettheuserdefinevariables.Theycanbereusedinshellcommands.Convention:lowercasenames
Youcanalsodefineenvironmentvariables:variablesthatarealsovisiblewithinscriptsorexecutablescalledfromtheshell.Convention:uppercasenames.
envListsalldefinedenvironmentvariablesandtheirvalue.
79FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Shellvariablesexamples
Shellvariables(bash)
projdir=/home/marshall/coolstufflsla$projdir;cd$projdir
Environmentvariables(bash)
cd$HOME
exportDEBUG=1./find_extraterrestrial_life(displaysdebuginformationifDEBUGisset)
80FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Mainstandardenvironmentvariables
LD_LIBRARY_PATH
SharedlibrarysearchpathDISPLAY
ScreenidtodisplayX(graphical)applicationson.EDITOR
Defaulteditor(vi,emacs...)
HOMECurrentuserhomedirectory
HOSTNAMENameofthelocalmachine
MANPATHManualpagesearchpath
PATHCommandsearchpath
PRINTER
Defaultprintername
SHELLCurrentshellname
TERMCurrentterminaltype
USER
Currentusername
Usedbylotsofapplications!
81FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
PATHenvironmentvariables
PATHSpecifiestheshellsearchorderforcommands
/home/acox/bin:/usr/local/bin:/usr/kerberos/bin:/usr/bin:/bin:/usr/X11R6/bin:/bin:/usr/bin
LD_LIBRARY_PATHSpecifiesthesharedlibrary(binarycodelibrariessharedbyapplications,liketheClibrary)searchorderforld
/usr/local/lib:/usr/lib:/lib:/usr/X11R6/lib
MANPATHSpecifiesthesearchorderformanualpages
/usr/local/man:/usr/share/man
82FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
PATHusagewarning
Itisstronglyrecommendednottohavethe.directoryinyourPATHenvironmentvariable,inparticularnotatthebeginning:
Acrackercouldplaceamaliciouslsfileinyourdirectories.Itwouldgetexecutedwhenyourunlsinthisdirectoryandcoulddonaughtythingstoyourdata.
Ifyouhaveanexecutablefilecalledtestinadirectory,thiswilloverridethedefaulttestprogramandsomescriptswillstopworkingproperly.
Eachtimeyoucdtoanewdirectory,theshellwillwastetimeupdatingitslistofavailablecommands.
Callyourlocalcommandsasfollows:./test
83FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Alias
Shellsletyoudefinecommandaliases:shortcutsforcommandsyouuseveryfrequently.
Examples
aliasls='lsla'Usefultoalwaysruncommandswithdefaultarguments.
aliasrm='rmi'Usefultomakermalwaysaskforconfirmation.
aliasfrd='find_rambaldi_deviceasaprisky'Usefultoreplaceverylongandfrequentcommands.
aliascia='./home/sydney/env/cia.sh'Usefultosetanenvironmentinaquickway(.isashellcommandtoexecutethecontentofashellscript).
84FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thewhichcommand
Beforeyourunacommand,whichtellsyouwhereitisfound
bash>whichlsaliasls='lscolor=tty'/bin/ls
tcsh>whichlsls:aliasedtolscolor=tty
bash>whichalias/usr/bin/which:noaliasin(/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin)
tcsh>whichaliasalias:shellbuiltincommand.
85FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
~/.bashrcfile
~/.bashrcShellscriptreadeachtimeabashshellisstarted
Youcanusethisfiletodefine
Yourdefaultenvironmentvariables(PATH,EDITOR...).
Youraliases.
Yourprompt(seethebashmanualfordetails).
Agreetingmessage.
86FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Commandediting
Youcanusetheleftandrightarrowkeystomovethecursorinthecurrentcommand.
Youcanuse[Ctrl][a]togotothebeginningoftheline,and[Ctrl][e]togototheend.
Youcanusetheupanddownarrowstoselectearliercommands.
Youcanuse[Ctrl][r]tosearchinsidethehistoryofpreviouscommands.
87FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Commandhistory(1)
historyDisplaysthelatestcommandsthatyouranandtheirnumber.Youcancopyandpastecommandstrings.
Youcanrecallthelatestcommand:!!
Youcanrecallacommandbyitsnumber!1003
Youcanrecallthelatestcommandmatchingastartingstring:!cat
88FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Commandhistory(2)
Youcanmakesubstitutionsonthelatestcommand:^more^less
Youcanrunanothercommandwiththesamearguments:more!*
89FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
MiscellaneousTexteditors
90FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Texteditors
GraphicaltexteditorsFineformostneeds
nedit
Emacs,Xemacs
Kate,Gedit
TextonlytexteditorsOftenneededforsysadminsandgreatforpowerusers
vi,vim
nano
91FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thenedittexteditor
http://www.nedit.org/
Besttexteditorfornonvioremacsexperts
Featurehighlights:
Veryeasytextselectionandmoving
Syntaxhighlightingformostlanguagesandformats.Canbetailoredforyourownlogfiles,tohighlightparticularerrorsandwarnings.
Easytocustomizethroughmenus
Notinstalledbydefaultbyalldistributions
92FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
neditscreenshot
93FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Emacs/Xemacs
EmacsandXemacsareprettysimilar(uptoyourpreference)
Extremelypowerfultexteditorfeatures
Greatforpowerusers
Lessergonomicthannedit
Nonstandardshortcuts
Muchmorethanatexteditor(games,email,shell,browser).
Somepowercommandshavetobelearnt.
94FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Kateandgedit
Kateisapowerfultexteditordedicatedtoprogrammingactivities,forKDE
http://kate.kde.org
GeditisatexteditorfortheGnomeenvironment
http://projects.gnome.org/gedit/
95FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
vi
TextmodetexteditoravailableinallUnixsystems.Createdbeforecomputerswithmiceappeared.
Difficulttolearnforbeginnersusedtographicaltexteditors.
Veryproductiveforpowerusers.
Oftencan'tbereplacedtoeditfilesinsystemadministrationorinEmbeddedSystems,whenyoujusthaveatextconsole.
96FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
vimviimproved
viimplementationnowfoundinmostGNU/Linuxhostsystems
Implementslotsoffeaturesavailableinmoderneditors:syntaxhighlighting,commandhistory,help,unlimitedundoandmuchmuchmore.
Coolfeatureexample:candirectlyopencompressedtextfiles.
ComeswithaGTKgraphicalinterface(gvim)
Unfortunately,notfreesoftware(becauseofasmallrestrictioninfreedomtomakechanges)
97FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
vibasiccommands
Thoughviisextremelypowerful,itsmain30commandsareeasytolearnandaresufficientfor99%ofeveryone'sneeds!
Youcanalsotakethequicktutorialbyrunningvimtutor.
Getourvimementosheetifyoudidn'tgetitwiththiscourse:http://freeelectrons.com/docs/commandline
98FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
GNUnano
http://www.nanoeditor.org/
Anothersmalltextonly,mousefreetexteditor.
AnenhancedPicoclone(nonfreeeditorinPine)
Friendlyandeasiertolearnforbeginnersthankstoonscreencommandsummaries.
Availableinbinarypackagesforseveralplatforms.
Analternativetoviinembeddedsystems.However,notavailableasabusyboxbuiltin.
99FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
GNUnanoscreenshot
100FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
MiscellaneousCompressingandarchiving
101FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Measuringdiskusage
Caution:differentfromfilesize!
duh(diskusage)h:returnssizeondiskofthegivenfile,inhumanreadableformat:K(kilobytes),M(megabytes)orG(gigabytes),.Withouth,dureturnstherawnumberofdiskblocksusedbythefile(hardtoread).NotethatthehoptiononlyexistsinGNUdu.
dushs:returnsthesumofdiskusageofallthefilesinthegivendirectory.
102FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Measuringdiskspace
dfhReturnsdiskusageandfreespaceforthefilesystemcontainingthegivendirectory.Similarly,thehoptiononlyexistsinGNUdf.
Example:>dfh.FilesystemSizeUsedAvailUse%Mountedon/dev/hda59.2G7.1G1.8G81%/
dfhReturnsdiskspaceinformationforallfilesystemsavailableinthesystem.Whenerrorshappen,usefultolookforfullfilesystems.
103FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Compressinganddecompressing
Veryusefulforshrinkinghugefilesandsavingspace
g[un]zipGNUzipcompressionutility.Creates.gzfiles.Ordinaryperformance(similartoZip).
b[un]zip2Morerecentandeffectivecompressionutility.Creates.bz2files.Usually2025%betterthangzip.
[un]lzmaMuchbettercompressionratiothanbzip2(upto10to20%).Compatiblecommandlineoptions.
104FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Archiving(1)
Usefultobackuporreleaseasetoffileswithin1file
tar:originallytapearchive
Creatinganarchive:tarcvfc:createv:verbose.Usefultofollowarchivingprogress.f:file.Archivecreatedinfile(tapeusedotherwise).
Example:tarcvf/backup/home.tar/homebzip2/backup/home.tar
105FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Archiving(2)
Viewingthecontentsofanarchiveorintegritycheck:tartvft:test
Extractingallthefilesfromanarchive:tarxvf
Extractingjustafewfilesfromanarchive:tarxvfFilesordirectoriesaregivenwithpathsrelativetothearchiverootdirectory.
106FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
ExtraoptionsinGNUtar
tar=gtar=GNUtaronGNU/LinuxCancompressanduncompressarchivesonthefly.UsefultoavoidcreatinghugeintermediatefilesMuchsimplertodothanwithtarandbzip2!
joption:[un]compressesontheflywithbzip2
zoption:[un]compressesontheflywithgzip
lzmaoption:[un]compressesontheflywithlzma
Examples(whichonewillyouremember?)gtarjcvfbills_bugs.tar.bz2bills_bugs
tarcvfbills_bugs|bzip2>bills_bugs.tar.bz2
107FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Checkingfileintegrity
Verylowcostsolutiontocheckfileintegrity
md5sumFC3i386disk*.iso>MD5SUMComputesaMD5(MessageDigestAlgorithm5)128bitchecksumofthegivenfiles.Usuallyredirectedtoafile.
Exampleoutput:db8c7254beeb4f6b891d1ed3f689b412FC3i386disc1.iso2c11674cf429fe570445afd9d5ff564eFC3i386disc2.isof88f6ab5947ca41f3cf31db04487279bFC3i386disc3.iso6331c00aa3e8c088cc365eeb7ef230eaFC3i386disc4.iso
md5sumcMD5SUMCheckstheintegrityofthefilesinMD5SUMbycomparingtheiractualMD5checksumwiththeiroriginalone.
108FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
MiscellaneousPrinting
109FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Unixprinting
Multiuser,multijob,multiclient,multiprinterInUnix/Linux,printingcommandsdon'treallyprint.Theysendjobstoprintingqueues,possiblyonthelocalmachine,onnetworkprintingserversoronnetworkprinters.
Printerindependentsystem:PrintserversonlyacceptjobsinPostScriptortext.Printerdriversontheservertakecareoftheconversiontoeachprintersownformat.
Robustsystem:Rebootasystem,itwillcontinuetoprintpendingjobs.
110FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Printingcommands
Usefulenvironmentvariable:PRINTERSetsthedefaultprinteronthesystem.Example:exportPRINTER=lp
lpr[P]SendsthegivenfilestothespecifiedprintingqueueThefilesmustbeintextorPostScriptformat.Otherwise,youonlyprintgarbage.
a2ps[P]AnytoPostScriptconvertsmanyformatstoPostScriptandsendtheoutputtothespecifiedqueue.Usefulfeatures:severalpages/sheet,pagenumbering,infoframe...
111FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Printjobcontrol
lpq[P]Listsalltheprintjobsinthegivenordefaultqueue.
lpisnotreadyRankOwnerJobFile(s)TotalSize1stasloane84nsa_windows_backdoors.ps60416bytes2ndamoore85gw_bush_iraq_mistakes.ps65024000bytes
cancel[]Removesthegivenjobnumberfromthedefaultqueue.
112FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
UsingPostScriptandPDFfiles
ViewingaPostScriptfile
PostScriptviewersexist,buttheirqualityisprettypoor.
BetterconverttoPDFwithps2pdf:ps2pdfdecss_algorithm.psxpdfdecss_algorithm.pdf&
PrintingaPDFfile
Youdon'tneedtoopenaPDFreader!
BetterconverttoPostScriptwithpdf2ps:pdf2psrambaldi_artifacts_for_dummies.pdflprrambaldi_artifacts_for_dummies.ps
113FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
MiscellaneousSynchronizingfiles
114FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Smartdirectorycopywithrsync
rsync(remotesync)hasbeendesignedtokeepinsyncdirectorieson2machineswithalowbandwidthconnection.
Onlycopiesfilesthathavechanged.Fileswiththesamesizearecomparedbychecksums.
Onlytransferstheblocksthatdifferwithinafile!
Cancompressthetransferredblocks
Preservessymboliclinksandfilepermissions:alsoveryusefulforcopiesonthesamemachine.
Canworkthroughssh(secureremoteshell).Veryusefultoupdatethecontentsofawebsite,forexample.
115FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
rsyncexamples(1)
rsynca/home/arvin/sd6_agents//home/sydney/misc/
a:archivemode.EquivalenttorlptgoD...easywaytotellyouwantrecursionandwanttopreservealmosteverything.
rsyncPavdelete/home/steve/ideas//home/bill/my_ideas/
P:partial(keeppartiallytransferredfiles)andprogress(showprogressduringtransfer)
delete:deletefilesinthetargetwhichdon'texistinthesource.
Caution:directorynamesshouldendwith/.Otherwise,yougetamy_ideas/ideas/directoryatthedestination.
116FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
rsyncexamples(2)
Copyingtoaremotemachine
rsyncPav/home/bill/legal/arguments/\[email protected]:/home/legal/arguments/
Userbillwillbepromptedforapassword.
Copyingfromaremotemachinethroughssh
[email protected]:/prod/beer/\fridge/homer/beer/
Userhomerwillbepromptedforhissshkeypassword.
117FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
MiscellaneousComparingfilesanddirectories
118FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Comparingfilesanddirectories
difffile1file2Reportsthedifferencesbetween2files,ornothingifthefilesareidentical.
diffrdir1/dir2/Reportsallthedifferencesbetweenfileswiththesamenameinthe2directories.
Thesedifferencescanbesavedinafileusingtheredirection,andthenlaterreappliedusingthepatchcommand.
Toinvestigatedifferencesindetail,betterusegraphicaltools!
119FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
tkdiff
http://tkdiff.sourceforge.net/Usefultooltocomparefilesandmergedifferences
120FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
kompare
AnothernicetooltocomparefilesandmergedifferencesPartofthekdesdkpackage(FedoraCore)
121FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
gvimdiff
Anothernicetooltoviewdifferencesinfiles
AvailableinmostdistributionswithgvimApparentlynotusingdiff.Noissuewithfileswithbinarysections!
122FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
MiscellaneousLookingforfiles
123FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thefindcommand
Betterexplainedbyafewexamples!
find.name*.pdfListsallthe*.pdffilesinthecurrent(.)directoryorsubdirectories.Youneedthedoublequotestopreventtheshellfromexpandingthe*character.
finddocsname"*.pdf"execxpdf{}';'Findsallthe*.pdffilesinthedocsdirectoryanddisplaysoneaftertheother.
Manymorepossibilitiesavailable!However,theabove2examplescovermostneeds.
124FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thelocatecommand
Muchfasterregularexpressionsearchalternativetofind
locatekeysListsallthefilesonyoursystemwithkeysintheirname.
locate*.pdfListsallthe*.pdffilesavailableonthewholemachine
locate/home/fridge/*beer*Listsallthe*beer*filesinthegivendirectory(absolutepath)
locateismuchfasterbecauseitindexesallfilesinadedicateddatabase,whichisupdatedonaregularbasis.
findisbettertosearchthroughrecentlycreatedfiles.
125FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
MiscellaneousVariouscommands
126FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Gettinginformationaboutusers
whoListsalltheusersloggedonthesystem.
whoamiTellswhatuserIamloggedas.
groupsTellswhichgroupsIbelongto.
groupsTellswhichgroupsbelongsto.
fingerTellsmoredetails(realname,etc)aboutDisabledinsomesystems(securityreasons).
127FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Changingusers
Youdonothavetologouttologonanotheruseraccount!
suhyde(Rare)Changetothehydeaccount,butkeepingtheenvironmentvariablesettingsoftheoriginaluser.
sujekyll(Morefrequent)Logonthejekyllaccount,withexactlythesamesettingsasthisnewuser.
suWhennoargumentisgiven,itmeanstherootuser.
128FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Thewgetcommand
Insteadofdownloadingfilesfromyourbrowser,justcopyandpastetheirURLanddownloadthemwithwget!
wgetmainfeatures
httpandftpsupport
Canresumeinterrupteddownloads
Candownloadentiresitesoratleastcheckforbadlinks
Veryusefulinscriptsorwhennographicsareavailable(systemadministration,embeddedsystems)
Proxysupport(http_proxyandftp_proxyenv.variables)
129FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
wgetexamples
wgetc\http://microsoft.com/customers/dogs/winxp4dogs.zipContinuesaninterrupteddownload.
wgetmhttp://lwn.net/Mirrorsasite.
wgetrnphttp://www.xml.com/ldd/chapter/book/Recursivelydownloadsanonlinebookforofflineaccess.np:"noparent".Onlyfollowslinksinthecurrentdirectory.
130FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Misccommands(1)
sleep60Waitsfor60seconds(doesn'tconsumesystemresources).
wcreport.txt(wordcount)438211518302report.txtCountsthenumberoflines,wordsandcharactersinafileorinstandardinput.
131FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Misccommands(2)
bc("basiccalculator?")bcisahandybutfullfeaturedcalculator.Evenincludesaprogramminglanguage!Usetheloptiontohavefloatingpointsupport.
dateReturnsthecurrentdate.Usefulinscriptstorecordwhencommandsstartedorcompleted.
132FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Checksumcommands
Achecksumorhashsumisafixedsizedatumcomputedfromanarbitraryblockofdigitaldataforthepurposeofdetectingaccidentalerrorsthatmayhavebeenintroducedduringitstransmissionsorstorage.http://en.wikipedia.org/wiki/Checksum
TheMD5hashalgorithmisimplementedinthemd5sumcommand$md5sumpatch2.6.24.7.bz20c1c5d6d8cd82e18d62406d2f34d1d38patch2.6.24.7.bz2
TheSHAalgorithmisimplementedintheshaXsum(sha1sum,sha256sum,etc.)
Theintegrityofseveralfilescanbeverifiedagainstafilelistingthechecksumsusingthecoption.
133FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Systemadministration
Seeourpresentationaboutsystemadministrationbasics:
Networksetup
Creatingandmountingfilesystems
Accessingadministrator(root)priviledges
Packagemanagement
Alsoavailableonhttp://freeelectrons.com/docs/commandline
134FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
Applicationdevelopment
135FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Compilingsimpleapplications
ThecompilerusedforallLinuxsystemsisGCChttp://gcc.gnu.org
Tocompileasinglefileapplication,developedinC:gccotesttest.c
Willgenerateatestbinary,fromthetest.csourcefile
ForC++:g++otesttest.cc
TheWalloptionenablesmorewarnings
Tocompilesourcesfilestoobjectfilesandlinktheapplication:gccctest1.cgccctest2.cgccotesttest1.otest2.o
gccautomaticallycallsthelinkerld
136FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Usinglibraries(1)
OnanyLinuxsystem,aClibraryisavailableandoffersalargesetofAPIsforapplicationdevelopment.
Seehttp://www.gnu.org/software/libc/manual/
OutsideoftheClibrary,thousandsofotherlibrariesareavailableforgraphicprogramming,multimedia,networking,scientificcomputations,andmoroe.
Mostlibrariesarealreadyavailableaspackagesinyourdistribution,ingeneralintwopackages
libfooisthepackagecontainingthelibraryitself.Thispackageisrequiredtoexecutealreadycompiledapplications,butnotsufficienttobuildnewapplications
libfoodevisthepackagecontainingtheheadersandotherconfigurationsfilesandtoolsneededtobuildnewapplicationsrelyingonlibfoo.
137FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Usinglibraries(2)
Inyoursourcecode,includetheproperheaderfilesofthelibrary
Usually#includeor#include
Theseheadersarepresentin/usr/include/
Refertothedocumentationofthelibraryfordetails,availablein/usr/share/doc//,ontheWeb...orintheheaderfiles!
Tocompileyourapplicationwiththelibrary,theeasiestsolutionistousepkgconfig,whichissupportedbymostlibrariestoday:gccotesttest.c$(pkgconfigcflagslibs)
Bydefault,theapplicationaredynamicallylinkedwiththelibraries
Thelibrariesmustbepresentin/lib/fortheapplicationtowork
Usethelddcommandtoseewhichlibrariesareneededbyanapplication
138FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
MakeandMakefiles
Thecompilationprocesscanbeautomatedusingthemaketool.
makereadsafilecalledMakefilefromthecurrentdirectory,andexecutestherulesdescribedinthisfile
Everyruleishasatargetname,acolon,andalistofdependencies,andthelistofcommandstogeneratethetargetfromthedependenciestarget:dep1dep2
command1command2command3
Whensimplyrunningmake,thedefaulttargetthatisgeneratedisall.Atargetisonlyregeneratedifdependencieshavechanged.
Seehttp://www.gnu.org/software/make/manual/
139FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
140FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
SimpleMakefileexample
CFLAGS=Wall
all:test
test:t1.ot2.ogcco$@$^$(CFLAGS)
clean:$(RM)ftest
install:$(CP)test/usr/bin
Variablescanbedefinedandlaterexpanededwith$(VARNAME)
Thedefaulttargetallsimplydependsonthetesttarget.
Thetesttargetdependsont1.oandt2.o.Oncethesefilesaregenerated,thegcccommandisexecuted.$@isthetargetname$^isthenameofalldependencies.The.ofilesaregeneratedusingimplicitdependencies,knownbymake.
Thesetargetsareexecutedbyrunningmakecleanandmakeinstall
141FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Buildsystems
Makefilesarenice,buttheydon'teasilyalloweasyadaptationtothedifferentbuildenvironmentanddifferentbuildoptions
Moreelaboratedbuildsystemshavebeendeveloped
Autotools(automake,autoconf),basedonMakefilesandshellscripts.Eventhoughtheyareoldandalittlebitdifficulttounderstand,theyarethemostpopularbuildsystemforfreesoftwarepackages.
CMake,anewer,cleanerbuildsystem
SconcsandWaf,otherbuildsystemsbasedonPython
Thetypicalstepstocompileaautotoolsbasedpackageare./configuremakesudomakeinstall
142FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Debugging
TheofficialdebuggerthatcomeswiththeGNUdevelopmenttoolsisgdb.
Seehttp://sourceware.org/gdb/download/onlinedocs/gdb.html
Anapplicationmustbecompiledwiththegoptiontobeproperlydebugged.Thisoptionaddsdebugginginformationtotheapplicationbinarygccotesttest.cg
Theapplicationcanthenberuninsidethegdbdebugger:gdbtest
Orthedebuggercanbeattachedtotheapplicationwhileitisrunning:gdbtestpPID
WherePIDistheprocessIDoftherunningapplication
143FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Usinggdb
gdbisatextbaseddebugger,withacommandlineinterfacelikeashell,providingdedicatedcommands.Someoftheimportantcommandsare:
break(b)tosetabreakpointinthecode.Canbeusedwithafunctionnameoralocationinthesourcecode,oranabsolutememoryaddress.
print(p)toprintthevalueofavariable.Usedwithavariablename,evenifit'sacomplexone(whichinvolvesdereferencingstructures,forexample)
ctocontinuetheexecutionuntilthenextbreakpoint.
next(n)toexecuteonlythenextlineofcode(stepoveranyfunctioncall)andstep(s)toexecuteonlythenextlineofcode(stepintoanyfunctioncall)
backtrace(bt)todisplaythefunctioncallstack
144FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
gdbsamplesession
thomas@surf:/tmp$gccotesttest.cgthomas@surf:/tmp$gdbtestGNUgdb6.8debian[...)(gdb)breakfooBreakpoint1at0x80483c7:filetest.c,line5.(gdb)runStartingprogram:/tmp/test2
Breakpoint1,foo(a=2,b=3)attest.c:55 returna+b;(gdb)pa$1=2(gdb)pb$2=3(gdb)cContinuing.foo=5
Programexitednormally.
145FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
ddd,thegraphicalgdb
Thereareseveralgraphicalfrontendstogdb.Apopularoneisddd.
Allowstonavigateinthesourcecodewhiledebugging
Allowstosetbreakpoints,inspectandchangevariablevaluedirectlybylookingatthesourcecode
http://www.gnu.org/software/ddd/
146FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
TheUnixandGNU/Linuxcommandline
VersioncontrolsystemCVS
147FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Versioncontrol(1)
Activitythatconsistsinmaintainingthefullhistoryofprojectfiles
Everyversionofeveryfileisrecorded
Itallowsto:
Createaknowledgedatabaseabouttheprojectcodebase
Revertincaseofmistake
Reviewthemodificationsintroducedbetweentwodifferentversions
Workasateamonasingleproject
Createparalleldevelopmentorreleasebranches
Tagpreviousversionsofaproject
148FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Versioncontrol(2)
Usefulfor:
Sourcecode,ofcourse
Documentation,translationstrings,compilationscripts,configurationfiles,etc.
Binarydatacanbeversioned,butthefunctionalitieswillbelimited.Besttousetextonlyfileformats.
Generatedfilesshouldneverbeversioned.
Versioncontrolisabsolutelynecessarywhenworkingasateamonaproject
Versioncontrolsystemsareoneofthefundamentaltoolsusedforopensourcedevelopment
Versioncontrolisstillveryusefulwhenworkingaloneonaproject.
149FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Toolsandapproaches
Alotofversioncontrolsystemsavailable
Proprietary:Perforce,Synergy,StarTeam,BitKeeper,ClearCase
Freeandopensource:CVS,Subversion,Git,Mercurial,Arch,Monotone
Inthefree/opensolutions,twomainapproaches
Theclassicalone,thecentralizedapproach,implementedbyCVSandSubversion
Anewerone,thedecentralizedordistributedapproach,implementedinGit,Mercurial,ArchorMonotone.
150FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Centralizedapproach
Thecentralizedapproachisbasedonthepresenceofaserverhostingarepository.
Therepositorycontainsthehistoryofallfilesandactsasthereferenceforallparticipantsintheproject.
Typicalclient/serverapproach.
Repository
Client Client
151FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Conflictmanagement(1)
Whentwopersonsareworkingatthesametimeonthesamefile,whenthechangesaresenttotheserver,aconflictcanoccurifthemodificationsarenotcompatible.
A
A A
Linus Richard
A' A''
RepositoryA'A''
152FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Conflictmanagement(2)
Twosolutionstosolvetheconflictproblem
Thelockingapproach:preventingtwodeveloperstoworkonthesamefile.
Themergeapproach:theseconddevelopersendinghischangestotheserverisresponsibleformerginghischangeswiththeotherdeveloperschangesalreadypresentontheserver.
Inthefree/opensourceprojects,thetraditionalmodelisthemergeone.Itavoidstheburdenofhandlingthelockingandscalesbetterwithabiggerteam.
ButCVSandSubversionbothimplementadvisorylocking:adevelopermustvoluntarilylockthefilesheisgoingtoworkwith.
153FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Conflictresolutionbyfusion
A
A
Linus Richard
AA' A''
A''
A''A*
A*
A*
154FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
CVS
FreeSoftware
Usedtobethemostwidelyusedfreeversioncontrolsystem.NowreplacedbySubversion.
Stillusedbybigprojects,butalotoflargeprojects(KDE,Apache,Eclipse)havemovedtoSubversion.
Homepage:http://www.nongnu.org/cvs/
Documentation:http://ximbiot.com/cvs/manual/
155FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
CVSusage
Themainclientisacommandlineone,implementedinthecvscommand
cvshelpgivesthelistofavailablecommands
add,admin,annotate,checkout,commit,diff,edit,editors,export,history,import,init,log,login,logout,ls,pserver,rannotate,rdiff,release,remove,rlog,rls,rtag,server,status,tag,unedit,update,version,watch,watchers
cvshelpcommandgivesthehelpofcommand.
Graphicalinterfaces
IntegrationinVim,Emacs,Anjuta,DevC++,Eclipse,Kdevelop,etc.
Graphicalclients:Cervisia(forKDE),gcvs(forGnome),CrossVC,etc.
Webclients:ViewVC, cvsweb,etc.
156FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Createtheworkingcopy
Thefirststeptoworkonanexistingprojectistocreateaworkingcopy.Aworkingcopyisacopyonthedevelopermachineofthefulltreeoftheprojectsourcecode.
Thedeveloperwillworkonthiscopyofthesourcecode,asusual(filesaredirectlyaccessibleandeditable).
cvsdrepositoryaddresscheckoutmodule
Createinthecurrentdirectoryaworkingcopyofthemodulemodulelocatedinthegivenrepository
Arepositoryaddresscanbe:
Alocalpath,like:local:/home/user/cvsrepo/
TheaddressofaCVSpserver,like:pserver:user@host:/var/cvsrepo/
UsingSSH,bysettingtheenvironmentvariableCVS_RSHtossh,andusinganaddresslike:ext:user@host:/var/cvsrepo/
157FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Simpleusage
Workingcopycreationtobedoneonceforall!
Modificationsoftheprojectfiles
Filesaremodifieddirectly,asusual,nothingspecialisnecessary.CVSknowsthecontentsoftheoriginalversionofthefilesandisabletocomputethelistofmodificationsmadebythedeveloper.
EverydirectorycontainsaCVSdirectorythatshouldnotbemodified.
Sendthemodificationstotheserver:commit
cvscommit
Runsatexteditortowritethemessagethatdescribesthecommit.Thismessagewillbepreservedforeverinthehistory.Veryimportanttodescribeyourchangeindetails!
Fetchthemodificationsmadebyotherdevelopers:update
cvsupdate
158FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Example
$cvsd:local:/tmp/repo/checkoutprojectcvscheckout:UpdatingprojectUproject/READMEUproject/a.cUproject/b.c$cdproject/project$viREADMEproject$cvscommitm"Addinginfos"cvscommit:Examining./tmp/repo/project/README,v
159FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Filemanagement
Whennewfilesordirectoriesarecreatedinsidetheproject,theyarenotautomaticallytakenintoaccountbyCVS
Theymustbeexplicitlyaddedwiththeaddcommand
cvsaddfile1.c
Thenewfileisnotpropagatedtotherepositoryuntilthenextcommit
Samethingwithdirectories
Filesanddirectoriescanberemovedwiththeremovecommand
IssuesfixedinSubversion
Filesanddirectoriescannotberenamedwithoutlosingthehistory
DirectoryremovalisbadlyhandledinCVS
160FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Example
project$vic.cproject$cvsaddc.ccvsadd:schedulingfile`c.c'foradditioncvsadd:use`cvscommit'toaddthisfilepermanentlyproject$cvscommitm"Newfilec.c"cvscommit:Examining./tmp/repo/project/c.c,v
161FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Stateoftheworkingcopy
Togetinformationsaboutthestateoftheworking,usethestatuscommand
Willtelltheversionofthefile,iftheyhavebeenlocallymodified,etc.
Toseethemodificationsmadetotheprojectandnotcommittedyet,usethediffcommand
Repository
Workingcopydiff
checkout update
162FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Diffcommandexample
project$cvsdiffcvsdiff:Diffing.Index:c.c========================================================RCSfile:/tmp/repo/project/c.c,vretrievingrevision1.1diffur1.1c.cc.c 28Jan200916:48:110000 1.1+++c.c 28Jan200916:50:410000@@1+1,2@@Test.+Test2.
163FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Usingthehistory
Seethecommitmessages
cvslog
cvslogfile.c
Seethedifferencesbetweentwopastrevisions
cvsdiffr1.1r1.2file.c
WithCVS,theversioningisdoneonaperfilebasis:acommitisnotanentitythatcanbereferredtooncecompleted.Itmakestheusageofthehistoryverycomplicated.ThisisfixedinSubversion.
Seewhomadeachange
cvsannotate
Agraphicalinterface,eitheragraphicalclientorawebclient,willbeveryusefultonavigateandusethehistoryefficiently
164FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
ConflictmanagementinCVS(1)
Afterchangingafile,wetrytocommitthechangetotheserver$cvscommitm"Modification"cvscommit:Examining.cvscommit:Uptodatecheckfailedfor`main.c'cvs[commitaborted]:correctaboveerrorsfirst!
Thefilehasbeenmodifiedontheserversinceourlastupdate.Sowemustmakemergethemodificationswehavedonewiththemodificationsoftheotherdevelopers,byupdatingourworkingcopy.$cvsupdatecvsupdate:Updating.RCSfile:/tmp/repo/project/main.c,vretrievingrevision1.1retrievingrevision1.2Mergingdifferencesbetween1.1and1.2intomain.crcsmerge:warning:conflictsduringmergecvsupdate:conflictsfoundinmain.cCmain.c
165FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
ConflictmanagementinCVS(2)
Insidemain.c
#include
intmain(void){1.2return0;}
Ourmodification!
Themodificationmadebytheotherdeveloper
166FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
ConflictresolutioninCVS
Theconflictmustbemanuallyresolvedbythedeveloper,noautomatedtoolcanresolvesuchconflicts
Theconflictisresolveddirectlyinthefilebyremovingthemarkersandmergingthemodifications(eitherselectingoneofthemorcreatinganewversionbasedonbothmodifications)
Onceresolved,thefilecanbecommittedasusual
project$cvscommitm"InFrench"cvscommit:Examining./tmp/repo/project/main.c,v
167FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Repositoryinitialization
Arepositorymustbeinitializedusingtheinitcommand:
mkdir/home/user/cvsrepo
cvsd:local:/home/user/cvsrepoinit
Onceinitialized,itcanbeaccessedlocallyorremotelythroughSSH.Remoteaccessthroughpserverwillrequireadditionalconfiguration,seetheCVSdocumentation.
Ifalreadyexistingprojectshavetobeimportedintherepository,usetheimportcommand
cdproject
cvsd:local:/home/user/cvsrepoimportmodulenamevendortagreleasetag
vendortag,symbolicnameforthebranch
releasetag,symbolicnamefortherelease
168FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Tagsandbranches
Tagsallowtoidentifyagivenversionoftheprojectthroughameaningfulsymbolicname
Usefulforexampleiftheversionhasbeendeliveredtothetestteamortoacustomer.
Tagsarecreatedusingthecvstagcommand.
Branchesallowtocreateparallelflowofdevelopments
Maintenanceofapreviousrelease
Developmentofexperimentalnewfeatures
Createdusingtheboptionofthecvstagcommand
Branchingandmergingisrelativelycomplicated,andfallsoutsidethescopeofthistraining.
169FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Connectionwithothertools
Versioncontrol
BugtrackingBuildandtest
IRC
Sendamailateverycommit,withthediff.Allowotherstoreview,andnotifiesthemofthechanges..
Sendamessageateverycommit.Notifiesofthechanges.
Ateverycommit,abuildandtestmachinbuildsandteststheproject.
Linkthecommitswiththebugsandthebugswiththecommits.
170FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
WhySubversion?
BecauseCVShasmanydrawbacksandshortcoming
Notpossibletosimplyrenamefilesanddirectorieswhilepreservingthehistory
Noatomiccommitsidentifyingthecommitasawhole,makingitdifficulttonavigateinthehistory,revertcommits,andisthesourceofrepositoryincoherencyincaseofcrashes
Poorbranchingandmergingcapabilities
Inusage,SubversionisverysimilartoCVS:thecommandsareexactlythesame.
FixesallCVSshortcomings
Andprovidesinterestingbranchingandmergingfeaturessuchasmergetracking.
Manylargescaleprojectsalreadymadetheswitch.
171FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Distributedversioncontrolsystems
Sinceafewyears,anewgenerationofversioncontrolsystems
Distributedversioncontrolinsteadofacentralizedapproach
Principles
Notechnicallycentralrepository,everycopyisarepository
Alldeveloperscancreatelocalbranches,sharethesebrancheswithotherdeveloperswithoutaskingacentralauthority.
Advancedbranchingandmergingcapabilities.
Moreandmorecommonlyusedinfreesoftwareprojects(Linuxkernel,X.org,etc.)
Mostcommonlyusedtools:Git,Mercurial
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Relateddocuments
Allourtechnicalpresentationsonhttp://freeelectrons.com/docs
LinuxkernelDevicedriversArchitecturespecificsEmbeddedLinuxsystemdevelopment
FreeElectrons.Kernel,driversandembeddedLinuxdevelopment,consulting,trainingandsupport.http//freeelectrons.com
Howtohelp
Youcanhelpustoimproveandmaintainthisdocument...
Bysendingcorrections,suggestions,contributionsandtranslations
Byaskingyourorganizationtoorderdevelopment,consultingandtrainingservicesperformedbytheauthorsofthesedocuments(seehttp://freeelectrons.com/).
Bysharingthisdocumentwithyourfriends,colleaguesandwiththelocalFreeSoftwarecommunity.
Byaddinglinksonyourwebsitetoouronlinematerials,toincreasetheirvisibilityinsearchengineresults.
CustomDevelopment
SystemintegrationEmbeddedLinuxdemosandprototypesSystemoptimizationApplicationandinterfacedevelopment
FreeElectronsOurservices
EmbeddedLinuxTraining
Allmaterialsreleasedwithafreelicense!
UnixandGNU/LinuxbasicsLinuxkernelanddriversdevelopmentRealtimeLinux,uClinuxDevelopmentandprofilingtoolsLightweighttoolsforembeddedsystemsRootfilesystemcreationAudioandmultimediaSystemoptimization
Consultingandtechnicalsupport
HelpindecisionmakingSystemarchitectureSystemdesignandperformancereviewDevelopmenttoolandapplicationsupportInvestigatingissuesandfixingtoolbugs
Linuxkernel
LinuxdevicedriversBoardsupportcodeMainstreamingkernelcodeKerneldebugging