intro to linux - for training.odp
TRANSCRIPT
-
8/19/2019 Intro to Linux - for training.odp
1/62
1
Linux and Shell Programming
- Mohan
-
8/19/2019 Intro to Linux - for training.odp
2/62
2
Why this Course?
-
8/19/2019 Intro to Linux - for training.odp
3/62
3
• SECTION I Introduction to Linux
•
SECTION II Command line Interface
• SECTION III Shell Basics
• SECTION IV Pies and Commands
•
SECTION V
Cron !o"s
• SECTION VI Linux #ile Tree
Agenda
• SECTION VII Basic $n
•
SECTION VIII %e&ular
• SECTION I' VI Editor
• SECTION ' Scritin&
•
SECTION 'I
$ser (a
• SECTION 'II #ile Secu
-
8/19/2019 Intro to Linux - for training.odp
4/62
*
INTRO!CTION TO LIN!"
• SECTION I
+ ,o- Linux came into this -orld.
+ Linux /rchitecture and Common Linu
+ Various Linux 0istri"utions
+ Linux #ile S)stem
+ Boot Process ,ae )ou eer -ond
+ 0isla) (ana&er 4$I
-
8/19/2019 Intro to Linux - for training.odp
5/625
#o$ did the Linux %ame into this $orld?
LIN$S TO%V/L0S$SIN4 $NI'
#o$ it is maintained no$?
0EVELOPE% CO(($NIT6
(O0E%/TO%S
LIN$S TO%V/L0S
In 177289
-
8/19/2019 Intro to Linux - for training.odp
6/62
:
#istory• In 1771; Linus Toralds -rote his o-n OS e• Su"se?uentl) orted for other hard-are latforms9
&a%ts•
Linux is "ased on $NI'9• Linux is (ultiuser9• Linux uses a hierarchical file s)stem -ith to node called root @ / A• /ccess to the OS throu&h the shell• Offers a text "ased command interface
-
8/19/2019 Intro to Linux - for training.odp
7/62
Linux Ar%hite%ture and Common terminologies
OS /PPLIC/TION >E%NELBOOTLO/0E% 0/E(ON
S,ELL '11 IN0O (/N/4E% #ILES6STE( P/C>/4ESDP/C>/4E (/
-
8/19/2019 Intro to Linux - for training.odp
8/62
=
• >ernel Glue between Hardware and applications
• 0istri"ution Collection of software making up a Linux distribu
• Boot loader Program that boots the operating system (GR!" L#
• Serice Programs that run as background processes (httpd" ft
• Shell Command line #nterpreter
• '11 -indo- (ana&er Graphical subsystem for most of the Linux
• #ile s)stem ethod to store and organi)e files (ext*" ext+" ,
• Pac
-
8/19/2019 Intro to Linux - for training.odp
9/62
7
Linux istri'utions
0ifferences package management systems" software 1ersions" and fi
-
8/19/2019 Intro to Linux - for training.odp
10/62
1
&edora (CentOS, RHEL)• The Linux
-
8/19/2019 Intro to Linux - for training.odp
11/62
11
Some ?uestions -orth thin
-
8/19/2019 Intro to Linux - for training.odp
12/62
12
• / :ile system storin& and or&aniGin&
•
/ .artition is a lo&ical art of the dis
-
8/19/2019 Intro to Linux - for training.odp
13/62
13
Linux &ile System %ontd++Linux 2 /usr/bin
3in 2 C453indows5system*-
http4//www'pathname'com/fhs/pub/fhs2-'*'pdf - Become an Linux E
Linux 2 case2sensiti1e
3in 2 not case2sensiti1e
http://www.pathname.com/fhs/pub/fhs-2.3.pdfhttp://www.pathname.com/fhs/pub/fhs-2.3.pdfhttp://www.pathname.com/fhs/pub/fhs-2.3.pdf
-
8/19/2019 Intro to Linux - for training.odp
14/62
1*
The ;oot Pro%ess++
• hen the comuter is started; POST @Po-er On Self TestA is run9• BIOS is stored in the %O( chi9•
$sin& the information in "ios
-
8/19/2019 Intro to Linux - for training.odp
15/62
15
• The Boot loader is resonsi"le for loadin& the Oeratin& S)stem9
/st Stage < ;IOS)M;R method• Boot loader resides at 1st sector of hard dis< @(B%A• (B% also has the artition ta"le9• Boot loader finds the "oota"le artition and searches 2nd sta&e of "ootloader9
/st Stage < (&I)!(&I method• $E#I #irm-are contains a "oot mana&er -hich has the location of E#I artition9•
Boot loader resides in E#I artition in hard dis
-
8/19/2019 Intro to Linux - for training.odp
16/62
1:
The ;oot Pro%ess++
+ Besides startin& the s)stem; init is resonsi"le for
-
8/19/2019 Intro to Linux - for training.odp
17/62
1
• 4enerall); in a Linux des
-
8/19/2019 Intro to Linux - for training.odp
18/62
1=
• 7NOM( is the default to disla) mana&er in most of the enironment9
•
$nit); >0E; 'fce are the other des
-
8/19/2019 Intro to Linux - for training.odp
19/62
17
INTRO TO LiN!" CLI
• SECTION II
-
8/19/2019 Intro to Linux - for training.odp
20/62
2
• 2 a)s 4$I; CLI
•
NO overhead in CLI interface.
• How do you change your username and password in linux?
GI !GNO"#$ % System Preferences AboutMe change passwo
CLI % Open Terminal type ‘passwd’
• How do you enter &' files? How do you rename &' files?• hich is easier for reetitie tas
-
8/19/2019 Intro to Linux - for training.odp
21/62
21
Commands 0escrition
cd Chan&e 0irector)
tree 0isla) the 0irectories and files in tree structurepwd 0isla) current -or
-
8/19/2019 Intro to Linux - for training.odp
22/62
22
Try it 3oursel:
19 0isla) )our current director)9 Chan&e to the /etc director)9 Chec< )our current directo29 No- chan&e to )our home director) usin& onl) three
-
8/19/2019 Intro to Linux - for training.odp
23/62
23
Commands 0escritionle 0isla)s -hat t)e of file
touch Create an emt) file
touch -t Chan&e timestam
rm remoe a file
rm -f force remoe file
rm -rf %ecursie forcecp
(aing $ith :iles
+ #ileD0ir names are case sensitie+ Eer)thin& is a file @directories are secial casesA+ $se –i for interactie otion
-
8/19/2019 Intro to Linux - for training.odp
24/62
2*
Try it 3oursel:
19 List the files in the D"in director)929 0isla) the t)e of file of /bin/cat; /etc/passwd and /usr/bin/passwd9
39
0o-nload *:229df usin&wget http4//www'farnell'com/datasheets/+7--8
19 0isla) the t)e of file of +7--8'pdf29 %ename +7--8'pdf to datasheet'9pg @use mA939 0isla) the t)e of file of datasheet'9pg
*9 Create a director) 6/touched and enter it9 Create a emt) file today'txt in touched59 Create a file yesterday'txt to match )esterda)s date9:9 Co) yesterday'txt to copy'yesterday'txt
9 Create a director) called 6/testbackup and co) all files from 6/touched into it9=9 Create a su"director) subdir in the director) testbackup79 $se one command to remoe the director) 6/testbackup and all files into it9
19 Create a director) 6/etcbackup and co) all :'conf files from /etc into it9 0id )ousu"directories of /etc .
http://www.farnell.com/datasheets/46220.pdfhttp://www.farnell.com/datasheets/46220.pdfhttp://www.farnell.com/datasheets/46220.pdf
-
8/19/2019 Intro to Linux - for training.odp
25/62
25
Commands 0escrition
head -
disla) the first ten lines of a file
Tail - disla) the last ten lines of a filecat 0isla)Dconcatenate
cat > Create a file
tac 0isla) the file reerse @line orderA
more / less disla)in& files that taing $ith :ile %ontents
-
8/19/2019 Intro to Linux - for training.odp
26/62
-
8/19/2019 Intro to Linux - for training.odp
27/62
2
S#(LL CONC(PTS And Commands
• SECTION IV
S
-
8/19/2019 Intro to Linux - for training.odp
28/62
2=
• ar&uments• hite sace remoal @test -ith MechoA•
Sin&le ?uotes dou"le ?uotes @dou"le ?uotes allo- arsin& of aria"les; sin&le
• ‘which’ command -ill search for "inaries in the P/T, enironment aria"le• External or "uiltin commands. @use Mtype’ to chec
-
8/19/2019 Intro to Linux - for training.odp
29/62
27
19 ,o- man) ar&uments are in this line @not countin& the command itselfA9
touch
-
8/19/2019 Intro to Linux - for training.odp
30/62
3
139 Exlain the difference "et-een the follo-in& commandsF
echo
/bin/echo
1*9 Exlain the difference "et-een the follo-in& commandsF
echo Hello
echo 2n Hello
159 0isla) / B C -ith t-o saces "et-een B and C9
1:9 Comlete the follo-in& command @do not use sacesA to disla) exactl) the
follo-in& oututF
+?+ >@
A8?A+ >-+
19 $se echo to disla) the follo-in& exactl)F
..QQcommands and ar&uments
=5
#ind t-o solutions -ith sin&le ?uotes; t-o -ith dou"le ?uotes and one -ithout ?uo
1=9 $se one echo command to disla) three -ords on three lines9
Try it yoursel:
Sh ll @ i 'l
-
8/19/2019 Intro to Linux - for training.odp
31/62
31
• Permanent shell aria"les called Enironment aria"les; often needed ") alica• Shell aria"les can "e created temoraril) li
-
8/19/2019 Intro to Linux - for training.odp
32/62
32
19 $se echo to disla) ,ello follo-ed ") )our username9 @use a "ash aria"leA
29 Create a aria"le ans-er -ith a alue of *29
39 Co) the alue of L/N4 to ()L/N49
*9 List all current shell aria"les9
59 List all exorted shell aria"les9
:9 0o the en and set commands disla) )our aria"le .
:9 0estro) )our ans-er aria"le9
9 Create t-o aria"les; and exort one of them9
=9 0isla) the exorted aria"le in an interactie child shell9
79 Create a aria"le; &ie it the alue 0um"; create another aria"le -ith alue decho and the t-o aria"les to echo 0um"ledore9
19 #ind the list of "ac
-
8/19/2019 Intro to Linux - for training.odp
33/62
33
• #ind default shell H &re K$SE%N/(EH DetcDass-d• #ind all Shells aaila"le H cat DetcDshells• Chan&e shell H !ust t)e the shell name• Chan&e default shell H csh
i::erent ty.es o: Shell++
%ontrol o.erators
-
8/19/2019 Intro to Linux - for training.odp
34/62
3*
%ontrol o.erators
Oerator 0escrition Examles
U (ultile commands run se?uentiall) echo ,ello U echo o
%un "inaries in the "ac
-
8/19/2019 Intro to Linux - for training.odp
35/62
35
Each ?uestion can "e ans-ered ") one command line
19 hen )ou t)e ass-d; -hich file is executed .
29 hat
-
8/19/2019 Intro to Linux - for training.odp
36/62
3:
Examle Commands 0escritions
$ arro- Nai&ation Execute the last command
Execute the last command
ec Execute the last command that started -ith Mec
histor) 1 ,istor) of last 1@nA commands9
*11 Executes the *11th @nthA command9
echo ,IST#ILE The commands are stored in 9"ash[histor)
Shell #istory
Try it 3oursel:
-
8/19/2019 Intro to Linux - for training.odp
37/62
3
19 Issue the command echo Hi welcome
29 %eeat the reious command usin& onl) t-o characters @there are t-o soluti
39 0isla) the last 5 commands )ou t)ed9
*9 Issue the lon& echo from ?uestion 1 a&ain; usin& the line num"ers )ou receiecommand in ?uestion 39
59 here are these commands stored -hen exitin& the shell .
Try it 3oursel:
&ile 7lo''ing < generating :ile names
-
8/19/2019 Intro to Linux - for training.odp
38/62
3=
Examles
Y &enerate filenames; matchin& the asteris<to an) com"ination of characters @een noneA9
ls #ileY
. matchin& the ?uestion mar< -ith exactl) one character9 ls file. ls file..
Z \ an) of the characters "et-een Z and the first su"se?uent \ ls fileZ/7\ fileZaG\
&ile 7lo''ing generating :ile names
Try it 3oursel:
19 Create a test director) and enter it929 Create the follo-in& files F fileA fileA8 fileAA file- ,ile- ,ile* file
fileB! filea fileB fileBBB file( file -
Try it 3oursel:
-
8/19/2019 Intro to Linux - for training.odp
39/62
37
39 List @-ith lsA all files startin& -ith file
*9 List @-ith lsA all files startin& -ith #ile
59 List @-ith lsA all files startin& -ith file and endin& in a num"er9
:9 List @-ith lsA all files startin& -ith file and endin& -ith a letter
9 List @-ith lsA all files startin& -ith #ile and hain& a di&it as fifth character9
=9 List @-ith lsA all files startin& -ith #ile and hain& a di&it as fifth character and no
79 List @-ith lsA all files startin& -ith a letter and endin& in a num"er9
19 List @-ith lsA all files that hae exactl) fie characters9
119 List @-ith lsA all files that start -ith f or # and end -ith 3 or /9
129 List @-ith lsA all files that start -ith f hae i or % as second character and end in139 List all files that do not start -ith the letter #9
Try it 3oursel:
-
8/19/2019 Intro to Linux - for training.odp
40/62
*
PIP(S AN COMMANS
• SECTION V
In.ut)Out.ut Redire%tion
-
8/19/2019 Intro to Linux - for training.odp
41/62
*1
• Three "asic streams @stream; stream1; stream2A
. )O .
BashStdin@KA
• BStdout
Bash
-
8/19/2019 Intro to Linux - for training.odp
42/62
*2
• Redire%tion and .i.es ST0O$T @orA ST0E%% of one command to ST0Icommand
• BB a..end aend to the EO#
• stdin &re somethin& @orA &re somethin& K some[file
&ILT(RS
-
8/19/2019 Intro to Linux - for training.odp
43/62
*3
Examles
cat uttin& stdin on stdout tac sdf9txt cat cat
tee uttin& stdin on t-o stdout cat sdf9txt tee asdf
&re lines matchin& the strin&9 cat sdf9txt &re sac&re lines not matchin& the strin&9 cat sdf9txt &re s
&re i Case insensitie cat sdf9txt &re i s
cut Select columns from files; deendin& on a delimiter cut dF f1;3 DetcDasecho 1F2F3F*F5 cut
translate Translate the characters cat sdf9txt tr e EMcat sdf9txt tr aG /
cat sdf9txt tr aG Mncat sdf9txt tr s M
-c Count lines; -ords; -c l sdf9txt-c --c c
-
8/19/2019 Intro to Linux - for training.odp
44/62
**
• -ho -c l• -ho cut d f1 sort• -ho cut d f1 sort uni?• &re "ash DetcDass-d cut dF f1
0escrition Examles
sort Sort in order sort sdf9txt
sort
-
8/19/2019 Intro to Linux - for training.odp
45/62
*5
19 Put a sorted list of all "ash users in "ashusers9txt9
29 Put a sorted list of all lo&&ed on users in onlineusers9txt9
39 (a
-
8/19/2019 Intro to Linux - for training.odp
46/62
*:
Commands 0escrition
man (anual a&e of a command
man (anual a&e of a command
man (anual a&e of a commandman –k List all releant commands -ith one line descrition
apropos Same as a"oe
whatis List the commands one line descrition
whereis 0isla)s the manual a&e location
mand& Indexes the manual entries
Tr) it 6ourself #ind -hat is the command to moe a file9 here is the manual located.hich89,el84o to "in dir
-
8/19/2019 Intro to Linux - for training.odp
47/62
*
&IL(S(C!RIT39 CRON DO;S
• SECTION I'
!sers and 7rou.s r - x
-
8/19/2019 Intro to Linux - for training.odp
48/62
*=
+f you don’t ha,e access to a -le then you cant change its permissions
T)e of #ile %e&ular #ile
d 0irector)
lS)m"olic Lin<DetcDmotd
Named Pie
" Bloc< deiceDdeDsda
cCharacter deiceDdeDconsole
s soc
-
8/19/2019 Intro to Linux - for training.odp
49/62
*7
• $sers and &rous Mls lh Mcut 2d4 2fA /etc/passwd D column
+f you don’t ha,e access to a -le then you cant change its permissions
ch&r K&rounameHKfilenameH Chan&e &rou ch&r
cho-n KusernameH KfilenameH Chan&e O-ner
chmod chmod &r
chmod uxchmod or
chmod a-
TR3 IT 3O!RS(L&
-
8/19/2019 Intro to Linux - for training.odp
50/62
5
• 19 /s normal user; create a director) JDermissions9 Create a file o-ned ") )ours• 29 Co) a file o-ned ") root from DetcD to )our ermissions dir; -ho o-ns this file• 39 /s root; create a file in the users JDermissions director)9• *9 /s normal user; loo< at -ho o-ns this file created ") root9• 59 Chan&e the o-nershi of all files in JDermissions to )ourself9• :9 (a
-
8/19/2019 Intro to Linux - for training.odp
51/62
51
• crontab $l lists all the scheduled tas
-
8/19/2019 Intro to Linux - for training.odp
52/62
52
• Create Mcron director) in home folder9• $se the date command to see the current timin&9• Schedule a ^o" that uts M,i elcome in the file Mhi9txt9 Mhi9txt should "e in the di• Schedule a ^o" to aend date and time eer) minute9
-
8/19/2019 Intro to Linux - for training.odp
53/62
53
MISS( ;ASIC !NI" TOOLS
• SECTION VI
-
8/19/2019 Intro to Linux - for training.odp
54/62
5*
find
locate
date
cal
slee
time
&Gi
Gcat
"Gi and "Gi2
tar
TR3 IT 3O!RS(L&
-
8/19/2019 Intro to Linux - for training.odp
55/62
55
19 Exlain the difference "et-een these t-o commands9 This ?uestion is er) im
)ou dont
-
8/19/2019 Intro to Linux - for training.odp
56/62
5:
:9 T-o commands that do the same thin&F co) Y9odf files to D"ac
-
8/19/2019 Intro to Linux - for training.odp
57/62
5
Linux &ile tree
• SECTION III
Linux :ile tree
-
8/19/2019 Intro to Linux - for training.odp
58/62
5=
• D"in• "in "inaries for all users• s"in "inaries to confi&ure the oeratin& s)stem
• Dli"• D"in and Ds"in often use shared li"rarie• Li"32 and li":*
• Dot• Dot is to store otional soft-are• Emt) at the start
Linux :ile tree < %on:iguration and data dire%tories
-
8/19/2019 Intro to Linux - for training.odp
59/62
57
• Detc• Edita"le Text Confi&uration• Contains machinesecific confi&uration files
• D"oot• D"oot director) contains all files needed to "oot the
comuter • 9cf& or 9conf
init9d Start and sto daemons
x11 DetcD'11Dxor&9conf
s
-
8/19/2019 Intro to Linux - for training.odp
60/62
:
(emor) 0irectories
Dde oulated -ith files as the
-
8/19/2019 Intro to Linux - for training.odp
61/62
:1
19 0oes the file D"inDcat exist . hat a"out D"inDdd and D"inDecho9 hat is the t)e of thesefiles .29 hat is the siGe of the Linux
-
8/19/2019 Intro to Linux - for training.odp
62/62
:2
:9 $se cat to disla) DetcDhosts and DetcDresol9conf9 hat is )our idea a"out the
of these files .
9 /re there an) files in DetcDs