Sei sulla pagina 1di 33

LinuxShellCommands

Introduction
ManypeoplesaysthatLinuxisacommandbased operatingsystem. SomanyofusthinksthatLinuxisnotsouserfriendly OS. Butitisnottrue.LinuxisaGUIbasedOSwithaShell whichismorepowerfulthanitscounterpartinWindows OS. Wewillbefamiliarwithsomeshellcommands.

Identity

Typeuname andLinuxwilltellhisnametoyou Ifyouwanttoknowyournametypewhoami

Manual

ForeachcommandLinuxcontainsmanual.Toviewthe manual:manname
manuname

Editors
Toviewfilesalargenumberofeditorsareavailable. Theyare: kwrite emacs gedit vi Toview:editornamefilename kwritefile.txt

User

InLinux,rootisthemostpowerfuluser.Butotherusers canbecreatedeasily.Eachlinuxusermustbeunder certaingroup. Toaddagroup:groupaddgroup1 Todeleteagroup:groupdelgroup1 Toaddauser:useraddggroupnameusername Todeleteauser:userdelusername Tochangeauser:suuser1 Toupdatethepasswd:passwduser1

ViewText
Toviewalineoftextintheshell:echo echowelcometolinux Tocleartheshell:clear

DirectoryandFilePermissions
Eachfileordirectoryhas3securitygroups. Owner Group AllOthers Eachsecuritygrouphas3flagsthatcontroltheaccess status:read,write,execute Theyarelistedas'rwx'ora""iftheaccessisturnedoff. rwxrwxrwx [read,writeandexecutableforowner, groupandallothers] rwrr [readandwritebyowner,readonlyfor groupandallothers]

DirectoryandFilePermissions

Tochangethepermissionstypechmod u,g,oorall[whosepermissionyouarechanging] +or [typeofchange:addorsubtractpermission] combinationofr,worx[whichpermissionyouare changing:read,writeorexecute] fileordirectory[nameoffileordirectorytochange] chmodgo+rwfile1file2addreadandwrite accessforgroupandothersforfiles'file1'and 'file2 chmoda+rwxfile1addread,writeandexecute foreveryonefor'file1. chmod555file1

DirectoryandFilePermissions

Tochangetheownerofafileordirectorytypechown. chown username<fileordirectory> chownuser1file Tochangethegroupofafileordirectorytypechgrp. chgrp groupname<fileordirectory> chgrpgroup1file1file2

DirectoryandFileListings

Tolistinformationaboutdirectoryorfiles:ls Thiscommandcontainssomeoptions. a [donothideentriesstartingwith.] A [donotlistimplied.and..] h [printsizesinhumanreadableformat] l [usealonglistingformat] S [sortbyfilesize]


Permissions.Directories.Owner.Group.Size.Date.Name

drwxrwx .2.oracle.oinstall .1206.Jan2215:10.a

