Sunteți pe pagina 1din 7

5/20/2015

LearnUNIXin10minutes

FREEENGINEER.ORG
LearnUNIXin10minutes.Version1.3
Preface
ThisissomethingthatIhadgivenouttostudents(CADusertraining)inyearspast.
Thepurposewastohaveononepagethebasicscommandsforgettingstartedusing
theUNIXshell(sothattheydidn'tcallmeaskingwhattodothefirsttimesomeone
gavethematape).
ThisdocumentiscopyrightedbutfreelyredistributableunderthetermsoftheGFDL.

Haveanideaforthispage?
Sendmepatches,comments,corrections,aboutwhateveryouthinkiswrongorshouldbe
included.Iamalwayshappytohearfromyou.Pleaseincludetheword"UNIX"inyoursubject.
Sections:
Directories:
Movingaroundthefilesystem:
Listingdirectorycontents:
Changingfilepermissionsandattributes
Moving,renaming,andcopyingfiles:
Viewingandeditingfiles:
Shells
Environmentvariables
InteractiveHistory
FilenameCompletion
Bashisthewaycoolshell.
Redirection:
Pipes:
CommandSubstitution
Searchingforstringsinfiles:Thegrepcommand
Searchingforfiles:Thefindcommand
Readingandwritingtapes,backups,andarchives:Thetarcommand
Filecompression:compress,gzip,andbzip2
Lookingforhelp:Themanandaproposcommands
Basicsofthevieditor
FAQs
******************************************************************************************
BasicUNIXCommandLine(shell)navigation:LastrevisedMay172001
******************************************************************************************
Directories:
FileanddirectorypathsinUNIXusetheforwardslash"/"
toseparatedirectorynamesinapath.
examples:
/"root"directory
/usrdirectoryusr(subdirectoryof/"root"directory)
/usr/STRIM100STRIM100isasubdirectoryof/usr
Movingaroundthefilesystem:
pwdShowthe"presentworkingdirectory",orcurrentdirectory.
cdChangecurrentdirectorytoyourHOMEdirectory.
cd/usr/STRIM100Changecurrentdirectoryto/usr/STRIM100.
cdINITChangecurrentdirectorytoINITwhichisasubdirectoryofthecurrent
directory.
cd..Changecurrentdirectorytotheparentdirectoryofthecurrentdirectory.
cd$STRMWORKChangecurrentdirectorytothedirectorydefinedbytheenvironment
variable'STRMWORK'.
http://freeengineer.org/learnUNIXin10minutes.html

1/7

5/20/2015

LearnUNIXin10minutes

cd~bobChangethecurrentdirectorytotheuserbob'shomedirectory(ifyouhavepermission).
Listingdirectorycontents:
lslistadirectory
lsllistadirectoryinlong(detailed)format
forexample:
$lsl
drwxrxrx4cliffuser1024Jun1809:40WAITRON_EARNINGS
rwrr1cliffuser767392Jun614:28scanlib.tar.gz
^^^^^^^^^^^
|||||||||||
|||||ownergroupsizedatetimename
||||numberoflinkstofileordirectorycontents
|||permissionsforworld
||permissionsformembersofgroup
|permissionsforowneroffile:r=read,w=write,x=execute=nopermission
typeoffile:=normalfile,d=directory,l=symboliclink,andothers...
lsaListthecurrentdirectoryincludinghiddenfiles.Hiddenfilesstart
with"."
lsld*Listallthefileanddirectorynamesinthecurrentdirectoryusing
longformat.Withoutthe"d"option,lswouldlistthecontents
ofanysubdirectoryofthecurrent.Withthe"d"option,ls
justliststhemlikeregularfiles.
Changingfilepermissionsandattributes
chmod755fileChangesthepermissionsoffiletoberwxfortheowner,andrxfor
thegroupandtheworld.(7=rwx=111binary.5=rx=101binary)
chgrpuserfileMakesfilebelongtothegroupuser.
chownclifffileMakesclifftheowneroffile.
chownRcliffdirMakesclifftheownerofdirandeverythinginitsdirectorytree.
Youmustbetheownerofthefile/directoryorberootbeforeyoucandoanyofthesethings.
Moving,renaming,andcopyingfiles:
cpfile1file2copyafile
mvfile1newnamemoveorrenameafile
mvfile1~/AAA/movefile1intosubdirectoryAAAinyourhomedirectory.
rmfile1[file2...]removeordeleteafile
rmrdir1[dir2...]recursivlyremoveadirectoryanditscontentsBECAREFUL!
mkdirdir1[dir2...]createdirectories
mkdirpdirpathcreatethedirectorydirpath,includingallimplieddirectoriesinthepath.
rmdirdir1[dir2...]removeanemptydirectory
Viewingandeditingfiles:
catfilenameDumpafiletothescreeninascii.
morefilenameProgressivelydumpafiletothescreen:ENTER=onelinedown
SPACEBAR=pagedownq=quit
lessfilenameLikemore,butyoucanusePageUptoo.Notonallsystems.
vifilenameEditafileusingthevieditor.AllUNIXsystemswillhaveviinsomeform.
emacsfilenameEditafileusingtheemacseditor.Notallsystemswillhaveemacs.
headfilenameShowthefirstfewlinesofafile.
headnfilenameShowthefirstnlinesofafile.
tailfilenameShowthelastfewlinesofafile.
tailnfilenameShowthelastnlinesofafile.
Shells
Thebehaviorofthecommandlineinterfacewilldifferslightlydepending
http://freeengineer.org/learnUNIXin10minutes.html

