Académique Documents
Professionnel Documents
Culture Documents
ServiceofInformatics.CNB
AnIntroductoryCoursetoInformatics
Introduction
ACommandLanguageInterpreterorshellisaprogramthatcoverstheoperatingsystemactingasan
interfacethatyouusetocommunicatewiththecomputeratthemorebasiclevel:itallowsyoutokeep
trackandarrangeyourfiles,startotherprogramstoprocessyourinformation,andtosomeextentbuild
programstoautomatetasksthatyouneedtodofrequently.
Unixallowsyoutouseseveraldifferentshells,themostpopularofthembeingsh,csh,ksh,bashand
tcsh.Theyallowyoutosendveryelaboratecommandstothecomputer,butforthemostofyourwork,all
youneedisahandfulofcommands,mostofwhicharecommontoallthem.
Beingscaryasweare,itisimportantthatyoutakecaretochooseagoodpassword.Thisshouldby
contrastnotbeaword,atleastnotasinglewordthatcouldbefoundinanydictionary.Itisalsoagood
ideaifyouputinsideitnumbersorspecialpunctuationcharactersandusemixedcaseletters.Itshouldbe
long(atleastsixcharacters,althoughthatisnowadaystooshort),makingitwhenpossibleintoapass
phraseinsteadofapassword,whichmakesitbythewayeasiertoremember.Howeversomebraindead
systems(likeSGI'swhichonlyallowamaximumof8characterlongpasswords,sadly)won'tallowyouto
uselongpasswords,forcingyoutousestrange,unnaturaloreasytoguessones.Still,wheneverpossible,
weadvisethatyouusepassphrasespossiblymixingcapsandincludingpunctuationsymbolsforyour
safety.
OK,sohowdoyoudoit?InUNIXthecommandtouseiscalledpasswd:itwillpromptyouforyour
oldpasswordtoverifyyouareyourselfandnotsomepasserbythatcamewhileyouwenttemporarilyout
foracofeeoryouweredistracted.Onceyouhaveidentifiedyourselfyouareaskedforthenewpassword.
Thisisdonetwicebecausethesystemdoesn'tshowanythingyoutypeonthescreen(toavoidshoulder
surfers)andyoucouldhavemadeamistakeandneverbeallowedinagainunlessyourepeatedthesame
mistakeagain.Onceyournewpasswordhasbeenverifieditisstoredencrypted(usuallyinahidden
place)anditbecomesactivedfromthenon.
Anexamplesessionforonegiven"JoeRandomUser"whoseoriginalpasswordwas"Ohyeah!"couldbe:
passwd
Changing password for JoeRandomUser
Old password: Oh yeah!
New password: Tell me, what?
Re-enter new password: tell me what?
They don't match
Try again.
New password: Tell me, what?
Re-enter new password: Tell me, what?
#
#
#
#
not shown
not shown
if we make a mistake
we are given another chance
# OK now
Otherexamplesofgoodpasswordscouldpossiblybe:
1. Torres10
2. 4weeksinglobe
3. 53+two=57
4. Sgt.PeppersLonelyHeartsClubBand
5. 4536Kms.apied,ausemessouliers!
6. pBR322
7. ClNa=Salt
8. 120Km/h
9. +34(1)9036969
10.4oqtep.Yt3s
Thesepasswordsareveryeasytoremember,buthardenoughformostcurrentcrackingsystems.However
youshouldbeawarethatthereisalreadypeoplestartingtocompilecollectionsofpopular"phrases"to
useincrackingsoftware,soatsomepointverycommonphrasesmayprobablyneedtobeavoidedtoo.
Also,youshouldavoid"easytoguess"phrasesorpasswordsbasedonpersonaldata:fromtheabove
examples,thefirstonewouldbeeasyifeveryoneknewIlikedsomeparticularbrandofbrandy,the
secondifIwereaknownreaderofJulesverne,thefourthifIwhereafanofTheBeatles,thesixthifI
whereamolecularbiologist,theeightifIwereadriverandtheninthifitweremyphonenumber(which
itisn'tbytheway).Thelastexample,rathermoredifficulthasbeenobtainedbyusingtheinitialsofthe
lyricsofapopularlibertinesong,makingiteasytorememberbutdifficulttoguess.
So,yousee,itisnotdifficulttocomeupwithgoodpasswords,andcanbeevenlotsoffunifyoudon't
takeitasahassle.Allthat'sneededisasetofsimpleprecautionstoavoidthatevenyourclosestfriends
couldguessit.Evenifyouarelimitedto8characterpasswordstherearestillplentyoffunnytricksyou
canresorttoifyouwant.
Files
Normallyyouwilluseadifferentprogramforeachdifferenttask.Thewayinwhichanyprogramstores
theinformationyouuseisbyarrangingitinaspecificobjectorcontainernamedfile.Tipically,files
madewithoneprogramwillnotbeofmuchuseforotherprograms.Filesaredistinguishedbygiving
themanamewhenyoucreatethem.Ifyouchosecarefullythename,youwillalsorecognizeitscontents
onreadingthename.
NotethatasadifferencetosystemslikeVMS,thesystemdoesnotkeepoldversionsaround.Itdirectly
overwritesanyoldversionwithoutacomment.Versionhousekeepingisataskyoumustdomanuallyin
Unix.
Anotherpossibilitytocreateatextfileistouseatexteditor.Themostpopulartexteditorsinunixare
emacs,joe,picoandvi.
Removing a file
Onceyounolongerneedafileevermore,youcaneliminateit.Notethatthisisaonewayfunction:once
afileisdeleteditisgoneforever.Thecommandtouseisrm(remove):
rm my_file.dat
Means"makeacopyoffilemy_original.datandnamethatcopymy_copy.dat".
Changing the name of a file
Thisiseasilydonewiththecommandmv(move):
mv my_ugly_file.dat my_gorgeous_name.dat
Thiswillshowyouthecontentsofthefileonepageorscreenfulatatime.Initiallyyouwillbeshownthe
firstpageorscreen;afterwardsyoucanmovearoundbyusingthecarriagereturnkeytomoveoneline
downorspacetomoveonescreenforward.Thereisahostofkeypressesthatwillhelpyoumovearound
inthefile,andthatyoucanseebypressingthe
Working with wildcards
Awildcardisaspecialcharacterthatcanbesubstitutedbysomethingelse.Thisallowsyoutobroadenthe
scopetowhichyourcommandsapply.TherearemanywildcardsavailableinUNIX,butthemost
commonlyusedarethesymbols?and*.Wheneveryouusea?insideafilenameitwillassumethatit
canbesubstitutedbyanyonecharacterinthesameposition.A*indicatesanyoneormorecharacters
startingatthatposition.Thisprocessisusuallyknownasglobbing.
AnimportantissuetorememberisthatinUNIX,globbing(i.e.wildcardexpansion)isdonebytheshell.
Thismeansthatwheneveryougiveacommandwithwildcards,thesewillbeexpandedfirsttoconstructa
newcommandline,andthenthecommandwillbeinvokedwiththesenewarguments.
Forinstance:ifyoutypethecommand
cp *.dat *.txt
inadirectorycontainingthefiles
file.dat
archive.dat
mydata.dat
other.txt
letter.txt
Thiswon'tmean"copyallfilesthatendin.datintofileswiththesamenamebutendingin.txt".Instead,
theshellcommandwilldothesubstitutionandexecutethecommand
cp file.dat archive.dat mydata.dat other.txt letter.txt
Others(o):thisreferstoanybodywhocanloginthesystem.
Group(g):thisreferstoanyonethatbelongsinthesamegroupasyou.
User(u):thisisyou.
Foranyofthesecategoriesyoucanspecifyalsoseveralkindsofprotection:
Read(r):Allowpeopleinthatleveltoread,printorcopythatitem.
Write(w):Allowmembersofthatleveltowriteormodifytheobject.
Execute(x):Thisworksforexecutablefiles(i.e.programs)allowingtorunthem.
Tochangetheprotectionofafileusechmod (changeaccessmode).Sincetherearesomanyprotection
mechanisms,youhavetouseanargumentlist.Argumentlistsarelistsofdifferentargumentsthatyou
grouptogetherseparatedbycommas.Inthecaseofchmodyouindicatethecategorywhoseprivileges
youwanttomodify(u,g,o),whetheryouwanttoenable(+)ordisable()privileges,andtheprivileges
tobeaffected:
chmod u+rw,g+r-wx,o-rwx my_file.dat
Thismeans:changeaccessmodessothattheuser(I)canreadandwritethefile,peopleinmygroupcan
readbutnotwriteorexecutethefile,andanyotherscan'taccessitinanyway.
Directories
Youcouldhaveallyourdifferentfilesmixedtogether,butthisisnotpractical:Itisbettertokeeprelated
filesseparatedfromunrelatedones.Youcanachievethesameresultsinacomputerusingdirectories:
thesearelikefolders,drawers,cabinets,rooms,etc...:youcanstorefiles(documents)onthem,andlabel
directoriessignificatively,andnestthemtodifferentlevels.
Notethatyouusedirectlythedirectoryname.InUnixyoudonothaveanyspecialmethodtoreferto
directoriesashappensinothersystemslikeVMS.Theyarejustfileslikeanyother,onlytheyarespecial
filesthatcontainmorefilesinside.
Finding your way.
Yourdefaultdirectoryistheoneinwhicheverythinghappensunlessyoutellotherwise.Youcanlearn
whereyouarewithpwd (printworkingdirectory).Thesystemanswerswiththenameofyourcurrent
workingfolder.
pwd
/u/grpname/username
Youcantellthesystemtoselectadifferentdirectorytoworkin,withthecommandcd(change
directory):allsubsequentcommandswilltakeplaceinthatnewdirectory:
pwd
/u/grp/username
ls
file1.dat
file2.dat
subdir
cd /u/grp/username/subdir
ls
otherfile.dat
otherfile.plus
cd /u/grp/username
#What do we have?
othersubdir
#Move into subdir
#Verify
#Go back
Youcanspecifyasubdirectoryhangingfromyourcurrentpositionbyjustgivingitsname;hence:
cd subdir
wouldhavesufficedinthepreviousexample.
Sinceadirectorymaybecontainedinonlyoneparentdirectory,youcanusetwodots(..)tospecifythe
parentofasubdirectory.Intheaboveexampleyoucouldaswellhavetypedattheend
cd ..
Youcancombinedirectorynamesinapathtellingthenamesofthedirectoriestobetraversedseparated
byslashes(/).Intheexampleabove/u/grp/username/subdirmeant"Gototopleveldirectory'u',
enter'grp',then'username'andthengoonto'subdir'".
Youcanusethiswayofreferringtodirectorieswithothercommands,e.g.
mv rna/some.seq .
wouldhaveasconsequencethatsome.seqisrenamed(andeffectivelymoved)fromthesubdirectory
rnatoourcurrentdirectory(specifiedbyusingasingledot)`.'.
Deleting a directory
Todeleteadirectoryyoumustdoitintwosteps:
Emptythedirectoryfromallitscontents(both,filesandsubdirectories).
Deletethedirectoryusingrmdir(removedirectory).
ls my_directory
rmdir my_directory
#verify it is empty
#and remove it
I/O redirection
OneofthemostpowerfulfeaturesofUNIXistheabilitytoinstructacommandtosenditsoutputorread
itsinputfromanywhereyoulike.Youcaneventellacommandtosenditsoutputtoanotheronefor
furtherprocessinghencecombiningtheirfunctionalityinversatileways.Thisallowsyoutohaveshort,
elegantcommandsthatdoasimplejobverywellandhavethemcooperatetogetcomplexresults.
Whilethereareafewdifferencesamongshells,allofthemunderstandacommonsubsetofredirection
directives:
Redirecting output
Youcansendallofacommand'soutput(everythingitwouldotherwisenormallysendtoyourterminal)
toafilebyusingthe>characterafterthecommandfollowedbythenameofthefileintowhichyouwant
tosavetheoutput:
ls > mylisting.txt
Thiscreatesanewfilenamedmylisting.txt(ifitalreadyexists,itremovestheoldonebefore
creatingit)andsendsalltheoutputoflstoit.
Themainproblemwith>isthatifthedestinationfileexists,itspreviouscontentsarelost.Youcan
insteadappendtheoutputofthecommandattheendofanypreviouscontentsbyusingtworightangle
brackets,>>tospecifythedestinationoftheoutput:
ls >> mylisting.txt
Input redirection
Youmayaswellinstructacommandthatnormallyreadsdatafromyourterminaltogetitsinputfroma
fileinstead.Thisisachievedbyusingtheleftanglebracket<character:
sort < myfile
Notethatinthiscaseyoudon'thavethechoicetoappendsinceitdoesn'tmakesenseforacommandto
appendtoanyinputprevioustoitsinvocation.
Concatenating I/O
Whileoftenuseful,itisnotenoughtobeabletoredirectonecommand'sinputoroutputtoafile.Many
timesonewouldwanttocombinethecapabilitiesofoneprogramwiththoseofanotherone.Forinstance,
onewouldliketogetafulllistingofadirectorysortedotherthanthedefaultway,andmaybeseeingthe
listingonepageatatime.Thiscaneasilybedonebyfirstsendingtheoutputofonecommandtoafile,
andtheninstructingthenextcommandtoreadfromthatfile:
ls -l > tmpfile1
sort < tmpfile1 > tmpfile2
more tmpfile2
Butthisiscumbersomeandrequiresthecreationofmanytemporaryfiles.Wouldn'titbeniceifwecould
instructthecomputertotaketheoutputofacommandanduseitastheinputofthenextone?Wecan
achievethiseffectwiththebarcharacter|toseparatetwocommands.Thisiscalledapipesinceitserves
asaconductionofdatafromonecommandtothenext.Wecanuseseriesofpipestocombineseriesof
programs:
ls -l | sort -k9 | less
ls -l | sort -k5 | less
Inthefirstexamplewegetafulldirectorylistingwithls -landthenwepassonthelistingtosortso
itgetssortedbyfilename(-k9meanssortbytheninthfield,thefilename)andfinallywebrowsethe
listingcomfortablyusingless,anenhancedversionofthefilebrowsermore.
Inthesecondexamplewegetanotherlistinginasimilarway:wepassthefulllistingwegetfromlsto
sortandthesortedlistingtolessforeasyviewing.Theonlydifferenceisthatthistimewegetfiles
sortedbysize(fifthfield).
Wecanconcatenateasmanycommandsasweneedtoachieveourgoal.Thisallowsforgreatversatility.
Inthiscontext,eachoftheintermediatecommandsweuse(like,e.g.sortabove)iscalledafiltersince
itreadssomeinput,performssomeoperationonit(filtersit)andproducessomeoutputthatispassedon
tothenextfilter.
Job control
OneoftheadvantagesofUNIXisthatitisamultitaskingoperatingsystem.Thismeansyoumaycarry
outseveraltasksatthesametime.Forinstance,youmightberunningalongscientificjobwhileatthe
sametimeeditadatafile.Youmayalsogivecommandstobeexecutedsothattheyrunindependently
andyoudon'thavetowaitforthemtofinishtokeeponworkingandbenotifiedlaterwhenitfinishes.
Thisallowsyoutogetthemostofyoutimeonline.
Stopping a job
Insomecases,youmaybeworkingonsomeprogram,andthensuddenlybeinterestedindoinganother
task,butwithoutleavingtheprogramyouareusing.Forinstance,youmaybereadingafile,andthen
wonderaboutwhattimeisit.Youcantemporarilystoptheprocessbyusing^Z(pressingthe[CTRL]and
[Z]keyssimultaneously),issueanycommandsyouwant,andthenresuscitethesuspendedprocesswith
thecommandfg.
man date
date(1)
NAME
Background jobs
YoumayinstructUNIXtorunacommandinthebackground.Thismeansthatthecommandwillrun
unattendedwhileyoukeeponworkinginothertasks.Thisalsomeansthatthecommandmustbeableto
rununattended,i.e.withoutanyhelpfromyou.
Byaddinganampersand&attheendofacommandline,youstatethatthecommandorseriesof
commandsyougiveshouldruninthebackground,detachedfromyourterminal:
ls -lR /data/ftp/pub/databases/pdb > pdblist &
Thisrequiresthatthecommandorcommandsyougivedonotneedanyhumanhelptocomplete.
Otherwisetheywillstopatsomepointaskingforsomedataandwon'tcontinuerunning.
Ifyouwanttorunaprogramthatfirstasksyouforsomeinitialdataandthenwrapsitselfintosomevery
lengthycomputationyoucan'tusethe&mechanism.Inthiscase,youmaystarttheprograminteractively,
answerallitsquestions,andwhenitstartsitscomputationstep,stopitwith^Z.Atthispointyoucan
issuethecommandbgtosendittothebackgroundasifyouhadstarteditwithanampersand.
bc -l > PI
scale=1000
4 * a(1)
^Z
Suspended
bg
[1]
bc -l > PI &
...
...
[1] + Suspended (tty input)
fg %1
quit
bc -l > PI
Herewefirststartbc,thebinarycalculatorusingthetrascendentalmathlibrary(-l)andsendingits
outputtoafilenamedPI.Wetheninstructbctouseaprecisionof1000decimalplaces(scale=1000)
andcalculatePI(4byarctangentof1radian).Sincethiswilltakequitesometime,westopherethe
programusing^Zandsendittothebackgroundusingbg.Theshelladvisesusthatwehavemodifiedthe
commandasifwehadrunitwithanampersandattheend,andtellsusthatthisbackgroundjobhas
number[1].
Intheaboveexamplewealsoseehowwecanrecoveraninteractiveprogramthatisrunninginthe
background.Sometimelater,theprogramwillfinishitscomputationandwaitformoreinteractive
commands.Sinceitisnotintheforeground,itcan'ttakeanyinput,andwearenotifiedofit([1] +
Suspended (tty input) bc -l > PI).Whatwedoisbringituptotheforegroundwiththefg
commandfollowedbya%signandthejobnumber.Wecannowtellourcommandthatwearedoneor
continuedoingmorecomputationsifsowewish.
TTY
ttyq29
ttyq29
ttyq29
ttyq29
TIME
0:02
0:00
0:02
0:00
CMD
ls
bc
tcsh
ps
Thisshowsallthecommandsyouarerunning,togetherwiththeirsystemIDnumber(PID)whichisnot
yourjobnumberandtheterminal(TTY)fromwhichtheyhavebeenstarted.
Killing a job
Humansasweare,wearenotfreefrommistakes.Insomecaseswemaywanttostopanerroneous
commandwehaveissued.Therearetwowaystodoit:
Ifthecommandisrunninginthebackground,wecanusethekillcommandfollowedbyapercentsign
andthejobnumber:
ls -lR / > /dev/null &
[1] 11084
kill %1
[1]
Terminated
Notethespaceafterkillandthatthejobnumbermustbeimmediatelyprecededbya%sign.
Ifwearerunninganinteractivecommand,wecannormallyuse^C(thecombinationofkeys[CTRL]and
[C])tofinishthetask:
bc
^C
(interrupt) use quit to exit.
Butsomeprogramsdonotrespondto^Cortakeittomeansomethingdifferent.Inthesecaseswemay
trytoplayatrickonthecomputer:wefirstuse^Ztotemporarilystopthejobasabove,andthenwekill
theprogramwithkill.Notethatyoumustknowthenumberthatcorrespondstothestoppedprocess(one
plusthenumberofthehighestbackgroundjob).
ls -lR / > /dev/null
^Z
Suspended
ps
PID TTY
TIME CMD
29683 ttyq29 0:02 tcsh
11146 ttyq29 0:00 ps
11144 ttyq29 0:00 ls
kill 11144
[1]
Terminated
Notethatinthisexamplewehaveusedpstogetthesystemidentificationnumberfortheprocess(PID)
andthenwehaveusethisnumberwithoutapreceding%signtokillit.
Finishing off
Whenyouaredonewiththesystemandwanttoendyourworkingsessiontherearevariouswaysin
whichyoucandosodependingontheshellyouuseandhowyoustartedthesession.Themostcommon
onesare:
Typing[CTRL]+DtomeanEndofTransmission,i.e.thatyouhaveterminatedgivingorders.
Usingthecommandlogout
Usingthecommandexit
Normallyyou'lljusttypelogoutandthat'sall,butitisalwaysgoodtoknowabouttheother
possibilitiesjustincase.
allowsyoutoindentifymanualpagesbykeywords,whilemanwilldisplayamanualpageontherequired
topic;forexample
apropos permissions
chmod (1)
chmod (1)
genperm (1M)
permissions (1X)
permissions
....
man chmod
....
man tcsh
AlternativelyyoucanstarttheprogramxmanifyouhaveaccesstotheXwindowsgraphicaluser
interface.
AsanintroductiontoUNIXmanualpages,youshouldknowthattheyarearrangedinnumberedsections
devotedtodifferentthemes.aproposshowsthesectionnumberinparenthesesafterthecommand.
Normallyyouwillonlybeinterestedinsection1ofthemanual,usercommands.ThesectionsinBSD
systemsare:
(1)usercommands
(2)systemcalls
(3)programminglibraries
(4)specialfiles
(5)fileformats
(6)games
(7)miscellaneousdocuments
(8)systemmaintenance
Butaswesaid,normallyyoushouldonlybeconcernedwithsection1ofthemanual.
OryoumaytakealookataQuickReferenceGuide
JosR.Valverde|EMBnet/CNB|JRValverde@embnet.cnb.uam.es