Linux Shell Commands
Linux Shell Commands
PeteNesbitt May2006
LinuxShellCommands
BelowareafewexamplesofLinuxcommandsoftenseeninscripts.Ofcoursetheymaybe incorporatedintoscriptsorusedatthecommandline.Theintentionistoprovideabasicexampleof thesecommands,youmayfindthemanpage,thecommandfollowedbyhelporgoogleusefulfor furtheringyourunderstanding.Theyarepresentedinnoparticularorder. Althoughthesecommandsmayappearsimpleinfunction,mosthavemanyoptions.Therealpowerof scriptingorcomplexcommandlinescomeswhenthesesimplefunctionsarejoinedwithpipesand redirectswhichcancausetheoutputfromonecommandtobecometheinputofthenext.Inmostcases spaceshavebeenaddedaroundthepipesymbol'|',thisisforreadability.Suchspacesareoptionaland donotimpactthecommandstring. IMPORTANTNOTE:Ifyoutryandcutandpastetheexamplesinthisdocument,youwillprobably needtomanuallyreplacethespecialcharacterssincethewordprocessormodifiesthemfor presentation.Onescopiedfromthecommandlinewillbeokay,butonestypeddirectlyintothis documentwillneedtobecorrected.Theseinclude,butarenotlimitedto'`/\| Command:grep Description:matchesastringfrominput Examples: 1) findallfilesthatendin.tar ls|grep".tar$" 2) findalllinesinafilethatdonotcontainthestringerror,regardlessofcase grepiverrormessages.log 3) matchoneormorestrings(thiscanbeegreporgrepEalsoseefgreporgrepF) egrep123|abc|wxysomefile.txt Command:cat Description:displayorjointestfiles Examples: 1) printafiletostandardoutput(thescreen) cat/etc/hosts 2) joinfile1andfiles2savingthemasfile3
1of3
OverviewofaFewCommonCommands
PeteNesbitt May2006
LinuxShellCommands
catfile1file2>file3 Command:sed Description:replacestring1withstring2 Examples: 1) replaceallinstancesofNewwiththeOldfromfile1andsaveasfile2 seds/New/Old/gfile1>file2 2) removethefirstinstanceofthestring123fromastringofnumbersandjustprinttoscreen echo123456123|seds/123// (outputs456123) Command:tr Description:usedtotranslate,deleteormanipulatetextstrings Examples: 1) changeastringfromlowertouppercase echoabcdef|tr[:lower:][:upper:] (outputsABCBEF) 2) removeanycharacters(b,corg)anywhereinastring echoabcdefg|trdbcg (outputsadef) Command:cut Description:separateastringintopieces,outputcertainones Examples: 1) extractthe2ndsetofcolumnseparatedvalues echo123:456:789|cutd:f2 (outputs456) 2) extractthe1stand3rdsetofspaceseparatedvalues echo123456789|cutd''f1,3 (outputs123789) Command:awk Description:manipulatesfieldsfromastring.Unlikecut,theFieldSeparatorcanbemultiple characters. Examples: 1) extractthe2ndsetofvalues echo"abc123def123ghi"|awkF123'{print$2}' (outputsdef)
2of3
OverviewofaFewCommonCommands
PeteNesbitt May2006
LinuxShellCommands
2) extractandreordersomepartofastring echo"abcdefghi"|awk'{print$2,"\t",$1}'
(outputsdefabc)
Command:sort Description:sortsdata. Examples: 1) reversethenaturalnumberorderofaseriesofdigits.Withoutthen,valuessuchas161and 18872wouldbeplacedtogetherinthelistbelow.(lsisusedjusttogetusaquicklistof numbers) lsl|awk'{print$5}'|sortnr outputsaverticallist: 51200 19349 18903 18872 8993 6607 5810 ... 1368 1345 274 222 195 161 141