2/7

5/20/2015

LearnUNIXin10minutes

ontheshellprogramthatisbeingused.
Dependingontheshellused,someextrabehaviorscanbequitenifty.
Youcanfindoutwhatshellyouareusingbythecommand:
echo$SHELL
Ofcourseyoucancreateafilewithalistofshellcommandsandexecuteitlike
aprogramtoperformatask.Thisiscalledashellscript.Thisisinfactthe
primarypurposeofmostshells,nottheinteractivecommandlinebehavior.
Environmentvariables
Youcanteachyourshelltorememberthingsforlaterusingenvironmentvariables.
Forexampleunderthebashshell:
exportCASROOT=/usr/local/CAS3.0DefinesthevariableCASROOTwiththevalue
/usr/local/CAS3.0.
exportLD_LIBRARY_PATH=$CASROOT/Linux/libDefinesthevariableLD_LIBRARY_PATHwith
thevalueofCASROOTwith/Linux/libappended,
or/usr/local/CAS3.0/Linux/lib
Byprefixing$tothevariablename,youcanevaluateitinanycommand:
cd$CASROOTChangesyourpresentworkingdirectorytothevalueofCASROOT
echo$CASROOTPrintsoutthevalueofCASROOT,or/usr/local/CAS3.0
printenvCASROOTDoesthesamethinginbashandsomeothershells.
InteractiveHistory
Afeatureofbashandtcsh(andsometimesothers)youcanuse
theuparrowkeystoaccessyourpreviouscommands,edit
them,andreexecutethem.
FilenameCompletion
Afeatureofbashandtcsh(andpossiblyothers)youcanusethe
TABkeytocompleteapartiallytypedfilename.Forexampleifyou
haveafilecalledconstantinemonksandwillywonka.txtinyour
directoryandwanttoeditityoucantype'viconst',hittheTABkey,
andtheshellwillfillintherestofthenameforyou(providedthe
completionisunique).
Bashisthewaycoolshell.
Bashwillevencompletethenameofcommandsandenvironmentvariables.
Andiftherearemultiplecompletions,ifyouhitTABtwicebashwillshow
youallthecompletions.BashisthedefaultusershellformostLinuxsystems.
Redirection:
grepstringfilename>newfileRedirectstheoutputoftheabovegrep
commandtoafile'newfile'.
grepstringfilename>>existfileAppendstheoutputofthegrepcommand
totheendof'existfile'.
Theredirectiondirectives,>and>>canbeusedontheoutputofmostcommands
todirecttheiroutputtoafile.
Pipes:

http://freeengineer.org/learnUNIXin10minutes.html

3/7

5/20/2015

LearnUNIXin10minutes

Thepipesymbol"|"isusedtodirecttheoutputofonecommandtotheinput
ofanother.
Forexample:
lsl|moreThiscommandstakestheoutputofthelongformatdirectorylistcommand
"lsl"andpipesitthroughthemorecommand(alsoknownasafilter).
Inthiscaseaverylonglistoffilescanbeviewedapageatatime.
dusc*|sortn|tail
Thecommand"dusc"liststhesizesofallfilesanddirectoriesinthe
currentworkingdirectory.Thatispipedthrough"sortn"whichordersthe
outputfromsmallesttolargestsize.Finally,thatoutputispipedthrough"tail"
whichdisplaysonlythelastfew(whichjusthappentobethelargest)results.
CommandSubstitution
Youcanusetheoutputofonecommandasaninputtoanothercommandinanotherway
calledcommandsubstitution.Commandsubstitutionisinvokedwhenbyenclosingthe
substitutedcommandinbackwardssinglequotes.Forexample:
cat`find.nameaaa.txt`
whichwillcat(dumptothescreen)allthefilesnamedaaa.txtthatexistinthecurrent
directoryorinanysubdirectorytree.

