unix commands - cmu.edu · pdf fileunix commands this page lists some of the more commonly...

6
UNIX Commands This page lists some of the more commonly used UNIX commands. About UNIX Commands are typed at a prompt. Most often, the prompt is a percent sign (%) or dollar sign ($) but sometimes it is the name of the machine followed by the percent or dollar sign. Commands are case sensitive and are usually lower case. This means that ls and LS are completely different commands. Spaces are very important. There is always a space between the command and the file or directory it acts upon. To execute a UNIX command, press Enter at the end of the command line. If the command is accepted, the prompt and cursor will simply appear on the next line awaiting your next command. If the command is rejected, an error message such as "Command not found" appears. Check your spelling, spaces, etc and try to reenter the command. To negate a command before you have pressed Enter, press CTRL + C. To determine your default shell, type echo $SHELL. To change your default shell, run /usr/local/bin/chsh and follow the prompt. Do NOT use flags on the command. This is a custom script and not the standard chsh you might find on Linux machines. After running the command, allow 24 hours for the default shell to take effect. Dot files begin with a dot (.) and are used primarily to control system functions. Unless you are an advanced UNIX user, you should not add or delete anything from a dot file. Common UNIX Commands Command Action cat <file> Print contents of file in the command window cd <directory> Change directories cp <file> <file2> Copy the contents of file into file2 history List history of all commands issued at system prompt ls List the files and subdirectories in a directory ls -F List the difference between files and directories--directories have a slash (/) ls -l List files with status/detail information ls -lt List file information in long format, sorted by time with newest files or newly changed files appearing first ls -a List all the files in a directory including dot files fs lq Lists AFS quota, space used, percentage used fs q Lists percentage of quota used mkdir <directory> Make a directory mv <file> <file2> Move file to file 2 pwd Print the pathname of the current directory

Upload: ledat

Post on 30-Jan-2018

229 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Unix Commands - cmu.edu · PDF fileUNIX Commands This page lists some of the more commonly used UNIX commands. About UNIX • Commands are typed at a prompt. Most often, the prompt

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

Page 2: Unix Commands - cmu.edu · PDF fileUNIX Commands This page lists some of the more commonly used UNIX commands. About UNIX • Commands are typed at a prompt. Most often, the prompt

rm<file> Removeordeletefiles

rmdir<directory> Removedirectory

Ctrl+C Tonegateacommandthatyouhaveentered.

CommandExamples

Page 3: Unix Commands - cmu.edu · PDF fileUNIX Commands This page lists some of the more commonly used UNIX commands. About UNIX • Commands are typed at a prompt. Most often, the prompt

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

Page 4: Unix Commands - cmu.edu · PDF fileUNIX Commands This page lists some of the more commonly used UNIX commands. About UNIX • Commands are typed at a prompt. Most often, the prompt

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

Page 5: Unix Commands - cmu.edu · PDF fileUNIX Commands This page lists some of the more commonly used UNIX commands. About UNIX • Commands are typed at a prompt. Most often, the prompt

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).

Page 6: Unix Commands - cmu.edu · PDF fileUNIX Commands This page lists some of the more commonly used UNIX commands. About UNIX • Commands are typed at a prompt. Most often, the prompt

Toforceremovalofadirectorythatcontainsfiles.

Typermdir-r<directoryname>andpressEnter.

Removesadirectoryevenifitcontainsfiles.