Basic Linux Commands
Basic Linux Commands
by GnanaSeelan
31stSeptember2011
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
BasicLinuxCommands
calCommandtoseecalenderforanyspecificmonthoracompleteyear usage:cal[month][year] eg:calsep2010
5/27/11
LinuxFileSystem
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
Standarddirectorystructure Standarddirectorystructure /thetopmost /devallthedevicesareaccessibleasfiles /varvariabledatasuchasmails,logfiles,databases /usralmostallthepackagesinstalled /etcconfigurationfiles /homehomedirectoriesforalltheusers /roothomedirectoryoftheprivilegeduserroot /mntusedtomountotherdirectories/partitions.
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
eg:ls*.c
?matchesanysinglecharacter
eg:lsab?.c
[]Thiswillmatchanysinglecharacterintherange.
eg:lstut[09].m Thiswillfindfilessuchastut0.m,tut9.metc.,
>Redirectstandardoutputtoafile.
echohelloworld>hello.txt
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
ShellMetacharacters
>>Appendsstandardoutputtoafile.
eg:echoHelloAgain>>hello.txt
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
FileSystemCommands
mkdirmakedirectory
cdchangedirectories
FileSystemCommands
cpcopyfilesanddirectories usage:cpsourcedestination
rmdirRemoveanexistingdirectory rmremovefilesordirectories
Usage:rmrname Removesdirectoriesandfileswithinthedirectoriesrecursively.
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
FileHandlingCommands
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
10
FileHandlingCommands
catusedtodisplaythecontentsofasmallfileonterminal
usage:more<filename> usage:less<filename>
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
11
FileHandlingCommands
wccommandisusedtocountlines,wordsandcharacters,dependingonthe optionused.
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
12
SimpleFilters
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
13
Filters
taildisplaysthelinesattheendofthefile.Bydefaultitwilldisplaylast10 linesofthefile
usage:tailfilename
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
14
cut&paste cutcommandcanbeusedtocutthecolumnsfromafilewithcoption.
eg:cutc1,35/etc/passwd
Withfoptionyoucancutthefeildsdelemitedbysomecharacter
eg:pastea.txtb.txt
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
15
Orderingafile
sortreorderslinesinthefile
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
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
5/27/11 CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 17
Searchingforapattern UsinggrepinaUnix/Linuxcommandpipeline
lsal|grep^d printthelinesthatstartswithd
Linuxgrepcommandtosearchformultiplepatternsatonetime
egrepboss|root/etc/passwd
greppatternmatchingandregularexpressions(regexpatterns)
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
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
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 19
Replacesalltheoccurrencesofhellooneachlinetoworld.
5/27/11
LinuxCommands
pwdprintworkingdirectory
willshowyouthefullpathtothedirectoryyouarecurrentlyin.
shredoverwriteafiletohideitscontents
Theresultisthatyourfileissothoroughlydeleteditisveryunlikelytoeverbe retrievedagain.
lnstestsymlink
Createsasymboliclinknamedsymlinkthatpointstothefiletest
freeDisplaystheamountofusedandfreesystemmemory.
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
20
LinuxCommands
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
21
LinuxCommands
dfreportfilesystemdiskspaceusage
Usage:dfh h>printsizesinhumanreadableformat
dusummarizediskusageofeachfile,recursivelyfordirectories.
Usage:duh
findFindlocationsoffiles/directoriesquicklyacrossentirefilesystem
LinuxCommands
vdisplaysadetailedinformation.
lsusbautilityfordisplayinginformationaboutUSBbusesinthe systemandthedevicesconnectedtothem.
vdisplaysadetailedinformation.
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
23
LinuxCommands
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
24
pslistsalltheprocesses usage:psaux
killtokillaprocess
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
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
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
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,your crontabentrywouldlooklikeasfollows: 03***/root/backup.sh Executeeveryminute *****/bin/script.sh Thisscriptisbeingexecutedeveryminute.
5/27/11 CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 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)
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
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)
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
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)
5/27/11 CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 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
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
31
Howtousevieditor Startingvi
Youmayusevitoopenanalreadyexistingfilebytyping vifilename
viModes
Howtousevieditor
EnteringText
Inordertobeginenteringtextinthisemptyfile,youmustchangefrom commandmodetoinsertmode.Todothis,type I
DeletingWords
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
33
Howtousevieditor
DeletingLines
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
34
Howtousevieditor
ReplacingCharacters
ReplacingWords
Toreplaceonewordwithanother,movetothestartoftheincorrectwordand type cw
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
35
ReplacingLines
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
36
Howtousevieditor
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
37
Howtousevieditor
MovingbySearching
Howtousevieditor
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
39
AdministrativeCommands
5/27/11
Networkcommands
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
41
ThankYou
5/27/11
CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad
42