Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
35 views

Basic Linux Commands

The document provides an overview of basic Linux commands for viewing system information, manipulating files and directories, searching files, editing text and more. It describes commands for viewing the calendar and date/time, changing passwords, displaying command documentation and printing messages. It also covers standard Linux directory structure, shell metacharacters, file system commands and file handling commands.

Uploaded by

vivek0630
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Basic Linux Commands

The document provides an overview of basic Linux commands for viewing system information, manipulating files and directories, searching files, editing text and more. It describes commands for viewing the calendar and date/time, changing passwords, displaying command documentation and printing messages. It also covers standard Linux directory structure, shell metacharacters, file system commands and file handling commands.

Uploaded by

vivek0630
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 42

BasicLinuxCommands

by GnanaSeelan

31stSeptember2011

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

BasicLinuxCommands
calCommandtoseecalenderforanyspecificmonthoracompleteyear usage:cal[month][year] eg:calsep2010

dateprintsthedateandtime date+"%D%H:%M:%S" 09/14/1014:19:43 Options: dThedateofthemonth(131) yThelasttwodigitsoftheyear H,M,SHourMinuteandsecondrespectively Dthedateinmm/dd/yy


5/27/11 CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 2

BasicLinuxCommands echo:Printmessageontheterminal usage:echo<message> eg:echo"Welcometotheworkshop" o/p:Welcometotheworkshop

passwdallowsyoutochangeyourpassword mandisplaysthedocumentationforacommand usage:man<commandname> eg:manmkdir


CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

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

ShellMetacharacters MetacharactersThesearespecialcharactersthatarerecognisedbytheshell. *matches0ormorecharacters.

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

<Takesstandardinputfromafile |Thisispipecharacter.Sendstheoutputoffirstcommandasinputforthe secondcommand.

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

FileSystemCommands

mkdirmakedirectory

usage:mkdir<dirname> eg:mkdirppath/test/test1 p>noerrorifexisting,makeparentdirectoriesasneeded

cdchangedirectories

Usecdtochangedirectories.Typecdfollowedbythenameofadirectoryto accessthatdirectory. mvchangethenameofadirectory

Typemvfollowedbythecurrentnameofadirectoryandthenewnameof thedirectory. Ex:mvtestdirnewnamedir


5/27/11 CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 8

FileSystemCommands

cpcopyfilesanddirectories usage:cpsourcedestination

cpimyfileyourfile Withthe"i"option,ifthefile"yourfile"exists,youwillbepromptedbeforeitis overwritten. cprsrcdirdestdir Copyallfilesfromthedirectory"srcdir"tothedirectory"destdir"recursively.

rmdirRemoveanexistingdirectory rmremovefilesordirectories

Usage:rmrname Removesdirectoriesandfileswithinthedirectoriesrecursively.

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

FileHandlingCommands

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

10

FileHandlingCommands

catusedtodisplaythecontentsofasmallfileonterminal

usage:cat<filename> catwhensuppliedwithmorethanonefilewillconcatenatethefileswithout anyheaderinformation moreandlesscommandsareusedtoviewlargefilesonepageatatime

usage:more<filename> usage:less<filename>

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

11

FileHandlingCommands

wccommandisusedtocountlines,wordsandcharacters,dependingonthe optionused.

usage:wc[options][filename] Youcanjustprintnumberoflines,numberofwordsornumberofcharcters byusingfollowingoptions: l:Numberoflines w:Numberofwords c:Numberofcharacters

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

12

SimpleFilters

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

13

Filters

Filtersarecommandswhichacceptdatafromstandardinput,manupulateit andwritetheresultstostandardoutput. Headdisplaysthelinesatthetopofthefile

whenusedwithoutanyoptionitwilldisplayfirst10linesofthefile usage:headfilename n>printthefirstNlinesinsteadofthefirst10

taildisplaysthelinesattheendofthefile.Bydefaultitwilldisplaylast10 linesofthefile

usage:tailfilename

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

14

cut&paste cutcommandcanbeusedtocutthecolumnsfromafilewithcoption.

eg:cutc1,35/etc/passwd

Withfoptionyoucancutthefeildsdelemitedbysomecharacter

eg:cutd:f2/etc/passwd doptionisusedtospecifythedelimiterandfoptionusedtospecifythe feildnumber pastecommandwillpastethecontentsofthefilesidebyside

eg:pastea.txtb.txt

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

15

Orderingafile

sortreorderslinesinthefile

whitespacesfirst,thennumerals,uppercaseandfinallylowercase youcansortthefilebasedonafieldbyusingtandkoption. Eg:sortt""k2students.txt sortsthefilebasedonthesecondfieldusingthedelimiterasspace r>reversestheresult

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)

grep'[FG]oo'* grep'[09][09][09]'* grep'^fred'/etc/passwd

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

wshowwhoisloggedonandwhattheyaredoing usage:w whoshowwhoisloggedin usage:who whob>lastsystemboottime

