unix commands - cmu.edu · pdf fileunix commands this page lists some of the more commonly...
TRANSCRIPT
UNIXCommandsThispagelistssomeofthemorecommonlyusedUNIXcommands.
AboutUNIX
• Commandsaretypedataprompt.Mostoften,thepromptisapercentsign(%)ordollarsign($)butsometimesitisthenameofthemachinefollowedbythepercentordollarsign.
• Commandsarecasesensitiveandareusuallylowercase.ThismeansthatlsandLSarecompletelydifferentcommands.• Spacesareveryimportant.Thereisalwaysaspacebetweenthecommandandthefileordirectoryitactsupon.• ToexecuteaUNIXcommand,pressEnterattheendofthecommandline.Ifthecommandisaccepted,thepromptand
cursorwillsimplyappearonthenextlineawaitingyournextcommand.Ifthecommandisrejected,anerrormessagesuchas"Commandnotfound"appears.Checkyourspelling,spaces,etcandtrytoreenterthecommand.TonegateacommandbeforeyouhavepressedEnter,pressCTRL+C.
• Todetermineyourdefaultshell,typeecho$SHELL.• Tochangeyourdefaultshell,run/usr/local/bin/chshandfollowtheprompt.DoNOTuseflagsonthecommand.Thisis
acustomscriptandnotthestandardchshyoumightfindonLinuxmachines.Afterrunningthecommand,allow24hoursforthedefaultshelltotakeeffect.
• Dotfilesbeginwithadot(.)andareusedprimarilytocontrolsystemfunctions.UnlessyouareanadvancedUNIXuser,youshouldnotaddordeleteanythingfromadotfile.
CommonUNIXCommands
Command Action
cat<file> Printcontentsoffileinthecommandwindow
cd<directory> Changedirectories
cp<file><file2> Copythecontentsoffileintofile2
history Listhistoryofallcommandsissuedatsystemprompt
ls Listthefilesandsubdirectoriesinadirectory
ls-F Listthedifferencebetweenfilesanddirectories--directorieshaveaslash(/)
ls-l Listfileswithstatus/detailinformation
ls-lt Listfileinformationinlongformat,sortedbytimewithnewestfilesornewlychangedfilesappearingfirst
ls-a Listallthefilesinadirectoryincludingdotfiles
fslq ListsAFSquota,spaceused,percentageused
fsq Listspercentageofquotaused
mkdir<directory>
Makeadirectory
mv<file><file2> Movefiletofile2
pwd Printthepathnameofthecurrentdirectory
rm<file> Removeordeletefiles
rmdir<directory> Removedirectory
Ctrl+C Tonegateacommandthatyouhaveentered.
CommandExamples
NavigatingtheFileSystem(cdcommand)
Function Command Example Notes
Tomovetoyourhomedirectory
TypecdandpressEnter. Nomatterwhereyouareinthefilesystem,youcanusethecd(changedirectory)commandtogetyoubacktoyourhomedirectoryimmediately.
Tomovetoasubdirectoryofyourown
Typecd<path>andpressEnter.
cdpublic
Tochangefromyourhomedirectorytoyourpublicdirectory.
Whenyouarechangingdirectoriesdownfromyourcurrentworkingdirectory,itisnotnecessarytotypethefullpathname.
Tomovetoanotherperson'shomedirectory
Typecd<path>andpressEnter.
cd/afs/andrew.cmu.edu/usr11/juser
Inthisexample,the<path>isthefullpathoftheotherperson'sdirectory.
Tilde(~)
Function Command Example Notes
Toabbreviatethepathname.
Typecd~<AndrewID>andpressEnter.
cd~juser
Tochangeintojuser'sdirectorywithouttypinginthefullpathname.
Thetildeishelpfulwhenyoudon'tknowsomeone'scompletepathname,orwhenyoujustwanttosavetypingtime.
ThetildecanbeusedwithanyUNIXcommand;however,youshouldneverusethetildeincommandfilessuchas.loginorinyourpreferencesfile.Inthesecases,thetildemaynotberecognizedandcanpreventAndrewandUNIXfromworkingproperlyforyou.
WhereamI?(pwd)
Function Command Example Notes
To"ask"UNIXwhichdirectoryyouarein.
TypepwdandpressEnter.
Viewdirectorycontents(ls)
Function Command Example Notes
Viewnamesoffilesandsubdirectoriesinadirectory.
TypelsandpressEnter. ThelscommanddoesNOTlistanydotfiles(i.e.,filesthatbeginwithdot(.)
Tolistfileswithstatusinformation
Typels-landpressEnter. Thels-lcommandliststhefilename,itsowner,datelastchanged,andsize.Filesthataredirectoriesareprecededwitha"d";plainfileshavean-rw-.
Toeasilyviewdifferencesbetweenfilesanddirectories.
Typels-FandpressEnter. Directorieswillbelistedwitha"/."
TolistALLfiles,includingDotfiles.
Typels-aandpressEnter.
Recursivefilelisting
Typels-RandpressEnter. Liststhefilesinthecurrentdirectoryaswellasthoseinthesubdirectories.
CreateDirectory(mkdir)
Function Command Example Notes
Createadirectory
Typemkdir<directoryname>andpressEnter.
mkdirplayground
Tomakeanewdirectorycalledplayground.
Onceyou'vemadethedirectory,usethelscommandtoverify.
CopyFiles(cp)
Function Command Example Notes
Tocopyafileinthesamedirectory.
Typecp<file><file.copy>andpressEnter.
cpresumeresume.copy
Tomakeacopyofafilenamed"resume"inthesamedirectory.
Tocopyafileintoanotherdirectory.
Typecp<file><directory>andpressEnter.
cpresumeprivate
Tomakeacopyofafilenamed"resume"intheprivatedirectory.
Tocopyafileintoanotheruser'saccount.
Typecp<path>/<file><path>/<file>andpressEnter.
cp~juser/notessample/notes.joe
Tocopyafilenamed"notes"fromyourfriendJoe'saccountintoyoursampledirectoryandnamethefilenotes.joe.
MoveFilesorDirectories(mv)
Function Command Example Notes
Tomoveafiletoanewfileinthesamedirectory(i.e.,renameafile).
Typemv<file><file2>andpressEnter.
mvnotes.joenotes.working
Tomoveafilenamed"notes.joe"toafilenamed"notes.working."Inthiscase,mvissimplyrenamingthefile.
Thedifferencebetweenmvandcpisthatcpplacesacopyofthefileinanewlocationwithoutdisturbingtheoriginalcopy.Themvcommandsdeletesthefilefromitsoldlocationaftersavingitinthenewlocation.
Tomoveafiletoanewfileinadifferentdirectory
Typemv<file><path>/<file>andpressEnter.
mvnotespublic/notes
Tomoveafilenamed"notes"fromyourhomedirectoryintoyourpublicdirectory,whileINyourhomedirectory.
Themvcommandisalsousedtomovedirectories.
RemoveaFile(rm)
Function Command Example Notes
Toremoveafile.
Typerm<file>andpressEnter. rmnotes.working
Toremovethefilenamed"notes.working"
Promptremove Typerm-iandpressEnter. Toinvokeapromptbeforeremovingafile;waitsfora"Y"or"N"response.
RemoveaDirectory(rmdir)
Function Command Example Notes
Toremoveadirectory(thatdoesnotcontainfiles).
Typermdir<directoryname>andpressEnter.
cd[Enter]rmdirsample
Toremoveadirectorynamed"sample"whichisasubdirectoryofyourhomedirectory.
Becausethe"sample"directoryisinasubdirectoryofyourhomedirectory,youmustfirstmovetoyourhomedirectory(cd).
Toforceremovalofadirectorythatcontainsfiles.
Typermdir-r<directoryname>andpressEnter.
Removesadirectoryevenifitcontainsfiles.