You are on page 1of 6

Prev

RedHatEnterpriseLinux3:RedHatEnterpriseLinuxStepByStepGuide
Chapter3.ShellPromptBasics

Next

3.8.ManipulatingFileswithcat
RedHatEnterpriseLinuxhasautilitywhichcanhelpyoukeepshortlists,gatherliststogether,
andevenshowyouinformationaboutyoursystem.
Theutilityiscalledcat,shortforconcatenate,whichmeanstocombinefiles.
Thecommandcatalsodisplaysthecontentsofanentirefileonthescreen(forexample,type
catfilename.txt).Ifthefileisfairlylong,itquicklyscrollspastyouonthescreen.To
preventthis,usethecatfilename.txt|lesscommand.
Usingthepipe(|)andthelesscommandtogetherdisplaysthefileonepageatatime.Youcan
thenusetheupanddownarrowkeystomovebackwardandforwardthroughthepages.For
moreonusingpipestocombinetwoseparatefunctions,refertoSection3.9PipesandPagers.

3.8.1.UsingRedirection
Redirectionmeanscausingtheshelltochangewhatitconsiderstobestandardinputorwhere
thestandardoutputshouldbegoing.
Toredirectstandardoutput,usethe>symbol.Placing>afterthecatcommand(orafterany
utilityorapplicationthatwritestostandardoutput)directsitsoutputtothefilenamefollowing
thesymbol.
Forexample,usingcatbyitselfoutputswhateveryouinputtothescreenasifitwererepeating
thelineyoujusttyped.Thefollowingexampleshowscatrepeatingeverylinethatisentered:

Figure35.ThecatCommand

Toredirecttheoutputofcattoafile,typethefollowingatashellprompt(pressingthe[Enter]
keytakesyoutothenextblankline):
cat>sneakers.txt

Figure36.RedirectingOutputtoaFile
Press[Enter]togotoanemptylineandusethe[Ctrl][D]keycombinationtoquitcat.
DoyounoticeanythingdifferentinFigure36?Therearenorepeatedentries.Thatisbecause
thestandardoutputfromcatwasredirected.Thatredirectionwastoabrandnewfileyoumade
calledsneakers.txt.
Youcanfindthefileinthedirectoryyouwereinwhenyoustartedcat(typelsifyouwantto
seeitlisted).
Asyoulearnedearlier,youcanalsousecattoreadthefileyouhavecreated.Attheprompt,
type:
catsneakers.txt

Caution

Becarefulwhenyouredirecttheoutputtoafile,becauseyoucaneasily
overwriteanexistingfile!Makesurethenameofthefileyouarecreatingdoes
notmatchthenameofapreexistingfile,unlessyouwanttoreplaceit.

Useoutputredirectionagainforanotherfileandcallithome.txt.Forthisexample,typethe
commandcat>home.txt,then[Enter],followedby:
bringthecoffeehome
takeoffshoes
putonsneakers
makesomecoffee
relax!
Now,onanemptyline,usethe[Ctrl][D]keycombinationagaintoquitcat.
Next,usecattojoinhome.txtwithsneakers.txtandredirecttheoutputofbothfilestoa
brandnewfilecalledsaturday.txt(asseeninFigure37).Typethefollowing:
catsneakers.txthome.txt>saturday.txt

Figure37.JoiningFilesandRedirectingOutput
Youcanseethatcathasaddedhome.txtwheresneakers.txtended.

3.8.2.AppendingStandardOutput
Youcanuseoutputredirectiontoaddnewinformationtotheendofanexistingfile.Similarto
whenyouusedthe>symbol,youtellyourshelltosendtheinformationsomewhereotherthan
standardoutput.
However,whenyouuse>>,youareaddinginformationtoafile,ratherthanreplacingthe
contentsofafileentirely.

Thebestexplanationisademonstration.Taketwofileswhichhavealreadybeencreated
(sneakers.txtandhome.txt)andjointhembyusingtheappendoutputsymbol.Toaddthe
informationinhome.txttotheinformationalreadyinsneakers.txt,type:
cathome.txt>>sneakers.txt
Nowcheckthefileusingthecommandcatsneakers.txt.Thefinaloutputshowsthe
contentsofhome.txtattheendofthefile:
buysomesneakers
thengotothecoffeeshop
thenbuysomecoffee
bringthecoffeehome
takeoffshoes
putonsneakers
makesomecoffee
relax!
Thecommandyoutypedappendedtheoutputfromthefilehome.txttothefilesneakers.txt.
Byappendingtheoutput,yousaveyourselftime(andabitofdiskclutter)byusingexistingfiles,
ratherthancreatinganewfile.
Comparetheresultsofthefilessneakers.txtandsaturday.txt,andyoucanseethatthey
areidentical.Tomakeyourcomparison,type:
catsneakers.txtcatsaturday.txt
Thecontentsofbothfilesaredisplayedfirstsneakers.txt,thensaturday.txt(asshown
inFigure38).

Figure38.StringingCommandsandComparingFiles

3.8.3.RedirectingStandardInput
Notonlycanyouredirectstandardoutput,youcanperformthesametypeofredirectionwith
standardinput.
Whenyouusetheredirectstandardinputsymbol<,youaretellingtheshellthatyouwantafile
tobereadasinputforacommand.
Useafileyouhavealreadycreatedtodemonstratethisidea.Type:
cat<sneakers.txt
Becauseyouusedthelessthansymbol(<)toseparatethecatcommandfromthefile,the
outputofsneakers.txtwasreadbycat.

Figure39.RedirectingStandardInput
Prev
ClearingandResettingthe
Terminal

Home
Up

Next
PipesandPagers

You might also like