Searchingforstringsinfiles:Thegrepcommand
grepstringfilenameprintsallthelinesinafilethatcontainthestring
Searchingforfiles:Thefindcommand
findsearch_pathnamefilename
find.nameaaa.txtFindsallthefilesnamedaaa.txtinthecurrentdirectoryor
anysubdirectorytree.
find/namevimrcFindallthefilesnamed'vimrc'anywhereonthesystem.
find/usr/local/gamesname"*xpilot*"
Findallfileswhosenamescontainthestring'xpilot'which
existwithinthe'/usr/local/games'directorytree.
Readingandwritingtapes,backups,andarchives:Thetarcommand
Thetarcommandstandsfor"tapearchive".Itisthe"standard"waytoread
andwritearchives(collectionsoffilesandwholedirectorytrees).
Oftenyouwillfindarchivesofstuffwithnameslikestuff.tar,orstuff.tar.gz.This
isstuffinatararchive,andstuffinatararchivewhichhasbeencompressedusingthe
gzipcompressionprogramrespectivly.
ChancesarethatifsomeonegivesyouatapewrittenonaUNIXsystem,itwillbeintarformat,
andyouwillusetar(andyourtapedrive)toreadit.
Likewise,ifyouwanttowriteatapetogivetosomeoneelse,youshouldprobablyuse
taraswell.
Tarexamples:
tarxvExtracts(x)filesfromthedefaulttapedrivewhilelisting(v=verbose)
thefilenamestothescreen.
tartvListsthefilesfromthedefaulttapedevicewithoutextractingthem.
tarcvfile1file2
Writefiles'file1'and'file2'tothedefaulttapedevice.
tarcvfarchive.tarfile1[file2...]
Createatararchiveasafile"archive.tar"containingfile1,
http://freeengineer.org/learnUNIXin10minutes.html

4/7

5/20/2015

LearnUNIXin10minutes

file2...etc.
tarxvfarchive.tarextractfromthearchivefile
tarcvfzarchive.tar.gzdname
Createagzipcompressedtararchivecontainingeverythinginthedirectory
'dname'.Thisdoesnotworkwithallversionsoftar.
tarxvfzarchive.tar.gz
Extractagzipcompressedtararchive.Doesnotworkwithallversionsoftar.
tarcvfIarchive.tar.bz2dname
Createabz2compressedtararchive.Doesnotworkwithallversionsoftar
Filecompression:compress,gzip,andbzip2
ThestandardUNIXcompressioncommandsarecompressanduncompress.Compressedfileshave
asuffix.Zaddedtotheirname.Forexample:
compresspart.igsCreatesacompressedfilepart.igs.Z
uncompresspart.igsUncompresseispart.igsfromthecompressedfilepart.igs.Z.
Notethe.Zisnotrequired.
Anothercommoncompressionutilityisgzip(andgunzip).ThesearetheGNUcompressand
uncompressutilities.gzipusuallygivesbettercompressionthanstandardcompress,
butmaynotbeinstalledonallsystems.Thesuffixforgzippedfilesis.gz
gzippart.igsCreatesacompressedfilepart.igs.gz
gunzippart.igsExtractstheoriginalfilefrompart.igs.gz
Thebzip2utilityhas(ingeneral)evenbettercompressionthangzip,butatthecostoflonger
timestocompressanduncompressthefiles.Itisnotascommonautilityasgzip,butis
becomingmoregenerallyavailable.
bzip2part.igsCreateacompressedIgesfilepart.igs.bz2
bunzip2part.igs.bz2Uncompressthecompressedigesfile.

