UNIX常用命令UNIX常用命令
UnixDevelopmentEnvironment
lTheBasicKnowleage
lTheGeneralCommand
lUnixShell
lMakeFile
TheBasicKnowledge
Unix/linuxsystemsaremulti-userandmulti-tasking
lHavemultipleusersonthesamesystem
lRunmultiplePRograms,seeminglysimultaneously
lMaintainpermissionsthatdeterminewhichusershaveaccesstowhickfilesandprograms
lRegulatetheamountofdiskspaceeachusercanuse
lStoreuser-specificsettingsin“hidden”file
LoggingIn
lWhenyoufirstconnecttooneoftheUnixcomputersyouwillseetheprompt:
lLogin:
Atthelogin:prompt,typeinyourusernamePassWord
Onceyouhavetypedinyourusernameyouwillbepromptedtotypeinyourpassword
InitializationFiles
WhenyoulogintheUnix/Linuxloginprogramfinally,startsupacommand“shell”
UsersdonotdealwiththeOperatingsystemdirectly.MostofyourinteractionwithaUnixsystemtakesplaceinashell,aprogramthatisruneverytimeyoulogin,anddisplaysthe“$”prompt.Theshellisknownascommandinterpreter,yougiveitcommands,anditrunsthem.
Shell
lSh
lBourneShell
lKsh
lKornShell
lCsh
lCshellbasedonClanguage
lBash
lBornAgainShell
lTcsh
lanotherversionofCshell
UsingtheSystem
Finallyyouareloggedin!Youwillseeapromptlikeoneofthefollowing:
$/#
Justwaitingfoyoutotypesomething.Wewilluser$toindicatethecomputer’s“ready”prompt.
Okey,let’stryasimplecommand.
ls
lsistheprogramtolistfilesinadirectory.Justplainlswon’tlisthiddenfiles(fileswhosenamesstartwith``.’’,like.login).Nowtrytyping:
ls-a
pwd(printworkingdirectory)
Tofindoutwhichdirectoryyouarein,type:
pwd
Thiswillshowyouthefullpathtoyourcurrentworkingdirectory,relativetotherootdirectory(/).
Rm(removefilesordirectories)
Thecommandrmfilewillremovethespecifiedfile.
Someusefuloptionsforrminclude
rm-i(interactivemode-youwillbepromptedtocomfirmfiledeletion)
rm-f(forceremove-overridesinteractivemodeandremovesfilewithoutaprompt)
rm-r(recursive-removethecontentsofdirectoriesrecursively)
cp(copyfilesanddirectories)
Tocopyafile,do:
cpoldfilenamenewfilename
Ortocopyafiletoanewdirectory,whilekeepingtheoldfilenamedo:
cpfilename/directoryname/
Toensureyourdon’toverwriteanyexistingfilesyoucanusethe‘-i’option:
cp-ioldfilenamenewfilename
Thiscommandwillpromptyoutoconfirmifthereisalreadyafilewiththenamenewfilename.
mv(movefiles)
Thiscommandallowsyoutorenameafileasin:
mvoldfilenamenewfilename
Ortomoveafiletoanewdirectory:
mvfilenamedirectoryname/
Aswithcp,itisagoodideatousethe‘-i’optionwithmv,soyoudon’tmistakenlyoverwriteyourexistingfiles.
Thespecialdirectory
Youmayhavenoticedwhenyoutypedls–athattherewerelistingsdot(.)anddot-dot(..).Theserepresentyourcurrentworkingdirectory(.)andit’sparentdirectory(..).
Forexample,typing
cd..
cd.
cp/vol/examples/tutorial/science.txt.
cd~willalwaysbringyoubacktoyourhomedirectory.
cd~/homeworkwillbringyoutothe‘homework’directory.
Thecharacter*iscalledawildcardandwillmatchagainstnoneofmorecharacter(s)inafileordirectoryname.
Forexample,ifyoutype
ls*h
Youshouldgetalistofallfilesanddirectoriesstartingwiththeletterh.
Similarlyifyoutype:
ls*.cpp
The?characterissimilartothe*character,exceptthat?willmatchexactlyonecharacter,Forexample:
ls?anwillmatch‘man’butnot‘stan’!
Displaythecontentsofafileonthescreen
clear(clearscreen)
Beforeyoustartthenextsection,youmayliketocleartheterminalwindowofthepreviouscommandssotheoutputofthefollowingcommandscanbeclearlyunderstood.
Attheprompt,type
clear
Thiswillclearalltextandleaveyouwiththe%promptatthetopofthewindow.
cat(concatenate)
Thecommandcatcanbeusedtodisplaythecontentsofafileonthescreen.Type:
catscience.txt
asyoucansee,thefileislongerthanthesizeofthewindow,soitscrollspastmakingitunreadable.
less
Thecommandlesswritesthecontentsofafilenotothescreenapageatatime,type:
lessscience.txt
Pressthe[space-bar]ifyouwanttoseeanotherpage,type[q]ifyouwanttoquitreading.
head
Theheadcommandwritesthefirsttenlinesofafiletothescreen.
Firstclearthescreenthentype
headscience.txt
tail
thetailcommandwritesthelasttenlinesofafiletothescreen.Clearthescreenandtype:
tailscience.txt
Searchingthecontentsofafile
Simplesearchingusingless
Usingless,youcansearchthoughatextfileforakeyword(pattern).Forexample,tosearchthroughscience.txtfortheword‘science’,type
lessscience.txt
Thenstillinless(i.e.don’tpress[q]toquit),typeaforwardslash[/]followedbythewordtosearch.
Asyoucansee,lessfindsandhighlightsthekeyword.Type[n]tosearchforthenextoccurrenceoftheword.
grep
Itsearchesfilesforspecifiedwordsorpatterns.Firstclearthescreen,thentype
grepsciencescience.txt
Asyoucansee,grephasprintedouteachlinecontainingthewordscience.
Trytyping
grepSciencescience.txt
Thegrepcommandsiscasesensitive;ItdistinguishesbetweenScienceandscience.
Toignoreupper/lowercasedistinctions,usethe-ioption,i.e.Type
grep-isciencescience.txt
TosearchforapharSEOrpattern,youmustencloseitinsinglequotes.Forexampletosearchforspinningtop.Type