首页 > 系统 > Unix > 正文

UNIX常用命令

2024-06-28 13:23:19
字体:
来源:转载
供稿:网友
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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表