Lookingforhelp:Themanandaproposcommands
Mostofthecommandshaveamanualpagewhichgivesometimesuseful,oftenmoreorless
detailed,sometimescrypticandunfathomablediscriptionsoftheirusage.Somesaythey
arecalledmanpagesbecausetheyareonlyforrealmen.
Example:
manlsShowsthemanualpageforthelscommand
Youcansearchthroughthemanpagesusingapropos
Example:
aproposbuildShowsalistofallthemanpageswhosediscriptionscontaintheword"build"
Doamanaproposfordetailedhelponapropos.
Basicsofthevieditor
Openingafile
vifilename
Creatingtext
Editmodes:Thesekeysentereditingmodesandtypeinthetext
ofyourdocument.
iInsertbeforecurrentcursorposition
IInsertatbeginningofcurrentline
aInsert(append)aftercurrentcursorposition
AAppendtoendofline
rReplace1character
RReplacemode
<ESC>Terminateinsertionoroverwritemode
http://freeengineer.org/learnUNIXin10minutes.html

5/7

5/20/2015

LearnUNIXin10minutes

Deletionoftext
xDeletesinglecharacter
ddDeletecurrentlineandputinbuffer
nddDeletenlines(nisanumber)andputtheminbuffer
JAttachesthenextlinetotheendofthecurrentline(deletescarriagereturn).
Oops
uUndolastcommand
cutandpaste
yyYankcurrentlineintobuffer
nyyYanknlinesintobuffer
pPutthecontentsofthebufferafterthecurrentline
PPutthecontentsofthebufferbeforethecurrentline
cursorpositioning
^dPagedown
^uPageup
:nPositioncursoratlinen
:$Positioncursoratendoffile
^gDisplaycurrentlinenumber
h,j,k,lLeft,Down,Up,andRightrespectivly.Yourarrowkeysshouldalsoworkif
ifyourkeyboardmappingsareanywherenearsane.
stringsubstitution
:n1,n2:s/string1/string2/[g]Substitutestring2forstring1onlines
n1ton2.Ifgisincluded(meaningglobal),
allinstancesofstring1oneachline
aresubstituted.Ifgisnotincluded,
onlythefirstinstancepermatchinglineis
substituted.
^matchesstartofline
.matchesanysinglecharacter
$matchesendofline
Theseandother"specialcharacters"(liketheforwardslash)canbe"escaped"with\
i.etomatchthestring"/usr/STRIM100/SOFT"say"\/usr\/STRIM100\/SOFT"
Examples:
:1,$:s/dog/cat/gSubstitute'cat'for'dog',everyinstance
fortheentirefilelines1to$(endoffile)
:23,25:/frog/bird/Substitute'bird'for'frog'onlines
23through25.Onlythefirstinstance
oneachlineissubstituted.
Savingandquittingandother"ex"commands
Thesecommandsareallprefixedbypressingcolon(:)andthenenteredinthelower
leftcornerofthewindow.Theyarecalled"ex"commandsbecausetheyarecommands
oftheextexteditortheprecursorlineeditortothescreeneditor
vi.Youcannotenteran"ex"commandwhenyouareinaneditmode(typingtextontothescreen)
Press<ESC>toexitfromaneditingmode.
:wWritethecurrentfile.
:wnew.fileWritethefiletothename'new.file'.
:w!existing.fileOverwriteanexistingfilewiththefilecurrentlybeingedited.
:wqWritethefileandquit.
:qQuit.
:q!Quitwithnochanges.
:efilenameOpenthefile'filename'forediting.
:setnumberTurnsonlinenumbering
:setnonumberTurnsofflinenumbering
http://freeengineer.org/learnUNIXin10minutes.html

6/7

5/20/2015

LearnUNIXin10minutes

FAQs
TheUSENETFAQsshouldbethefirstplaceyoulookforananswertospecificquestions.
YoucanfindmostofthematRTFM
Thecontentsofthisdirectoryincludesvi,bash,andcomp.unix.questionsFAQs.
SearchingUSENETarchivesareveryusefultoo.
google.comhasaUSENETarchive(formerlyDeja.com's).
AdvancedGroupSearchrules.
ThisdocumentwasconvertedfromplaintextusingVimand
thenhacked.Vimisthebestversionoftheonetruetexteditor:vi.

Copyright(c)20002006
Permissionisgrantedtocopy,distributeand/ormodifythisdocument
underthetermsoftheGNUFreeDocumentationLicense,Version1.1
oranylaterversionpublishedbytheFreeSoftwareFoundation;
withInvariantSection:Preface,withFrontCoverTexts,andwithno
BackCoverTexts.AcopyofthelicensecanbefoundontheGNUwebsite
here.

FREEENGINEER.ORG

http://freeengineer.org/learnUNIXin10minutes.html

7/7

S-ar putea să vă placă și