1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Using Linux Tools
%
% Author: FOSSEE
% Copyright (c) 2009, FOSSEE, IIT Bombay
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[12pt,compress]{beamer}
\mode<presentation>
{
\usetheme{Warsaw}
\useoutertheme{infolines}
\setbeamercovered{transparent}
}
\usepackage[english]{babel}
\usepackage[latin1]{inputenc}
%\usepackage{times}
\usepackage[T1]{fontenc}
% Taken from Fernando's slides.
\usepackage{ae,aecompl}
\usepackage{mathpazo,courier,euler}
\usepackage[scaled=.95]{helvet}
\definecolor{darkgreen}{rgb}{0,0.5,0}
\usepackage{listings}
\lstset{language=sh,
basicstyle=\ttfamily\bfseries,
commentstyle=\color{red}\itshape,
stringstyle=\color{darkgreen},
showstringspaces=false,
keywordstyle=\color{blue}\bfseries}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Macros
\setbeamercolor{emphbar}{bg=blue!20, fg=black}
\newcommand{\emphbar}[1]
{\begin{beamercolorbox}[rounded=true]{emphbar}
{#1}
\end{beamercolorbox}
}
\newcounter{time}
\setcounter{time}{0}
\newcommand{\inctime}[1]{\addtocounter{time}{#1}{\tiny \thetime\ m}}
\newcommand{\typ}[1]{\lstinline{#1}}
\newcommand{\kwrd}[1]{ \texttt{\textbf{\color{blue}{#1}}} }
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Title page
\title[Using Linux Tools]{SEES: Using Linux Tools}
\author[FOSSEE] {FOSSEE}
\institute[IIT Bombay] {Department of Aerospace Engineering\\IIT Bombay}
\date[]{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\pgfdeclareimage[height=0.75cm]{iitmlogo}{iitmlogo}
%\logo{\pgfuseimage{iitmlogo}}
%% Delete this, if you do not want the table of contents to pop up at
%% the beginning of each subsection:
\AtBeginSection[]
{
\begin{frame}<beamer>
\frametitle{Outline}
\tableofcontents[currentsection]
\end{frame}
}
% If you wish to uncover everything in a step-wise fashion, uncomment
% the following command:
%\beamerdefaultoverlayspecification{<+->}
%%\includeonlyframes{current,current1,current2,current3,current4,current5,current6}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DOCUMENT STARTS
\begin{document}
\begin{frame}
\maketitle
\end{frame}
% CREATING TOC
\begin{frame}
\frametitle{Outline}
\tableofcontents
% You might wish to add the option [pausesections]
\end{frame}
\section{Introduction}
\begin{frame}[fragile]
\begin{block}{What is the Linux OS?}
\begin{itemize}
\item Free Open Source Operating System
\begin{description}
\item[Free]
Free as in Free Speech, not Free Beer
\item[Open-Source]
Permit modifications and redistribution of source code
\end{description}
\item Unix-inspired
\item Linux Kernel + Application software
\item Runs on a variety of hardware
\item Also called GNU/Linux
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{Why Linux?}
\begin{itemize}
\item Free as in Free Beer
\item Secure \& versatile
\end{itemize}
\begin{block}{Why Linux for Scientific Computing?}
\begin{itemize}
\item Free as in Free Speech
\item Can run for \emph{ever}
\item Libraries
\item Parallel Computing
\end{itemize}
\end{block}
\end{frame}
\section{Getting Started}
\begin{frame}[fragile]
\frametitle{Logging in}
\begin{itemize}
\item GNU/Linux does have a GUI
\item Command Line for this module
\item Hit \texttt{Ctrl + Alt + F1}
\item Login
\item \texttt{logout} command logs you out
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Where am I?}
\begin{itemize}
\item Logged in. Where are we?
\item \texttt{pwd} command gives the present working directory
\end{itemize}
\begin{lstlisting}
$ pwd
/home/user
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{What is in there?}
\begin{itemize}
\item \texttt{ls} command lists contents of \texttt{pwd}
\end{itemize}
\begin{lstlisting}
$ ls
jeeves.rst psmith.html blandings.html Music
\end{lstlisting} %$
\begin{itemize}
\item Can also pass directory as argument
\end{itemize}
\begin{lstlisting}
$ ls Music
one.mp3 two.mp3 three.mp3
\end{lstlisting} %$
\begin{itemize}
\item \alert{the Unix world is case sensitive}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{New folders}
\begin{itemize}
\item \texttt{mkdir} creates new directories
\end{itemize}
\begin{lstlisting}
$ mkdir sees
$ ls
\end{lstlisting}
\begin{itemize}
\item Special characters need to be escaped OR quoted
\end{itemize}
\begin{lstlisting}
$ mkdir software\ engineering
$ mkdir "software engg"
\end{lstlisting}
\begin{itemize}
\item Generally, use hyphens or underscores instead of spaces in names
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Moving around}
\begin{itemize}
\item \texttt{cd} command changes the \texttt{pwd}
\end{itemize}
\begin{lstlisting}
$ cd sees
$ pwd
/home/user/sees/
\end{lstlisting}
\begin{itemize}
\item Alternately written as \texttt{cd ./sees}
\item Specifying path relative to \texttt{pwd}
\item \texttt{..} takes one level up the directory structure
\end{itemize}
\begin{lstlisting}
$ cd ..
\end{lstlisting} % $
\begin{itemize}
\item We could use absolute path instead of relative
\end{itemize}
\begin{lstlisting}
$ cd /home/user/sees/
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{New files}
\begin{itemize}
\item \texttt{touch} command creates a blank file
\end{itemize}
\begin{lstlisting}
$ pwd
/home/user
$ cd sees
$ touch first
$ ls
first
\end{lstlisting} % $
\end{frame}
\section{Getting Help}
\begin{frame}[fragile]
\frametitle{What does a command do?}
\begin{itemize}
\item \texttt{whatis} gives a quick description of a command
\end{itemize}
\begin{lstlisting}
$ whatis touch
touch (1) - change file timestamps
\end{lstlisting} % $
\begin{itemize}
\item \texttt{man} command gives more detailed description
\end{itemize}
\begin{lstlisting}
$ man touch
\end{lstlisting} % $
\begin{itemize}
\item Shows all tasks that the command can perform
\item Hit \texttt{q} to quit the \texttt{man} page
\item For more, see the \texttt{man} page of \texttt{man}
\end{itemize}
\begin{lstlisting}
$ man man
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Using additional options}
\begin{itemize}
\item \texttt{-h} or \texttt{--help} give summary of command usage
\end{itemize}
\begin{lstlisting}
$ ls --help
\end{lstlisting} % $
\begin{itemize}
\item List out all files within a directory, recursively
\end{itemize}
\begin{lstlisting}
$ ls -R
\end{lstlisting} % $
\begin{itemize}
\item Create a new directory along with parents, if required
\end{itemize}
\begin{lstlisting}
$ pwd
/home/user/
$ ls sees/
$ mkdir -p sees/linux-tools/scripts
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Searching for a command}
\begin{itemize}
\item \texttt{apropos} searches commands based on their descriptions
\end{itemize}
\begin{lstlisting}
$ apropos remove
\end{lstlisting} % $
\begin{itemize}
\item Returns a list of all commands that contain the search term
\item In this case, we are interested in \texttt{rm}, \texttt{rmdir}
\end{itemize}
\end{frame}
\section{Basic File Handling}
\begin{frame}[fragile]
\frametitle{Removing files}
\begin{itemize}
\item \texttt{rm} is used to delete files
\end{itemize}
\begin{lstlisting}
$ rm foo
\end{lstlisting} % $
\begin{itemize}
\item \alert{\texttt{rm} works only for files; not directories}
\end{itemize}
\begin{itemize}
\item Additional arguments required to remove a directory
\item \texttt{-r} stands for recursive.
\item Removes directory and all of it's content
\end{itemize}
\begin{lstlisting}
$ rm -r bar
\end{lstlisting} % $
\begin{itemize}
\item \alert{\texttt{rmdir} can also be used; Explore}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Copying Files}
\begin{itemize}
\item \texttt{cp} copies files from one location to another
\end{itemize}
\begin{lstlisting}
$ cp linux-tools/scripts/foo linux-tools/
\end{lstlisting} % $
\begin{itemize}
\item New file-name can be used at target location
\item \texttt{foo} copied to new location with the name \texttt{bar}
\end{itemize}
\begin{lstlisting}
$ cp linux-tools/scripts/foo linux-tools/bar
\end{lstlisting} % $
\begin{itemize}
\item \texttt{cp} overwrites files, unless explicitly asked not to
\item To prevent this, use the \texttt{-i} flag
\end{itemize}
\begin{lstlisting}
$ cp -i linux-tools/scripts/foo linux-tools/bar
cp: overwrite `bar'?
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Copying Directories}
\begin{itemize}
\item \texttt{-r} is required to copy a directory and all it's
content
\item Copying directories is similar to copying files
\end{itemize}
\begin{lstlisting}
$ cd /home/user
$ cp -ir sees course
\end{lstlisting}
\end{frame}
\begin{frame}[fragile]
\frametitle{Moving Files}
\begin{itemize}
\item \texttt{cp} and \texttt{rm} would be one way
\item \texttt{mv} command does the job
\item Also takes \texttt{-i} option to prompt before overwriting
\end{itemize}
\begin{lstlisting}
$ cd /home/user
# Assume we have course directory already created
$ mv -i sees/ course/
\end{lstlisting}
\begin{itemize}
\item No prompt! Why?
\end{itemize}
\begin{lstlisting}
$ ls course
\end{lstlisting} % $
\begin{itemize}
\item \texttt{sees} became a sub-directory of \texttt{course}
\item \texttt{mv} command doesn't over-write directories
\item \texttt{-i} option is useful when moving files around
\item \texttt{mv} to rename --- move to same location with new name
\end{itemize}
\end{frame}
\section{Linux File Hierarchy, Permissions \& Ownership}
\begin{frame}
\frametitle{Linux File Hierarchy}
\begin{itemize}
\item \texttt{/} is called the root directory
\item It is the topmost level of the hierarchy
\item For details \texttt{man hier}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Permissions and Access control}
\begin{itemize}
\item In a multi-user environment, access control is vital
\item Look at the output of \texttt{ls -l}
\end{itemize}
\begin{lstlisting}
drwxr-xr-x 5 root users 4096 Jan 21 20:07 home
\end{lstlisting} % $
\begin{itemize}
\item The first column shows the permission information
\item First character specifies type of the file
\item Files have \texttt{-}; Directories have \texttt{d}
\item 3 sets of 3 characters --- for user, group and others
\item \texttt{r}, \texttt{w}, \texttt{x} --- for read, write, execute
\item Either the corresponding character or \texttt{-} is present
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Changing the permissions}
\begin{itemize}
\item Permissions can be changed by owner of the file
\item \texttt{chmod} command is used
\item \texttt{-R} option to recursively change for all content of a
directory
\end{itemize}
\begin{itemize}
\item Change permissions of \texttt{foo.sh} from
\texttt{-rw-r-{}-r-{}-} to \texttt{-rwxr-xr-{}-}
\end{itemize}
\begin{lstlisting}
$ ls -l foo.sh
$ chmod ug+x foo.sh
$ ls -l foo.sh
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Symbolic modes}
\begin{small}
\begin{center}
\begin{tabular}{lll}
Reference & Class & Description \\
\hline
u & user & the owner of the file \\
g & group & users who are members of the file's group \\
o & others & users who are not hte owner of the file or members of the group \\
a & all & all three of the above; is the same as \emph{ugo} \\
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{ll}
Operator & Description \\
\hline
+ & adds the specified modes to the specified classes \\
- & removes the specified modes from the specified classes \\
= & the modes specified are to be made the exact modes for the specified classes \\
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{lll}
Mode & Name & Description \\
\hline
r & read & read a file or list a directory's contents \\
w & write & write to a file or directory \\
x & execute & execute a file or recurse a directory tree \\
\end{tabular}
\end{center}
\end{small}
\end{frame}
\begin{frame}[fragile]
\frametitle{Changing Ownership of Files}
\begin{itemize}
\item \texttt{chown} changes the owner and group
\item By default, the user who creates file is the owner
\item The default group is set as the group of the file
\end{itemize}
\begin{lstlisting}
$ chown alice:users wonderland.txt
\end{lstlisting} % $
\begin{itemize}
\item Did it work? \alert{Not every user can change ownership}
\item Super-user or \texttt{root} user alone is empowered
\end{itemize}
\end{frame}
\section{Looking at files}
\begin{frame}[fragile]
\frametitle{\texttt{cat}}
\begin{itemize}
\item Displays the contents of files
\end{itemize}
\begin{lstlisting}
$ cat foo.txt
\end{lstlisting} % $
\begin{itemize}
\item Concatenates the text of multiple files
\end{itemize}
\begin{lstlisting}
$ cat foo.txt bar.txt
\end{lstlisting} % $
\begin{itemize}
\item Not-convenient to view long files
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{less}}
\begin{itemize}
\item View contents of a file one screen at a time
\end{itemize}
\begin{lstlisting}
$ less wonderland.txt
\end{lstlisting} % $
\begin{itemize}
\item q: Quit
\item Arrows/Page Up/Page Down/Home/End: Navigation
\item ng: Jump to line number n
\item /pattern: Search. Regular expressions can be used
\item h: Help
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{wc}}
\begin{itemize}
\item Statistical information about the file
\item the number of lines in the file
\item the number of words
\item the number of characters
\end{itemize}
\begin{lstlisting}
$ wc wonderland.txt
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{head} \& \texttt{tail}}
\begin{itemize}
\item let you see parts of files, instead of the whole file
\item \texttt{head} -- start of a file; \texttt{tail} -- end of a
file
\item show 10 lines by default
\end{itemize}
\begin{lstlisting}
$ head wonderland.txt
\end{lstlisting} % $
\begin{itemize}
\item \texttt{-n} option to change the number of lines
\end{itemize}
\begin{lstlisting}
$ head -n 1 wonderland.txt
\end{lstlisting} % $
\begin{itemize}
\item \texttt{tail} is commonly used to monitor files
\item \texttt{-f} option to monitor the file
\item \texttt{Ctrl-C} to interrupt
\end{itemize}
\begin{lstlisting}
$ tail -f /var/log/dmesg
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{cut}}
\begin{itemize}
\item Allows you to view only certain sections of lines
\item Let's take \texttt{/etc/passwd} as our example
\end{itemize}
\begin{lstlisting}
root:x:0:0:root:/root:/bin/bash
\end{lstlisting}
\begin{itemize}
\item View only user names of all the users (first column)
\end{itemize}
\begin{lstlisting}
$ cut -d : -f 1 /etc/passwd
\end{lstlisting} % $
\begin{itemize}
\item \texttt{-d} specifies delimiter between fields (default TAB)
\item \texttt{-f} specifies the field number
\item Multiple fields by separating field numbers with comma
\end{itemize}
\begin{lstlisting}
$ cut -d : -f 1,5,7 /etc/passwd
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{cut}}
\begin{itemize}
\item Allows choosing on the basis of characters or bytes
\item Example below gets first 4 characters of \texttt{/etc/passwd}
\end{itemize}
\begin{lstlisting}
$ cut -c 1-4 /etc/passwd
\end{lstlisting} % $
\begin{itemize}
\item One of the limits of the range can be dropped
\item Sensible defaults are assumed in such cases
\end{itemize}
\begin{lstlisting}
$ cut -c -4 /etc/passwd
$ cut -c 10- /etc/passwd
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{paste}}
\begin{itemize}
\item Joins corresponding lines from two different files
\begin{center}
\begin{tabular}{l|l}
\verb~students.txt~ & \verb~marks.txt~ \\
Hussain & 89 92 85 \\
Dilbert & 98 47 67 \\
Anne & 67 82 76 \\
Raul & 78 97 60 \\
Sven & 67 68 69 \\
\end{tabular}
\end{center}
\end{itemize}
\begin{lstlisting}
$ paste students.txt marks.txt
$ paste -s students.txt marks.txt
\end{lstlisting}
\begin{itemize}
\item \texttt{-s} prints content, one below the other
\item If first column of marks file had roll numbers? How do we get
a combined file with the same output as above (i.e. without roll
numbers). We need to use \texttt{cut} \& \texttt{paste} together.
But how?
\end{itemize}
\end{frame}
\section{The Command Shell}
\begin{frame}[fragile]
\frametitle{Redirection and Piping}
\begin{lstlisting}
$ cut -d " " -f 2- marks1.txt \
> /tmp/m_tmp.txt
$ paste -d " " students.txt m_tmp.txt
\end{lstlisting} % $
or
\begin{lstlisting}
$ cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt -
\end{lstlisting} % $
\begin{itemize}
\item The first solution used Redirection
\item The second solution uses Piping
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Redirection}
\begin{itemize}
\item The standard output (stdout) stream goes to the display
\item Not always, what we need
\item First solution, redirects output to a file
\item \texttt{>} states that output is redirected; It is
followed by location to redirect
\end{itemize}
\begin{lstlisting}
$ command > file1
\end{lstlisting} % $
\begin{itemize}
\item \texttt{>} creates a new file at specified location
\item \texttt{>>} appends to a file at specified location
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Redirection \ldots}
\begin{itemize}
\item Similarly, the standard input (stdin) can be redirected
\end{itemize}
\begin{lstlisting}
$ command < file1
\end{lstlisting} % $
\begin{itemize}
\item input and the output redirection could be combined
\end{itemize}
\begin{lstlisting}
$ command < infile > outfile
\end{lstlisting} % $
\begin{itemize}
\item Standard error (stderr) is the third standard stream
\item All error messages come through this stream
\item \texttt{stderr} can also be redirected
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Redirection \ldots}
\begin{itemize}
\item Following example shows \texttt{stderr} redirection
\item Error is printed out in the first case
\item Error message is redirected, in the second case
\end{itemize}
\begin{lstlisting}
$ cut -d " " -c 2- marks1.txt \
> /tmp/m_tmp.txt
$ cut -d " " -f 2- marks1.txt 1> \
/tmp/m_tmp.txt 2> /tmp/m_err.txt
\end{lstlisting} % $
\begin{itemize}
\item \texttt{1>} redirects \texttt{stdout}; \texttt{2>} redirects
\texttt{stderr}
\end{itemize}
\begin{lstlisting}
$ paste -d " " students.txt m_tmp.txt
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Piping}
\begin{lstlisting}
$ cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt -
\end{lstlisting} % $
\begin{itemize}
\item \texttt{-} instead of FILE asks \texttt{paste} to read from
\texttt{stdin}
\item \texttt{cut} command is a normal command
\item the \texttt{|} seems to be joining the two commands
\item Redirects output of first command to \texttt{stdin}, which
becomes input to the second command
\item This is called piping; \texttt{|} is called a pipe
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Piping}
\begin{itemize}
\item Roughly same as -- 2 redirects and a temporary file
\end{itemize}
\begin{lstlisting}
$ command1 > tempfile
$ command2 < tempfile
$ rm tempfile
\end{lstlisting} % $
\begin{itemize}
\item Any number of commands can be piped together
\end{itemize}
\end{frame}
\subsection{Features of the Shell}
\begin{frame}[fragile]
\frametitle{Tab-completion}
\begin{itemize}
\item Hit tab to complete an incompletely typed word
\item Tab twice to list all possibilities when ambiguous completion
\item Bash provides tab completion for the following.
\begin{enumerate}
\item File Names
\item Directory Names
\item Executable Names
\item User Names (when they are prefixed with a \~{})
\item Host Names (when they are prefixed with a @)
\item Variable Names (when they are prefixed with a \$)
\end{enumerate}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{History}
\begin{itemize}
\item Bash saves history of commands typed
\item Up and down arrow keys allow to navigate history
\item \texttt{Ctrl-r} searches for commands used
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Shell Meta Characters}
\begin{itemize}
\item ``meta characters'' are special command directives
\item File-names shouldn't have meta-characters
\item \verb+/<>!$%^&*|{}[]"'`~;+
\end{itemize}
\begin{lstlisting}
$ ls file.*
\end{lstlisting} % $
\begin{itemize}
\item Lists \texttt{file.ext} files, where \texttt{ext} can be
anything
\end{itemize}
\begin{lstlisting}
$ ls file.?
\end{lstlisting} % $
\begin{itemize}
\item Lists \texttt{file.ext} files, where \texttt{ext} is only one
character
\end{itemize}
\end{frame}
\section{More text processing}
\begin{frame}[fragile]
\frametitle{\texttt{sort}}
\begin{itemize}
\item \texttt{sort} can be used to get sorted content
\item Command below prints student marks, sorted by name
\end{itemize}
\begin{lstlisting}
$ cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt - \
| sort
\end{lstlisting} % $
\begin{itemize}
\item The default is sort based on the whole line
\item \texttt{sort} can sort based on a particular field
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{sort} \ldots}
\begin{itemize}
\item The command below sorts based on marks in first subject
\end{itemize}
\begin{lstlisting}
$ cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt -\
| sort -t " " -k 2 -rn
\end{lstlisting} % $
\begin{itemize}
\item \texttt{-t} specifies the delimiter between fields
\item \texttt{-k} specifies the field to use for sorting
\item \texttt{-n} to choose numerical sorting
\item \texttt{-r} for sorting in the reverse order
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{grep}}
\begin{itemize}
\item \texttt{grep} is a command line text search utility
\item Command below searches \& shows the marks of Anne alone
\end{itemize}
\begin{lstlisting}
$ cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt -
| grep Anne
\end{lstlisting} % $
\begin{itemize}
\item \texttt{grep} is case-sensitive by default
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{grep} \ldots}
\begin{itemize}
\item \texttt{-i} for case-insensitive searches
\end{itemize}
\begin{lstlisting}
$ cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt -
| grep -i Anne
\end{lstlisting} % $
\begin{itemize}
\item \texttt{-v} inverts the search
\item To see everyone's marks except Anne's
\end{itemize}
\begin{lstlisting}
$ cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt -
| grep -iv Anne
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{tr}}
\begin{itemize}
\item \texttt{tr} translates or deletes characters
\item Reads from \texttt{stdin} and outputs to \texttt{stdout}
\item Given, two sets of characters, replaces one with other
\item The following, replaces all lower-case with upper-case
\end{itemize}
\begin{lstlisting}
$ cat students.txt | tr a-z A-Z
\end{lstlisting} % $
\begin{itemize}
\item \texttt{-s} compresses sequences of identical adjacent
characters in the output to a single one
\item Following command removes empty newlines
\end{itemize}
\begin{lstlisting}
$ tr -s '\n' '\n'
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{tr} \ldots}
\begin{itemize}
\item \texttt{-d} deletes all specified characters
\item Only a single character set argument is required
\item The following command removes carriage return characters
(converting file in DOS/Windows format to the Unix format)
\end{itemize}
\begin{lstlisting}
$ cat foo.txt | tr -d '\r' > bar.txt
\end{lstlisting} % $
\begin{itemize}
\item \texttt{-c} complements the first set of characters
\item The following command removes all non-alphanumeric characters
\end{itemize}
\begin{lstlisting}
$ tr -cd '[:alnum:]'
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{uniq}}
\begin{itemize}
\item \texttt{uniq} command removes duplicates from \alert{sorted} input
\end{itemize}
\begin{lstlisting}
$ sort items.txt | uniq
\end{lstlisting} % $
\begin{itemize}
\item \texttt{uniq -u} gives lines which do not have any duplicates
\item \texttt{uniq -d} outputs only those lines which have duplicates
\item \texttt{-c} displays the number of times each line occurs
\end{itemize}
\begin{lstlisting}
$ sort items.txt | uniq -u
$ sort items.txt | uniq -dc
\end{lstlisting} % $
\end{frame}
\section{Simple Shell Scripts}
\begin{frame}[fragile]
\frametitle{Shell scripts}
\begin{itemize}
\item Simply a sequence of shell commands in a file
\item To save results of students in \texttt{results.txt} in
\texttt{marks} dir
\end{itemize}
\begin{lstlisting}
#!/bin/bash
mkdir ~/marks
cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt - \
| sort > ~/marks/results.txt
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Shell scripts \ldots}
\begin{itemize}
\item Save the script as \texttt{results.sh}
\item Make file executable and then run
\end{itemize}
\begin{lstlisting}
$ chmod u+x results.sh
$ ./results.sh
\end{lstlisting} % $
\begin{itemize}
\item What does the first line in the script do?
\item Specify the interpreter or shell which should be used to
execute the script; in this case \texttt{bash}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Variables \& Comments}
\begin{lstlisting}
$ name=FOSSEE
$ count=`wc -l wonderland.txt`
$ echo $count # Shows the value of count
\end{lstlisting} % $
\begin{itemize}
\item It is possible to create variables in shell scripts
\item Variables can be assigned with the output of commands
\item \alert{NOTE:} There is no space around the \texttt{=} sign
\item All text following the \texttt{\#} is considered a comment
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{echo}}
\begin{itemize}
\item \texttt{echo} command prints out messages
\end{itemize}
\begin{lstlisting}
#!/bin/bash
mkdir ~/marks
cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt - \
| sort > ~/marks/results.txt
echo "Results generated."
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Command line arguments}
\begin{itemize}
\item Shell scripts can be given command line arguments
\item Following code allows to specify the results file
\end{itemize}
\begin{lstlisting}
#!/bin/bash
mkdir ~/marks
cut -d " " -f 2- marks1.txt \
| paste -d " " students.txt - \
| sort > ~/marks/$1
echo "Results generated."
\end{lstlisting} % $
\begin{itemize}
\item \texttt{\$1} corresponds to first command line argument
\item \texttt{\$n} corresponds to $n{th}$ command line argument
\item It can be run as shown below
\end{itemize}
\begin{lstlisting}
$ ./results.sh grades.txt
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{PATH}}
\begin{itemize}
\item The shell searches in a set of locations, for the command
\item Locations are saved in ``environment'' variable called PATH
\item \texttt{echo} can show the value of variables
\end{itemize}
\begin{lstlisting}
$ echo $PATH
\end{lstlisting} % $
\begin{itemize}
\item Put \texttt{results.sh} in one of these locations
\item It can then be run without \texttt{./}
\end{itemize}
\end{frame}
\section{Control structures and Operators}
\begin{frame}[fragile]
\frametitle{Control Structures}
\begin{itemize}
\item \texttt{if-else}
\item \texttt{for} loops
\item \texttt{while} loops
\end{itemize}
\begin{itemize}
\item \texttt{test} command to test for conditions
\item A whole range of tests that can be performed
\begin{itemize}
\item \texttt{STRING1 = STRING2} -- string equality
\item \texttt{INTEGER1 -eq INTEGER2} -- integer equality
\item \texttt{-e FILE} -- existence of FILE
\end{itemize}
\item \texttt{man} page of \texttt{test} gives list of various tests
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{if}}
\begin{itemize}
\item Print message if directory exists in \texttt{pwd}
\end{itemize}
\begin{lstlisting}
#!/bin/bash
if test -d $1
then
echo "Yes, the directory" \
$1 "is present"
fi
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{if}-\texttt{else}}
\begin{itemize}
\item Checks whether argument is negative or not
\end{itemize}
\begin{lstlisting}
#!/bin/bash
if test $1 -lt 0
then
echo "number is negative"
else
echo "number is non-negative"
fi
\end{lstlisting} % $
\begin{lstlisting}
$ ./sign.sh -11
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{[ ]} - alias for \texttt{test}}
\begin{itemize}
\item Square brackets (\texttt{[]}) can be used instead of
\texttt{test}
\item
\end{itemize}
\begin{lstlisting}
#!/bin/bash
if [ $1 -lt 0 ]
then
echo "number is negative"
else
echo "number is non-negative"
fi
\end{lstlisting} % $
\begin{itemize}
\item \alert{spacing is important, when using the square brackets}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{if}-\texttt{else}}
\begin{itemize}
\item An example script to greet the user, based on the time
\end{itemize}
\begin{lstlisting}
#!/bin/sh
# Script to greet the user
# according to time of day
hour=`date | cut -c12-13`
now=`date +"%A, %d of %B, %Y (%r)"`
if [ $hour -lt 12 ]
then
mess="Good Morning \
$LOGNAME, Have a nice day!"
fi
\end{lstlisting} %$
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{if}-\texttt{else} \ldots}
\begin{lstlisting}
if [ $hour -gt 12 -a $hour -le 16 ]
then
mess="Good Afternoon $LOGNAME"
fi
if [ $hour -gt 16 -a $hour -le 18 ]
then
mess="Good Evening $LOGNAME"
fi
echo -e "$mess\nIt is $now"
\end{lstlisting} % $
\begin{itemize}
\item \texttt{\$LOGNAME} has login name (env. variable)
\item backquotes store commands outputs into variables
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{for}}
\begin{block}{Problem}
Given a set of \texttt{.mp3} files, that have names beginning with
numbers followed by their names --- \texttt{08 - Society.mp3} ---
rename the files to have just the names. Also replace any spaces
in the name with hyphens.
\end{block}
\begin{itemize}
\item Loop over the list of files
\item Process the names, to get new names
\item Rename the files
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{for}}
\begin{itemize}
\item A simple example of the \texttt{for} loop
\end{itemize}
\begin{lstlisting}
for animal in rat cat dog man
do
echo $animal
done
\end{lstlisting} % $
\begin{itemize}
\item List of animals, each animal's name separated by a space
\item Loop over the list; \texttt{animal} is a dummy variable
\item Echo value of \texttt{animal} --- each name in list
\end{itemize}
\begin{lstlisting}
for i in {10..20}
do
echo $i
done
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{for}}
\begin{itemize}
\item Let's start with echoing the names of the files
\end{itemize}
\begin{lstlisting}
for i in `ls *.mp3`
do
echo "$i"
done
\end{lstlisting} % $
\begin{itemize}
\item Spaces in names cause trouble!
\item The following works better
\end{itemize}
\begin{lstlisting}
for i in *.mp3
do
echo "$i"
done
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{tr} \& \texttt{cut}}
\begin{itemize}
\item Replace all spaces with hyphens using \texttt{tr -s}
\item Use cut \& keep only the text after the first hyphen
\end{itemize}
\begin{lstlisting}
for i in *.mp3
do
echo $i|tr -s " " "-"|cut -d - -f 2-
done
\end{lstlisting} % $
Now \texttt{mv}, instead of just echoing
\begin{lstlisting}
for i in *.mp3
do
mv $i `echo $i|tr -s " " "-"\
|cut -d - -f 2-`
done
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{while}}
\begin{itemize}
\item Continuously execute a block of commands until condition
becomes false
\end{itemize}
\begin{itemize}
\item program that takes user input and prints it back, until the
input is \texttt{quit}
\end{itemize}
\begin{lstlisting}
while [ "$variable" != "quit" ]
do
read variable
echo "Input - $variable"
done
exit 0
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Environment Variables}
\begin{itemize}
\item Pass information from shell to programs running in it
\item Behavior of programs can change based on values of variables
\item Environment variables vs. Shell variables
\item Shell variables -- only current instance of the shell
\item Environment variables -- valid for the whole session
\item Convention -- environment variables are UPPER CASE
\end{itemize}
\begin{lstlisting}
$ echo $OSTYPE
linux-gnu
$ echo $HOME
/home/user
\end{lstlisting} % $
\end{frame}
\begin{frame}[fragile]
\frametitle{Environment Variables \ldots}
\begin{itemize}
\item The following commands show values of all the environment
variables
\end{itemize}
\begin{lstlisting}
$ printenv | less
$ env
\end{lstlisting} % $
\begin{itemize}
\item Use \texttt{export} to change Environment variables
\item The new value is available to all programs started from the shell
\end{itemize}
\begin{lstlisting}
$ export PATH=$PATH:$HOME/bin
\end{lstlisting} % $
\end{frame}
\section{Miscellaneous Tools}
\begin{frame}[fragile]
\frametitle{\texttt{find}}
\begin{itemize}
\item Find files in a directory hierarchy
\item Offers a very complex feature set
\item Look at the \texttt{man} page!
\end{itemize}
\begin{itemize}
\item Find all \texttt{.pdf} files, in current dir and sub-dirs
\begin{lstlisting}
$ find . -name ``*.pdf''
\end{lstlisting} % $
\item List all the directory and sub-directory names
\begin{lstlisting}
$ find . -type d
\end{lstlisting} % $
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{cmp}}
\begin{itemize}
\item Compare two files
\end{itemize}
\begin{lstlisting}
$ find . -name quick.c
./Desktop/programs/quick.c
./c-folder/quick.c
$ cmp Desktop/programs/quick.c \
c-folder/quick.c
\end{lstlisting} % $
\begin{itemize}
\item No output when the files are exactly the same
\item Else, gives location where the first difference occurs
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{diff}}
\begin{itemize}
\item We know the files are different, but want exact differences
\end{itemize}
\begin{lstlisting}
$ diff Desktop/programs/quick.c \
c-folder/quick.c
\end{lstlisting} % $
\begin{itemize}
\item line by line difference between files
\item \texttt{>} indicates content only in second file
\item \texttt{<} indicates content only in first file
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{\texttt{tar}}
\begin{itemize}
\item \emph{tarball} -- essentially a collection of files
\item May or may not be compressed
\item Eases the job of storing, backing-up \& transporting files
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Extracting an archive}
\begin{lstlisting}
$ mkdir extract
$ cp allfiles.tar extract/
$ cd extract
$ tar -xvf allfiles.tar
\end{lstlisting} %$
\begin{itemize}
\item \texttt{-x} --- Extract files within the archive
\item \texttt{-f} --- Specify the archive file
\item \texttt{-v} --- Be verbose
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Creating an archive}
\begin{lstlisting}
$ tar -cvf newarchive.tar *.txt
\end{lstlisting} % $
\begin{itemize}
\item \texttt{-c} --- Create archive
\item Last argument is list of files to be added to archive
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Compressed archives}
\begin{itemize}
\item \texttt{tar} can create and extract compressed archives
\item Supports compressions like gzip, bzip2, lzma, etc.
\item Additional option to handle compressed archives
\begin{center}
\begin{tabular}{ll}
Compression & Option \\
gzip & \texttt{-z} \\
bzip2 & \texttt{-j} \\
lzma & \texttt{-{}-lzma} \\
\end{tabular}
\end{center}
\end{itemize}
\begin{lstlisting}
$ tar -cvzf newarchive.tar.gz *.txt
\end{lstlisting} % $
\end{frame}
\begin{frame}
\frametitle{Customizing your shell}
\begin{itemize}
\item Bash reads \texttt{/etc/profile},
\texttt{\textasciitilde{}/.bash\_profile},
\texttt{\textasciitilde{}/.bash\_login}, and
\texttt{\textasciitilde{}/.profile} in that order, when starting
up as a login shell.
\item \texttt{\textasciitilde{}/.bashrc} is read, when not a login
shell
\item Put any commands that you want to run when bash starts, in this
file.
\end{itemize}
\end{frame}
%% THE DOCUMENT ENDS HERE
\end{document}
%%%%%%%%%%%%%%%%%%%%
\section{Basic editing and editors}
\begin{frame}[fragile]
\frametitle{vim}
Vim is a very powerful editor. It has a lot of commands, and all of them
cannot be explained here. We shall try and look at a few, so that you
can find your way around in vim.
To open a file in vim, we pass the filename as a parameter to the \texttt{vim}
command. If a file with that filename does not exist, a new file is
created.
\begin{lstlisting}
$ vim first.txt
\end{lstlisting} % $
To start inserting text into the new file that we have opened, we need
to press the \texttt{i} key. This will take us into the \emph{insert} mode from the
\emph{command} mode. Hitting the \texttt{esc} key, will bring us back to the
\emph{command} mode. There is also another mode of vim, called the \emph{visual}
mode which will be discussed later in the course.
In general, it is good to spend as little time as possible in the insert
mode and extensively use the command mode to achieve various tasks.
To save the file, use \texttt{:w} in the command mode. From here on, it is
understood that we are in the command mode, whenever we are issuing any
command to vim.
To save a file and continue editing, use \texttt{:w FILENAME} The file name is
optional. If you do not specify a filename, it is saved in the same file
that you opened. If a file name different from the one you opened is
specified, the text is saved with the new name, but you continue editing
the file that you opened. The next time you save it without specifying a
name, it gets saved with the name of the file that you initially opened.
To save file with a new name and continue editing the new file, use
\texttt{:saveas FILENAME}
To save and quit, use \texttt{:wq}
To quit, use \texttt{:q}
To quit without saving, use \texttt{:q!}
\begin{itemize}
\item Moving around\\
While you are typing in a file, it is in-convenient to keep moving your
fingers from the standard position for typing to the arrow keys. Vim,
therefore, provides alternate keys for moving in the document. Note
again that, you should be in the command mode, when issuing any commands
to vim.
The basic cursor movement can be achieved using the keys, \texttt{h} (left),
\texttt{l} (right), \texttt{k} (up) and \texttt{j} (down).
\begin{lstlisting}
^
k
\end{lstlisting} % $
\begin{quote}
\begin{description}
\item[< h l >] j v
\end{description}
\end{quote}
Note: Most commands can be prefixed with a number, to repeat the
command. For instance, \texttt{10j} will move the cursor down 10 lines.
\item Moving within a line\\
\begin{center}
\begin{tabular}{ll}
Cursor Movement & Command \\
\hline
Beginning of line & \texttt{0} \\
First non-space character of line & \texttt{\textasciicircum{}} \\
End of line & \texttt{\$} \\
Last non-space character of line & \texttt{g\_} \\
\end{tabular}
\end{center}
\item Moving by words and sentences\\
\begin{center}
\begin{tabular}{ll}
Cursor Movement & Command \\
\hline
Forward, word beginning & \texttt{w} \\
Backward, word beginning & \texttt{b} \\
Forward, word end & \texttt{e} \\
Backward, word end & \texttt{ge} \\
Forward, sentence beginning & \texttt{)} \\
Backward, sentence beginning & \texttt{(} \\
Forward, paragraph beginning & \texttt{\}} \\
Backward, paragraph beginning & \texttt{\{} \\
\end{tabular}
\end{center}
\item More movement commands\\
\begin{center}
\begin{tabular}{ll}
Cursor Movement & Command \\
\hline
Forward by a screenful of text & \texttt{C-f} \\
Backward by a screenful of text & \texttt{C-b} \\
Beginning of the screen & \texttt{H} \\
Middle of the screen & \texttt{M} \\
End of the screen & \texttt{L} \\
End of file & \texttt{G} \\
Line number \texttt{num} & \texttt{[num]G} \\
Beginning of file & \texttt{gg} \\
Next occurrence of the text under the cursor & \texttt{*} \\
Previous occurrence of the text under the cursor & \texttt{\#} \\
\end{tabular}
\end{center}
Note: \texttt{C-x} is \texttt{Ctrl} + \texttt{x}
\item The visual mode\\
The visual mode is a special mode that is not present in the original vi
editor. It allows us to highlight text and perform actions on it. All
the movement commands that have been discussed till now work in the
visual mode also. The editing commands that will be discussed in the
future work on the visual blocks selected, too.
\item Editing commands\\
The editing commands usually take the movements as arguments. A movement
is equivalent to a selection in the visual mode. The cursor is assumed
to have moved over the text in between the initial and the final points
of the movement. The motion or the visual block that's been highlighted
can be passed as arguments to the editing commands.
\begin{center}
\begin{tabular}{ll}
Editing effect & Command \\
\hline
Cutting text & \texttt{d} \\
Copying/Yanking text & \texttt{y} \\
Pasting copied/cut text & \texttt{p} \\
\end{tabular}
\end{center}
The cut and copy commands take the motions or visual blocks as arguments
and act on them. For instance, if you wish to delete the text from the
current text position to the beginning of the next word, type \texttt{dw}. If
you wish to copy the text from the current position to the end of this
sentence, type \texttt{y)}.
Apart from the above commands, that take any motion or visual block as
an argument, there are additional special commands.
\begin{center}
\begin{tabular}{ll}
Editing effect & Command \\
\hline
Cut the character under the cursor & \texttt{x} \\
Replace the character under the cursor with \texttt{a} & \texttt{ra} \\
Cut an entire line & \texttt{dd} \\
Copy/yank an entire line & \texttt{yy} \\
\end{tabular}
\end{center}
Note: You can prefix numbers to any of the commands, to repeat them.
\item Undo and Redo\\
You can undo almost anything using \texttt{u}.
To undo the undo command type \texttt{C-r}
\item Searching and Replacing\\
\begin{center}
\begin{tabular}{ll}
Finding & Command \\
\hline
Next occurrence of \texttt{text}, forward & \texttt{\textbackslash{}text} \\
Next occurrence of \texttt{text}, backward & \texttt{?text} \\
Search again in the same direction & \texttt{n} \\
Search again in the opposite direction & \texttt{N} \\
Next occurrence of \texttt{x} in the line & \texttt{fx} \\
Previous occurrence of \texttt{x} in the line & \texttt{Fx} \\
\end{tabular}
\end{center}
\begin{center}
\begin{tabular}{ll}
Finding and Replacing & Command \\
\hline
Replace the first instance of \texttt{old} with \texttt{new} in the current line. & \texttt{:s/old/new} \\
Replace all instances of \texttt{old} with \texttt{new} in the current line. & \texttt{:s/old/new/g} \\
Replace all instances of \texttt{old} with \texttt{new} in the current line, but ask for confirmation each time. & \texttt{:s/old/new/gc} \\
Replace the first instance of \texttt{old} with \texttt{new} in the entire file. & \texttt{:\%s/old/new} \\
Replace all instances of \texttt{old} with \texttt{new} in the entire file. & \texttt{:\%s/old/new/g} \\
Replace all instances of \texttt{old} with \texttt{new} in the entire file but ask for confirmation each time. & \texttt{:\%s/old/new/gc} \\
\end{tabular}
\end{center}
\end{itemize} % ends low level
\end{frame}
\begin{frame}
\frametitle{SciTE}
SciTE is a \emph{source code} editor, that has a feel similar to the commonly
used GUI text editors. It has a wide range of features that are
extremely useful for a programmer, editing code. Also it aims to keep
configuration simple, and the user needs to edit a text file to
configure SciTE to his/her liking.
Opening, Saving, Editing files with SciTE is extremely simple and
trivial. Knowledge of using a text editor will suffice.
SciTE can syntax highlight code in various languages. It also has
auto-indentation, code-folding and other such features which are useful
when editing code.
SciTE also gives you the option to (compile and) run your code, from
within the editor.
\end{frame}
|