Basic Commands
Basic Commands
by B.Prathibha [email protected]
15thSeptember,2010
9/15/10
9/15/10
LinuxFileSystem
9/15/10
Standarddirectorystructure Standarddirectorystructure /thetopmost /devallthedevicesareaccessibleasfiles /varvariabledatasuchasmails,logfiles,databases /usralmostallthepackagesinstalled /etcconfigurationfiles /homehomedirectoriesforalltheusers /roothomedirectoryoftheprivilegeduserroot /mntusedtomountotherdirectories/partitions.
9/15/10
eg:ls*.c
?matchesanysinglecharacter
eg:lsab?.c
[]Thiswillmatchanysinglecharacterintherange.
eg:lstut[09].m Thiswillfindfilessuchastut0.m,tut9.metc.,
>Redirectstandardoutputtoafile.
echohelloworld>hello.txt
9/15/10
ShellMetacharacters
>>Appendsstandardoutputtoafile.
eg:echoHelloAgain>>hello.txt
9/15/10
FileSystemCommands
mkdirmakedirectory
cdchangedirectories
Usecdtochangedirectories.Typecdfollowedbythenameofadirectoryto accessthatdirectory.
mvchangethenameofadirectory
FileSystemCommands
cpcopyfilesanddirectories usage:cpsourcedestination
rmdirRemoveanexistingdirectory rmremovefilesordirectories
Usage:rmrname Removesdirectoriesandfileswithinthedirectoriesrecursively.
9/15/10
FileHandlingCommands
9/15/10
10
FileHandlingCommands
catusedtodisplaythecontentsofasmallfileonterminal
moreandlesscommandsareusedtoviewlargefilesonepageatatime
usage:more<filename> usage:less<filename>
9/15/10
11
FileHandlingCommands
wccommandisusedtocountlines,wordsandcharacters,dependingonthe optionused.
9/15/10
12
SimpleFilters
9/15/10
13
Filters
taildisplaysthelinesattheendofthefile.Bydefaultitwilldisplaylast10 linesofthefile
usage:tailfilename
9/15/10
14
cut&paste cutcommandcanbeusedtocutthecolumnsfromafilewithcoption.
eg:cutc1,35/etc/passwd
Withfoptionyoucancutthefeildsdelemitedbysomecharacter
pastecommandwillpastethecontentsofthefilesidebyside
eg:pastea.txtb.txt
9/15/10
15
Orderingafile
sortreorderslinesinthefile
9/15/10
16
Searchingforapattern
grepscansitsinputforapattern,displaysthelinecontainingthatpattern
usage:grepoptionspatternfilename(s)
searchingforatextstringinone
grep'boss'/etc/passwd searchesforthepatternbossinthe/etc/passwdfile
searchingforatextstringinmultiplefiles
greproot*.txt
CaseinsensitivefilesearchingwiththeUnixgrepcommand
grepihellohello.txt
Reversingthemeaningofagrepsearch
grepvboss/etc/passwd Displaysallthelinesthatdonotcontainthespecifiedpattern
9/15/10 17
Searchingforapattern UsinggrepinaUnix/Linuxcommandpipeline
lsal|grep^d printthelinesthatstartswithd
Linuxgrepcommandtosearchformultiplepatternsatonetime
egrepboss|root/etc/passwd
greppatternmatchingandregularexpressions(regexpatterns)
9/15/10
18
sed
sededitorforfilteringandtransformingtext
i>editthefilesinplace
sedi'1,10d'hello.txt
deletedthefirst10linesfromhello.txt
sedi2ihaihello.txt
Insertsthetexthaiinthesecondline
sedi'/hello/d'hello.txt
Deletedthelinecontainingthepatternhello.
sed's/hello/world/'hello.txt
Replacesthefirstoccurrenceofhellooneachlinetoworld.
sed's/hello/world/g'hello.txt
19
Replacesalltheoccurrencesofhellooneachlinetoworld.
9/15/10
LinuxCommands
pwdprintworkingdirectory
willshowyouthefullpathtothedirectoryyouarecurrentlyin.
shredoverwriteafiletohideitscontents
Theresultisthatyourfileissothoroughlydeleteditisveryunlikelytoeverbe retrievedagain.
lnstestsymlink
Createsasymboliclinknamedsymlinkthatpointstothefiletest
freeDisplaystheamountofusedandfreesystemmemory.
9/15/10
20
LinuxCommands
9/15/10
21
LinuxCommands
dfreportfilesystemdiskspaceusage
Usage:dfh h>printsizesinhumanreadableformat
dusummarizediskusageofeachfile,recursivelyfordirectories.
Usage:duh
findFindlocationsoffiles/directoriesquicklyacrossentirefilesystem
LinuxCommands
vdisplaysadetailedinformation.
lsusbautilityfordisplayinginformationaboutUSBbusesinthe systemandthedevicesconnectedtothem.
vdisplaysadetailedinformation.
9/15/10
23
LinuxCommands
9/15/10
24
pslistsalltheprocesses usage:psaux
killtokillaprocess
9/15/10
25
TaskAutomation
Cronisthenameofprogramthatenableslinuxuserstoexecutecommands orscripts(groupsofcommands)automaticallyataspecifiedtime/date. Youcansetupsetupcommandsorscripts,whichwillrepeatedlyrunataset time. Thecronservice(daemon)runsinthebackgroundandconstantlychecksthe /etc/crontabfile,/etc/cron.*/directories. Italsochecksthe/var/spool/cron/directory. Toeditthecrontabfile,typethefollowingcommandattheLinuxshell prompt: crontabe Syntaxofcrontab(FieldDescription) mhdommondow/path/to/commandarg1arg2 where
9/15/10
26
TaskAutomation m:Minute(059) h:Hours(023) dom:Date(031) mon:Month(012[12==December]) dow:weekdays(07[0or7sunday]) /path/to/commandScriptorcommandnametoschedule Ifyouwishedtohaveascriptnamed/root/backup.shruneverydayat3am, yourcrontabentrywouldlooklikeasfollows: 03***/root/backup.sh Executeeveryminute *****/bin/script.sh Thisscriptisbeingexecutedeveryminute.
9/15/10 27
TaskAutomation ExecuteeveryFriday1AM Toschedulethescripttorunat1AMeveryFriday,wewouldneedthefollowing cronjob: 01**5/bin/execute/this/script.sh Thescriptisnowbeingexecutedwhenthesystemclockhits: 1.minute:0 2.ofhour:1 3.ofdayofmonth:*(everydayofmonth) 4.ofmonth:*(everymonth) 5.andweekday:5(=Friday)
9/15/10
28
TaskAutomation Executeonworkdays1AM ToschedulethescripttorunfromMondaytoFridayat1AM,wewouldneed thefollowingcronjob: 01**15/bin/script.sh Thescriptisnowbeingexecutedwhenthesystemclockhits: 1.minute:0 2.ofhour:1 3.ofdayofmonth:*(everydayofmonth) 4.ofmonth:*(everymonth) 5.andweekday:15(=MondaytillFriday)
9/15/10
29
TaskAutomation Execute10pastaftereveryhouronthe1stofeverymonth 10*1**/bin/script.sh ifyouwanttorunsomethingevery10minutes: 0,10,20,30,40,50****/bin/script.sh or */10****/bin/script.sh SpecialWords Ifyouusethefirst(minute)field,youcanalsoputinakeywordinsteadofa number: @rebootRunonce,atstartup @yearlyRunonceayear"0011*" @annually(sameas@yearly)
9/15/10 30
TaskAutomation @monthlyRunonceamonth"001**" @weeklyRunonceaweek"00**0" @dailyRunonceaday"00***" @midnight(sameas@daily) @hourlyRunonceanhour"0**** Eg:@daily/bin/script.sh Storingthecrontaboutput Tostoretheoutputinaseparatelogfile.Here'show: */10****/bin/script.sh2>&1>>/var/log/script_output.log
9/15/10
31
Howtousevieditor Startingvi
Youmayusevitoopenanalreadyexistingfilebytyping vifilename
viModes
Howtousevieditor
EnteringText
Inordertobeginenteringtextinthisemptyfile,youmustchangefrom commandmodetoinsertmode.Todothis,type I
DeletingWords
9/15/10
33
Howtousevieditor
DeletingLines
9/15/10
34
Howtousevieditor
ReplacingCharacters
ReplacingWords
Toreplaceonewordwithanother,movetothestartoftheincorrectwordand type cw
9/15/10
35
ReplacingLines
9/15/10
36
Howtousevieditor
9/15/10
37
Howtousevieditor
MovingbySearching
Howtousevieditor
9/15/10
39
ThankYou
9/15/10
40