whoamiprintstheeffectiveuserid. whereisls Locatesbinariesandmanualpagesforthelscommand. catdisplaysthecontentsofthefileonthescreen.

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

21

LinuxCommands

dfreportfilesystemdiskspaceusage

Usage:dfh h>printsizesinhumanreadableformat

dusummarizediskusageofeachfile,recursivelyfordirectories.

Usage:duh

findFindlocationsoffiles/directoriesquicklyacrossentirefilesystem

Usage:find/nameappnametypedxdev typedsearchforthedirectorynamedappname xdevDon'tdescenddirectoriesonotherfilesystems. searchagainstalldirectoriesbelow/fortheappnamefoundindirectoriesbutonly ontheexistingfilesystem.


5/27/11 CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 22

LinuxCommands

Commandtofindandremovefiles find.name"FILETOFIND"execrmrf{}\; lspciautilityfordisplayinginformationaboutPCIbusesinthe systemanddevicesconnectedtothem.

vdisplaysadetailedinformation.

lsusbautilityfordisplayinginformationaboutUSBbusesinthe systemandthedevicesconnectedtothem.

vdisplaysadetailedinformation.

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

23

LinuxCommands

lshwlistthehardware hwinfoprobsforthehardware cat/proc/cpuinfogivesinformationaboutcpu cat/proc/meminfogivesinformationaboutmemory

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

24

pscommand ps(i.e.,processstatus)commandisusedtoprovideinformationaboutthe currentlyrunningprocesses,includingtheirprocessidentificationnumbers (PIDs).

pslistsalltheprocesses usage:psaux

killtokillaprocess

psismostoftenusedtoobtainthePIDofamalfunctioningprocessinorder toterminateitwiththekillcommand usage:kill9pid wherepidprocessidoftheprocesstobekilled.

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

vihastwomodes: *commandmode *insertmode Incommandmode,thelettersofthekeyboardperformeditingfunctions (likemovingthecursor,deletingtext,etc.).Toentercommandmode,press theescape<Esc>key. Ininsertmode,thelettersyoutypeformwordsandsentences.Unlikemany wordprocessors,vistartsupincommandmode.


5/27/11 CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 32

Howtousevieditor

EnteringText

Inordertobeginenteringtextinthisemptyfile,youmustchangefrom commandmodetoinsertmode.Todothis,type I

DeletingWords

Todeleteaword,movethecursortothefirstletteroftheword,andtype dw Thiscommanddeletesthewordandthespacefollowingit. Todeletethreewordstype 3dw

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

33

Howtousevieditor

DeletingLines

Todeleteawholeline,type dd Thecursordoesnothavetobeatthebeginningoftheline.Typingdddeletes theentirelinecontainingthecursorandplacesthecursoratthestartofthe nextline.Todeletetwolines,type 2dd Todeletefromthecursorpositiontotheendoftheline,type D(uppercase)

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

34

Howtousevieditor

ReplacingCharacters

Toreplaceonecharacterwithanother: 1.Movethecursortothecharactertobereplaced. 2.Typer 3.Typethereplacementcharacter. Thenewcharacterwillappear,andyouwillstillbeincommandmode.

ReplacingWords

Toreplaceonewordwithanother,movetothestartoftheincorrectwordand type cw

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

35

Howtousevieditor Youarenowininsertmodeandmaytypethereplacement.Thenewtextdoes notneedtobethesamelengthastheoriginal.Press<Esc>togetbackto commandmode.Toreplacethreewords,type 3cw

ReplacingLines

Tochangetextfromthecursorpositiontotheendoftheline: 1.TypeC(uppercase). 2.Typethereplacementtext. 3.Press<Esc>.

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

36

Howtousevieditor

Movingaroundinafile Htotoplineofscreen Mtomiddlelineofscreen Ltolastlineofscreen Gtolastlineoffile 1Gtofirstlineoffile

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

37

Howtousevieditor

MovingbySearching

Tomovequicklybysearchingfortext,whileincommandmode: 1.Type/(slash). 2.Enterthetexttosearchfor. 3.Press<Return>. Thecursormovestothefirstoccurrenceofthattext. Torepeatthesearchinaforwarddirection,type n Torepeatthesearchinabackwarddirection,type N


5/27/11 CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 38

Howtousevieditor

Tosavetheeditsyouhavemade,butleavevirunningandyourfileopen: 1.Press<Esc>. 2.Type:w 3.Press<Return>.

Toquitvi,anddiscardanychangesyourhavemadesincelastsaving: 1.Press<Esc>. 2.Type:q! 3.Press<Return>.

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

39

AdministrativeCommands

top ps man find pipes w who


CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad 40

5/27/11

Networkcommands

ifconfig miitool netstat nmap

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

41

ThankYou

5/27/11

CentreforDevelopmentofAdvancedComputing,Hyderabad CentreforDevelopmentofAdvancedComputing,Hyderabad

42

You might also like