DirectoryOperations
Toprintthecurrentdirectory:pwd Tochangethecurrentdirectory:cddirname ThevariableHOMEisthedefaultdirectory. Tomakeanewdirectory:mkdir m[setpermissionmode(asinchmod)] v[printamessageforeachcreateddirectory Todeleteanemptydirectory:rmdir

DirectoryOperations
Tomovetoadirectorypushingthecurrentdirectoryto stack:pushddirname Effect: addsadirectorytothetopofthedirectorystack orrotatesthestackmakingthenewtopofthestack thecurrentworkingdirectory

DirectoryOperations
Tomovestothedirectoryatthetopofthestackaswell astoremovethetopmostentry:popd Effect: removesthetopdirectoryfromthestack performsacd tothenewtopdirectory.

DirectoryOperations
Todisplaythelistofcurrentlyremembereddirectories: dirs Thedefaultdisplayisonasinglelinewithdirectory namesseparatedbyspaces. Howtoaddtothelist:pushd Howtoremovefromthelist:popd

FileOperations

Tocopyafile:cp Copysourcetodestinationormultiplesourcesto directory i [promptbeforeoverwrite] r [copydirectoriesrecursively] u [copyonlywhenthesrcfileisnewerthanthedest fileorwhenthedestfileismissing

FileOperations

Toremoveafileordirectory:rm f [ignorenonexistentfiles,neverprompt] i [promptbeforeanyremoval] r [removethecontentsofdirectoriesrecursively] v [explainwhatisbeingdone]

FileOperations

Tomoveorrenameafile:mv renamesrctodestormovesrc(s)todirectory i [promptbeforeoverwrite] u [moveonlywhenthesrcfileisnewerthanthedest fileorwhenthedestfileismissing v [explainwhatisbeingdone]

FileOperations

Todeterminefiletype:file filename Filetestseachargumentinanattempttoclassifyit.This causesthefiletypetobeprinted i [showthemimetype]. v [Printtheversionofthefile] filea.txt:a.txt:veryshortfile filea.xls:a.xls:MicrosoftOfficeDocument filei a.xls:a.xls:\012 application/msword

FileOperations

Toconcatfilesandprintonthestandardoutput:cat file1file2file3 n [numberalloutputlines] s [nevermorethanonesingleblankline]

FileViewing
Toviewfilesinshelluse:more orless. morefilename lessfilename Themaindifferencebetweenmoreandlessisthat lessallowsbackwardandforwardmovementusing thearrowkeys. moreonlyusesthe[Spacebar]andthe[B]keyfor forwardandbackwardnavigation.

FileViewing

Tooutputthefirstlinesoffiles:headfile1file2file3 Printthefirst10linesofeachfiletostandardoutput Withmorethanonefile,precedeeachwithaheader givingthefilename n[outputthelastnlines,insteadofthelast10]

FileViewing

Tooutputthelastlinesoffiles:tailfile1file2file3 Printthelast10linesofeachfiletostandardoutput Withmorethanonefile,precedeeachwithaheader givingthefilename n [outputthelastnlines,insteadofthelast10]

FileViewing

Tosortlinesofatextfiles:sortfile1file2file3 Writesortedconcatenationofallfile(s)tostandard output.

FileViewing
Toprintthenumberoflines,wordsandbytesinfiles: wcfile1file2file3 printbyte,word,andnewlinecountsforeachfileanda totallineifmorethanonefileisspecified. l [printthenewlinecounts] w [printthewordcounts]

StandardI/O/E
Bydefault,threedefaultfilesknownasstandardfiles areautomaticallyopenedwhenacommandisexecuted. Theyarestandardinput(stdin),standardoutput (stdout)andstandarderror(stderr). Forexample,thecommandlsa scansthecurrent directoryandcollectsalistofallthefiles,producesa humanreadablelist,andoutputstheresulttothe terminalwindow.

Redirection
Linuxredirectionfeaturescanbeusedtodetachthe defaultfilesfromstdin,stdout andstderr andattach otherfilestothem. Inputredirection: < getinputfromfileinsteadofthekeyboard Outputredirection: > sendoutputtofileinsteadoftheterminal window Appendoutput: >> commandisusedtoappendtoafileifitalready exists

Piping
Theinputofacommandmaycomefromtheoutputof anothercommand. Thisisaccomplishedwiththe| pipeoperator.

Howtoviewthelines1520ofafilenameda.txt?

Piping
Theinputofacommandmaycomefromtheoutputof anothercommand. Thisisaccomplishedwiththe| pipeoperator.

Howtoviewthelines1520ofafilenameda.txt? head20a.txt|tail5

Grep
grepmatchesapatterninagivenalistoffilesor standardinputandoutputsonlythematchinglines. grep patternfilename grepabcfile.txt greppatternsarecasesensitivebydefault. Someoptions i [caseinsensitivesearch] c [countoftotalmatches] E [regularexpressionscanbeprovidedaspatterns] n [displaythelinenumbersofthematchedlines]

Find
searchforfilesinadirectoryhierarchy. Bydefault,findreturnsallfilesbelowthecurrent workingdirectory. find Tosearchapattern:findname'*txt*' Tosearchforafiletype: findtyped[findalldirectories] finetypef[findallregularfiles] Findexecutesthe'print'actionbydefault.Tochangeit tostylesuchasls:findtypefls

Find
Tosearchallthedirectories notrecommended find/name"myfile"typef Tosearchaspecificdirectory find/home/dir1name"myfile"typef Tosearchmultipledirectories finddir1dir2name"myfile"typef ToSearchforallfilesownedbyauser finduseruserid Totakeanaction findtypefname'*ch*'execchmoda+rwx{}\; {} isreplacedwiththenameofthefile The; indiatestheendofthecommand.

Thanks

Potrebbero piacerti anche