-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbash-workshop.tex
More file actions
3477 lines (3250 loc) · 149 KB
/
bash-workshop.tex
File metadata and controls
3477 lines (3250 loc) · 149 KB
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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% LICENSE %
% This work is licensed under the Creative %
% Commons Attribution-NonCommercial 4.0 International License. %
% To view a copy of this license, visit: %
% https://creativecommons.org/licenses/by-nc/4.0/ %
% or send a letter to %
% Creative Commons %
% PO Box 1866, Mountain View, %
% CA 94042 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\documentclass[a4paper]{scrreprt}
\usepackage[english]{babel}
\usepackage{tabu}
\usepackage{longtable}
\usepackage{hyperref}
\hypersetup{
colorlinks=true
}
\usepackage{graphicx}
\usepackage{tikz}
\begin{document}
\begin{titlepage}
\centering
{\scshape\huge UNIX Workshop \par}
\vspace{2cm}
{\LARGE Dimitri Robl\par}
\vspace{1cm}
\includegraphics[scale=0.6]{pictures/xkcd456.png}
\newline
{CC BY-NC 2.5 by Rendall Monroe\\
\href{https://creativecommons.org/licenses/by-nc/2.5/legalcode}
{https://creativecommons.org/licenses/by-nc/2.5/legalcode}
\par}
\vspace{2cm}
{\large This work is licensed under the
\href{https://creativecommons.org/licenses/by-nc/4.0/}
{Creative Commons Attribution-Non-Commercial 4.0 International
License}\\}
\vspace{3cm}
{\large Version 0.1\hfill\today\par}
\end{titlepage}
Welcome to this three-day workshop which will teach you the basics of
UNIX, (GNU/)Linux, and the command line! There is a lot to learn
ahead of us, but first, you need to set up your working environment.
\tableofcontents
\chapter{Installing a Virtual Machine}
In order to make life easier for you, it won't be necessary that you
install a Linux distribution or distro\footnote{You'll learn what
exactly that means later in the course.} on your harddrive. Instead,
we will guide you through the installation of a virtual
machine\footnote{If you are unfamiliar with this concept you can read
the \href{https://en.wikipedia.org/wiki/Virtual_Machine}{Wikipedia
entry} about it.} running Debian GNU/Linux. If you already have a
working installation of any Linux distro (either physical or virtual)
you can skip this step.
\section{Prerequisites}
In order to use a virtual machine (VM), your physical machine should
have at least two cores, 4GiB of main memory (i.e. RAM) and there
should be at least 20GB free space on your disk. In this case more is
always good.
% end Prerequisites
\subsection{Enable Virtualization}
The first thing to check is whether you CPU supports virtualization,
and has it activated. Follow these steps:
\begin{enumerate}
\item Boot your computer.
\item Depending on your motherboard press \texttt{ESC},
\texttt{DEL}, \texttt{ENTER}, \texttt{F2}, \texttt{F10}, or
\texttt{F12}.
\item Look for an option which says "Advanced" or "CPU settings".
\item Depending on whether you use an Intel or an AMD CPU you
will have to enable Intel Virtualization Technology, Intel VT,
VT-x, AMD-V, AMD Virtualization or similar.
\item Check if this option is enabled and if not, enable it.
\item Save changes and Exit.
\end{enumerate}
% end Enable Virtualization
\subsection{Install Oracle VirtualBox}
Now you have to install the software to run virtual machines:
\begin{enumerate}
\item Download the installer from \href{https://www.virtualbox.org/}
{https://www.virtualbox.org/}. It runs on Windows, OS X, Linux
and Solaris hosts.
\item Start the installer and follow the instructions.
\end{enumerate}
% end Install Oracle VirtualBox
\subsection{Getting the Installation Image}
The final thing to obtain before the installation can begin is an
image to install. Just download the
\href{https://cdimage.debian.org/debian-cd/current/amd64/iso-cd/debian-9.5.0-amd64-netinst.iso}
{netinst Debian image} and store it somewhere you remember(!),
you'll need it afterwards.
In case you don't have an x86\_64 architecture \footnote{To
make things less confusing, this architecture is also called
x64, AMD64, and Intel 64.}, installation files for other architetures
are available at \href{https://www.debian.org/distrib/netinst}
{https://www.debian.org/distrib/netinst}. However, it is rather
unlikely that you have a different architecture on a general purpose
computer, unless you are still on a machine running 32-bit
x86\footnote{A.k.a. IA-32, i386.} or own one manufactured by Apple
before 2006 which has a PowerPC CPU.
Now everything is ready to proceed to the installation!
%end Getting the Installation Image
\section{Installing a Virtual Machine Running Debian GNU/Linux}
To install a VM running Debian GNU/Linux follow the instructions
below:
\begin{enumerate}
\item Open VirtualBox.
\item Click on the ``New" button in the upper left corner.
\begin{center}
\includegraphics[scale=0.35]{pictures/install_vm01.png}
\end{center}
\item Enter a name for your VM, and if this is not automatically
done select \emph{Type:} → \emph{Linux}, and \emph{Version:} →
\emph{Debian (64-bit)}.\footnote{If you use a different
rchitecture than x86\_64 you have to specify this here as well.}
Press ``Next".
\begin{center}
\includegraphics[scale=0.40]{pictures/install_vm02.png}
\end{center}
\item Select the amount of memory the VM will have. The best
value here depends on what you want to do with the VM and how
much RAM your physical machine has. The default value of 1024Mib
(i.e. 1GiB) is sufficient for our purposes, but you can use more
if you want and less if you are very low on physical RAM (i.e.
2GiB or less).
\begin{center}
\includegraphics[scale=0.40]{pictures/install_vm03.png}
\end{center}
\item Choose how much disk space to give to your VM. This includes
several steps:
\begin{enumerate}
\item Select ``Create a virtual hard disk now" and press ``Create".
\begin{center}
\includegraphics[scale=0.40]{pictures/install_vm04.png}
\end{center}
\item Leave ``VDI (VirtualBox Disk image)" selected and press
``Next".
\begin{center}
\includegraphics[scale=0.40]{pictures/install_vm05.png}
\end{center}
\item Again go with the default ``Dynamically allocated" and
and press ``Next".
\begin{center}
\includegraphics[scale=0.40]{pictures/install_vm06.png}
\end{center}
\item You can now change name and location of the file that
will be the VM's harddisk. If this sounds confusing: What the
VM will believe is its disk will actually be nothing more
than a file on the underlying operating system (OS) you run
the VM on.
A reasonable size also depends on your needs and the space
you have. If you plan to really do anything with your VM, we
recommend a minimum of 20GB, but if you just need it for this
workshop the standard size of 8GB will suffice.
After you are done, click on ``Create".
\begin{center}
\includegraphics[scale=0.40]{pictures/install_vm07.png}
\end{center}
\end{enumerate}
\item Now you're back to the main screen, which should look
something like this:
\begin{center}
\includegraphics[scale=0.40]{pictures/install_vm08.png}
\end{center}
Select you newly created VM and press ``Start".
\item A pop-up should appear which asks you to select a start-up
disk. Click on the folder icon and select the Debian GNU/Linux
ISO-file you downloaded earlier (and whose download location you
should remember ;)).
\begin{center}
\includegraphics[scale=0.50]{pictures/install_vm09.png}
\end{center}
Press the ``Start" button.
\end{enumerate}
That's it, a new window should pop up and the installation of your
new virtual OS can begin. Select ``Graphical Install" and follow the
on-screen instructions. There are some things you should be careful
about:
\begin{enumerate}
\item The selection of your locale (i.e. the language the system
will use) and your keyboard layout -- e.g. if you want to use an
English system, but a German keyboard you have to tell the
installer.
\item When asked to enter the \texttt{root} password, leave it
blank to disable the \texttt{root} account and use \texttt{sudo}
instead. We'll discuss the details later.
\begin{center}
\includegraphics[scale=0.35]{pictures/install_vm10.png}
\end{center}
\item If you don't know about disk partitioning leave the defaults
intact. However, in case you install Debian GNU/Linux on real
hardware on a portable device (e.g. a laptop) and not as a VM,
we \emph{strongly} recommend using the ``Guided - use entire
disk and set up encrypted LVM" option. This will encrypt your
harddrive which means that every time your PC or laptop starts
you will have to enter a passphrase to decrypt the disk before
the OS even boots. This has the huge advantage that in case your
device is stolen, it will be almost impossible to access your
data if the thief does not your password/passphrase -- and no
one should ever know that! Make sure to choose a strong password,
where ``strong" means ``as long as possible". You don't
necessarily have to care about special characters, length is
always better. See \href{https://www.xkcd.com/936/}{here}.
The only downside to this is the fact that if you forget or lose
your password/passphrase you yourself also won't be able to access
your data. This is one of the few cases in which it \emph{may} be
a good idea to write down your password/passphrase on a piece of
paper and store it in some secure place.
Also, don't worry about the warnings when it says ``Write changes
to disk" if you set up a VM. The VM `believes' it has a whole
disk to write on and is not aware that it is actually just a file
in the underlying OS. If you followed the instructions so far, it
is not possible to destroy your real hard disk by installing the
VM.
\item When you come to select mirror to download the packages,
double-click on ``Austria" and then select any mirror. As it says
in the description \texttt{ftp.at.debian.org} is usually a good
choice.
\begin{center}
\includegraphics[scale=0.40]{pictures/install_vm11.png}
\includegraphics[scale=0.40]{pictures/install_vm12.png}
\end{center}
\item Once you come to the screen ``Software selection" we recommend
selecting ``Xfce" or ``LXDE" if the VM is on low memory ( <
2GiB), as these are very light weight GUIs. Otherwise you can
stick with the default ``Debian desktop environment".
\begin{center}
\includegraphics[scale=0.35]{pictures/install_vm13.png}
\end{center}
\end{enumerate}
If you run into any problems, use your favorite search engine and
look for something like ``Debian installation tutorial". Most likely
you'll find written instructions as well as videos on how to do it.
% end Installing a Virtual Machine Running Debian GNU/Linux
\chapter{UNIX, GNU, and Linux}
This section seeks to clarify terms you are very likely to encounter
pretty soon when working with any (GNU/)Linux distro. Be prepared that
people are \emph{very} picky about words, and don't get discouraged
in case you meet a particularly nasty partisan of one of these
wordisms.
\section{UNIX}
In the beginning there was … the desire to play games! Yes, that's
it! In the 1960ies two MIT students called Dennis Ritchie and Ken
Thompson wanted to play the game "Space Travel" on their new, but
underpowered minicomputer\footnote{"Mini" here indicates that it
didn't fill a whole room.} PDP-7. To achieve their goal, they had to
tweak the machine a bit and as they were researching operating
systems (OS) at the time anyway, they ended up writing a whole new OS for
the PDP-7 which came to be named UNIX.
UNIX, based on the even older
\href{https://en.wikipedia.org/wiki/Multics}{Multics}, introduced
a lot of new concepts into OS-design, many of which are still in use
today. UNIX was developed by Bell Labs which where prohibited from
selling it under US-law, but in 1983 this decree was lifted and
AT\&{}T, the owner of Bell Labs promply tried to turn UNIX into a
product, as it was already widely used among computer science students
and companies they worked for. However, by this time there were
already multiple versions of UNIX around, and one of the most
important of them was developed by researchers in Berkeley; the
Berkeley System Distribution (BSD).
BSD was sued by AT\&{}T for copyright infringement despite the fact
that large parts of AT\&{}T's own UNIX implementation was based on
code from Berkeley. After a long legal battle, BSD won its freedom and
the base of modern FreeBSD, OpenBSD, NetBSD, etc. was laid out.
UNIX finally lost the battle for the PC against Apple and Microsoft,
which was very likely at least in parts based on the law suits.
However, in the server market and in technical and scientific circles,
UNIX and its derivatives continued to be state of the art and remain
so today.
% end UNIX
\section{GNU}
GNU (GNU's Not Unix) was founded in 1983 by Richard M. Stallman after
he was fed up with the closed source philosophy of UNIX. He wanted to
create an OS that is ``free as in freedom", meaning it adheres to the
four principles of free software, which state that users' have to be
\begin{description}
\item [0.] able to run the program as they wish, for any purpose.
\item [1.] able to study how the program works and can change it
according to their wishes.
\item [2.] able to redistribute copies to others, either free or
for a fee.
\item [3.] able to distribute modified versions to others, again
either for free or for a fee.
\end{description}
Many tools necessary for a complete OS were created by the GNU
project, including \texttt{gcc} (GNU C Compiler), \texttt{g++} (GNU
C++ Compiler), \texttt{gdb} (GNU Debugger), a shell, and others.
However, one essential part was incomplete: A working kernel. The
kernel is the part of the OS which interacts directly with the
hardware, so without a kernel, an OS can't run. Thus, the GNU tools
were used on a number of UNIX systems, but a stand-alone OS was still
wanting.
This changed when in 1991 a Finnish computer science student by the
name of Linus Torvalds released a kernel he (eventually) called
Linux.
% end GNU
\section{Linux}
Linus Torvalds originally released the Linux kernel under a license
he created himself, but in 1992 he re-licensed it under the GPLv2
(General Public License, version 2) which is a license of the GNU
project.
Technology-enthusiasts and academia quickly started using Linux as
their kernel and as soon as it was GPL-ed there existed a full GNU
OS with a working kernel. This is why you may read/hear the term
GNU/Linux. Be careful here, some people are \emph{very} picky about
using Linux only when referring to the kernel and GNU/Linux when
refering to a distro.
Linux' open source nature made it possible that its development
proceeded very quickly with thousands of people contributing code and
time. The current major release of Linux is version 4, the original
4.0 kernel was released in 2015. While Linux is often hailed as
\emph{the} example of a successful open source project with loads
of people sacrificing their spare time to make the Linux kernel
better, today this is obsolete in large parts. For the kernel
releases between 4.8--4.13, 87.7\%{} to 91.8\%{} of the changes have
been done by people who are paid for this by various
companies.\footnote{See
\href{https://www.linuxfoundation.org/2017-linux-kernel-report-landing-page/}
{here}.} This is not necessarily a bad thing, it just shows that the
IT-industry heavily relies on Linux and therefore actively develops it
and also that Linux today is very far from being a hobbyists' project
but a mature OS kernel developed mostly by professionals.
% end Linux
% end UNIX, GNU, and Linux
\chapter{Basics}
\label{basics}
This section will introduce important concepts and words you'll have
to work with, show you how to help yourself on a (GNU/)Linux system
in case you get stuck and explain the filesystem.
To follow along, please download the training directory at\\
\href{https://ct.cs.univie.ac.at/teaching/unixlinux-einfuehrungskurs/unixcourse.tgz}
{https://ct.cs.univie.ac.at/teaching/unixlinux-einfuehrungskurs/unixcourse.tgz}
On the command line you can type (or just copy-paste -- which can be
done by just selecting text with your mouse to copy it and paste it
by pressing the middle mouse button on most modern GUIs on
(GNU/)Linux) the following to get the archive containing the folder:
{\footnotesize
\begin{verbatim}
wget https://ct.cs.univie.ac.at/teaching/unixlinux-einfuehrungskurs/unixcourse.tgz
tar -xvzf unixcourse.tgz
\end{verbatim}
}
\section{Shells, Terminals, and Commands}
\label{shellsterminalscommands}
In the last chapter you've seen the concept of the kernel, the part
of the OS which talks to the hardware. If you want to communicate
with the kernel you need something, that wraps around it -- a
\emph{shell}, for example.
Essentially, a shell is a user interface which allows you to access
the services provided by the OS. You are most likely familiar with a
graphical user interface (GUI) which allows you to access some
services of the OS using a mouse and icons. A shell, on the other
hand, requires text as input by providing you with a \emph{command
line}. While the GUI is often part of the OS you buy, i.e. you can't
easily change the program that is your GUI if you use Microsoft
Windows or Apple's OS X, a shell on a (GNU/)Linux system is just a
program and you can choose from a wide variety of options. The shell
we are going to work with here is the
\href{https://www.gnu.org/software/bash/https://www.gnu.org/software/bash/}
{GNU \texttt{bash}} (Bourne Again SHell), which is based on an older
shell written by Stephen Bourne in 1979 and is obviously also a
wordplay with the verb `born'. We chose this one because it is
currently the standard shell on all major (GNU/)Linux distros, but do
play around with others if you like to.
The stuff you enter on the command line to your shell are
\emph{commands} and they work the same way as clicking an icon with
your mouse does, e.g. you can either double-click on the Firefox
icon, or just type the word ``firefox" on the command
line, followed by \texttt{ENTER}. Both actions will start a new
instance of the Firefox webbrowser (if it is installed). One thing
to keep in mind in the following sections is that most commands are
actually stand-alone programs, even though they write their output
to the terminal screen, while only a few of them are so-called
\emph{shell builtins}, i.e. keywords the shell itself understands
and works on without starting a new program. This will be of special
importance in \ref{helputilities} where we talk about utilities that
provide documentation for commands.
Many commands can take arguments and options. Their basic syntax is
usually:
\begin{verbatim}
command [OPTION]... [ARGUMENT]...
\end{verbatim}
The brackets around \texttt{OPTION} and \texttt{ARGUMENT} indicate
that they are optional and the dots tell you that the preceding
entity can be used multiple times, i.e. the line reads approximately
as ``Use \texttt{command} by typing \texttt{command}, possibly
followed by one or more option(s), possibly followed by one or more
argument(s)." Arguments tell the command e.g. on which file to operate,
whereas options tell it what to do with the arguments. To separate
these things, you have to put a space after the command, after each
argument, and after each (group) of options.
Options come in two flavors: Short and long ones. Short options are
usually indicated by a single, non-separated, preceding dash
`\texttt{-}', while long options can be recognized by two
non-separated preceding dashes `\texttt{--}'. A very simple example
would be
\begin{verbatim}
$ firefox --new-window https://www.univie.ac.at
\end{verbatim}
which, little surprisingly opens the homepage of the University of
Vienna in a new window of the Firefox webbrowser.
In most cases options for a single dash consist of a
single letter, while options preceeded by two dashes are words or
phrases, e.g. let's say you use the \texttt{ls} program (to
\emph{list} directories and/or files) which has the options
\texttt{-l}, \texttt{-a}, and \texttt{--human-readable} and want it
to process argument \texttt{arg}. To do so, you could type:
\begin{verbatim}
$ ls -a -l --human-readable unixcourse
total 524K
drwxr-xr-x 9 dimir dimir 4.0K Sep 24 22:43 .
drwxr-xr-x 4 dimir dimir 4.0K Sep 24 22:45 ..
drwxr-xr-x 13 dimir dimir 4.0K Sep 19 10:05 contentdir
drwxr-xr-x 2 dimir dimir 4.0K Sep 18 15:36 dir1
drwxr-xr-x 2 dimir dimir 4.0K Sep 17 13:52 dir2
drwxr-xr-x 2 dimir dimir 4.0K Sep 17 13:52 dir3
drwxr-xr-x 2 dimir dimir 4.0K Sep 17 15:36 'dir with spaces'
-rw-r--r-- 1 dimir dimir 63 Sep 19 11:07 errorfile
-rw-r--r-- 1 dimir dimir 29 Sep 18 15:24 file1
-rw-r--r-- 1 dimir dimir 15 Sep 23 12:32 file10
-rw-r--r-- 1 dimir dimir 11 Sep 23 12:22 file3
-rw-r--r-- 1 dimir dimir 0 Sep 24 22:43 filetodelete
-rw-r--r-- 1 dimir dimir 472 Sep 23 09:49 grepfile
-rw-r--r-- 1 dimir dimir 0 Sep 23 12:33 hasfileinit
-rw-r--r-- 1 dimir dimir 422K Sep 18 17:52 hugefile
-rw-r--r-- 1 dimir dimir 46 Sep 23 12:11 join1
-rw-r--r-- 1 dimir dimir 48 Sep 23 12:11 join2
-rw-r--r-- 1 dimir dimir 405 Sep 23 12:22 linefile
-rw-r--r-- 1 dimir dimir 1.4K Sep 23 12:29 lsfile
-rw-r--r-- 1 dimir dimir 26 Sep 23 12:22 numericsort
-rw-r--r-- 1 dimir dimir 25 Sep 17 13:53 onelinefile
-rw-r--r-- 1 dimir dimir 69 Sep 18 15:36 renamedfile
drwxr-xr-x 2 dimir dimir 4.0K Sep 18 11:14 rmdirectory
drwxr-xr-x 2 dimir dimir 4.0K Sep 18 15:18 rminteractive
-rw-r--r-- 1 dimir dimir 331 Sep 23 11:02 sedfile
-rw-r--r-- 1 dimir dimir 331 Sep 23 10:48 sedfile.bak
-rw-r--r-- 1 dimir dimir 18 Sep 23 12:22 sortfile
-rw-r--r-- 1 dimir dimir 747 Sep 23 11:25 tablefile
\end{verbatim}
One thing to remember is that it is possible to combine short options
behind a single dash, i.e. the following would be equivalent to the
above:
\begin{verbatim}
$ ls -al --human-readable unixcourse
total 524K
drwxr-xr-x 9 dimir dimir 4.0K Sep 24 22:43 .
drwxr-xr-x 4 dimir dimir 4.0K Sep 24 22:45 ..
drwxr-xr-x 13 dimir dimir 4.0K Sep 19 10:05 contentdir
drwxr-xr-x 2 dimir dimir 4.0K Sep 18 15:36 dir1
drwxr-xr-x 2 dimir dimir 4.0K Sep 17 13:52 dir2
drwxr-xr-x 2 dimir dimir 4.0K Sep 17 13:52 dir3
drwxr-xr-x 2 dimir dimir 4.0K Sep 17 15:36 'dir with spaces'
-rw-r--r-- 1 dimir dimir 63 Sep 19 11:07 errorfile
-rw-r--r-- 1 dimir dimir 29 Sep 18 15:24 file1
-rw-r--r-- 1 dimir dimir 15 Sep 23 12:32 file10
-rw-r--r-- 1 dimir dimir 11 Sep 23 12:22 file3
-rw-r--r-- 1 dimir dimir 0 Sep 24 22:43 filetodelete
-rw-r--r-- 1 dimir dimir 472 Sep 23 09:49 grepfile
-rw-r--r-- 1 dimir dimir 0 Sep 23 12:33 hasfileinit
-rw-r--r-- 1 dimir dimir 422K Sep 18 17:52 hugefile
-rw-r--r-- 1 dimir dimir 46 Sep 23 12:11 join1
-rw-r--r-- 1 dimir dimir 48 Sep 23 12:11 join2
-rw-r--r-- 1 dimir dimir 405 Sep 23 12:22 linefile
-rw-r--r-- 1 dimir dimir 1.4K Sep 23 12:29 lsfile
-rw-r--r-- 1 dimir dimir 26 Sep 23 12:22 numericsort
-rw-r--r-- 1 dimir dimir 25 Sep 17 13:53 onelinefile
-rw-r--r-- 1 dimir dimir 69 Sep 18 15:36 renamedfile
drwxr-xr-x 2 dimir dimir 4.0K Sep 18 11:14 rmdirectory
drwxr-xr-x 2 dimir dimir 4.0K Sep 18 15:18 rminteractive
-rw-r--r-- 1 dimir dimir 331 Sep 23 11:02 sedfile
-rw-r--r-- 1 dimir dimir 331 Sep 23 10:48 sedfile.bak
-rw-r--r-- 1 dimir dimir 18 Sep 23 12:22 sortfile
-rw-r--r-- 1 dimir dimir 747 Sep 23 11:25 tablefile
\end{verbatim}
Long options cannot be combined, so you have to type each of them in
full. Many programs have short and long options for the same
operations and it's just a question of taste which one you use. A good
approach is to start using long options because it is easier to
remember what they do and start memorizing the short options for
operations you use a lot.
But why bother to learn the names of programs if you can just click
on an icon? Surely, that's much more intuitive and simple! Well, it
depends. Imagine you commonly use about 50 programs, many of which
interact with each other -- a screen with 50 icons for programs
seems rather overwhelming, don't you think? And how about moving
stuff from one window to the other all the time -- annoying, I'd
say. Or opening the same 7 programs one after the other, hundreds of
times and copying data from one to the other? Convenience is clearly
not the word for this.
These are some of the tasks that can be solved easily on the command
line. Most command line utilities follow the UNIX philosophy that
each program should solve a single problem and solve it well. By
combining the capabilities of many programs you get a very flexible
toolset to solve your problem at hand. Make the easy things easy,
and the hard things possible!
\begin{figure}[h]
\centering
\includegraphics[scale=0.9]{pictures/terminal-dec-vt100.jpg}
\label{vt100}
\caption{A DEC VT100 terminal. CC BY-SA \href{https://en.wikipedia.org/wiki/User:ClickRick?rdfrom=commons:User:ClickRick}{ClickRick}}
\end{figure}
Finally, a \emph{terminal} is actually a piece of hardware (see
\ref{vt100}), which can take input and display output. In this course
and most likely in your whole life, you'll be confronted with
terminal emulators, i.e. software which behaves like a terminal, like
the on you see in \ref{xfce4-terminal}.
\begin{figure}[h]
\centering
\includegraphics[scale=0.4]{pictures/xfce4-terminal.png}
\caption{XFCE's terminal emulator}
\label{xfce4-terminal}
\end{figure}
When you open a terminal emulator your shell will be started and
will provide you with a \emph{prompt}, which usually looks a bit like
\begin{verbatim}
user@machine ~$
\end{verbatim}
where \texttt{user} is your username, \texttt{machine} is the name of
your computer and \texttt{\textasciitilde} is a shorthand for your
home directory (see below for an explanation).\footnote{From now on,
we will abbreviate your prompt as just `\texttt{\$}'.} All your shell
does now, is to wait for you to issue a command -- let's do it the
favor: type some random stuff and press \texttt{ENTER}. It should
look something like this:
\begin{verbatim}
$ ajsdggasd
bash: ajsdggasd: command not found
$
\end{verbatim}
Little surprisingly, this gibberish does not constitute a command.
But fear not, we shall use existing commands starting in the next
section!
% end Shells, Terminals, and Commands}
\section{The Filesystem}
\label{thefilesystem}
In order to use the filesystem efficiently it is essential to
understand its basic structure and this is what this section wants
to teach you.
The filesystem is software which manages how your files and
directories are stored on you disk/pendrive/CD/etc. Usually there are
at least three kinds of entities in a filesystem:
\begin{description}
\item [Files] Some kind of data, possibly in a special format (e.g.
PDF).
\item [Directories] These are places in which files are stored.
\item [Links] Links are references to either a file or a directory,
i.e. they don't contain data, but just point to another place
in the filesystem.
\end{description}
Filesystems in any UNIX system are usually structured as rooted
trees.\footnote{For the mathematically interested: A rooted tree is an
acyclic undirected graph in which one vertex has been designated the
root.} The root of the filesystem is designated by the slash
` \texttt{/}'.
{\footnotesize
\tikz [level 1/.style={sibling distance=1cm}]
\node {\texttt{/}}
child { node {\texttt{bin}} }
child { node {\texttt{boot}} }
child { node {\texttt{dev}} }
child { node {\texttt{etc}} }
child { node {\texttt{lib}} }
child { node {\texttt{media}} }
child { node {\texttt{mnt}} }
child { node {\texttt{opt}} }
child { node {\texttt{run}} }
child { node {\texttt{usr}}
child { node {\texttt{bin}} }
child { node {\texttt{include}} }
child { node {\texttt{lib}} }
child { node {\texttt{local}} }
child { node {\texttt{sbin}} }
child { node {\texttt{share}} }
child { node {\texttt{src}} }
}
child { node {\texttt{sbin}} }
child { node {\texttt{srv}} }
child { node {\texttt{tmp}} }
child { node {\texttt{var}} };
}
%TODO picture of filesystem!
As you are logged in and opened a terminal already (if not, do so
now), you can type
\begin{verbatim}
$ pwd
/home/me
\end{verbatim}
Of course, you'll get a slightly different output, but there are two
important things here:
\begin{enumerate}
\item \texttt{pwd} is our first real command! It stands for
`print working directory' which prints you current working
directory, i.e. tells you where in the filesystem you are.
\item The output, \texttt{/home/me} tells you, that you are in a
directory \texttt{me} which is in a directory called
\texttt{home}, which is in the root directory \texttt{/}.
\end{enumerate}
Okay, now that you know where you are, you may want to know which
files\footnote{Here `files' denotes data, directories, and links as
it is the most generic concept.} are in this directory -- you can
list them using
\begin{verbatim}
$ ls
Documents Downloads Pictures Private
\end{verbatim}
Again your output will differ slightly. So now, you used the program
\texttt{ls} to show you the contents of the directory you are in.
This is \texttt{ls}' standard behavior: If you don't provide any
arguments it will display the content of you current working
directory.
However, you can tell \texttt{ls} which directory it should list by
providing a \emph{path} as an argument. To show the contents of the
root directory, type:
\begin{verbatim}
$ ls /
bin boot dev etc lib media mnt opt run sbin srv tmp usr var
\end{verbatim}
The output provided here shows everything that is required in the
root directory according to the
\href{http://refspecs.linuxfoundation.org/fhs.shtml}{Filesystem
Hierarchy Standard (FHS)}, a document which defines what has to be in
the root directory of any (GNU/)Linux distro.\footnote{A short version
of this can be found by typing \texttt{man 7 hier}.} But as it only
defines the minimum, distros are free to add other things and you'll
most likely also find at least \texttt{home}, \texttt{proc}, and
\texttt{sys} in your root directory.
Now there is one more concept you have to understand regarding the
filesystem: absolute and relative paths. In a rooted tree you can
specify any point in the tree by starting at the root and listing
every step on the way. This is what \texttt{pwd} does: Above it said
that I was in \texttt{/home/me}, telling me my exact position in the
filesystem in an absolute manner; therefore paths starting with the
root directory are called \emph{absolute paths}.
Unfortunately, absolute paths can be very inconvenient and long.
Remember that we said \texttt{ls} can be provided with a path to list
the contents of any directory in the filesystem, not just the one you
are currently in and that in the above example the current directory
contained \texttt{Documents}, \texttt{Downloads}, \texttt{Pictures},
and \texttt{Private}. Imagine you have a subdirectory in
\texttt{Documents} which is called \texttt{university}. To list it,
you could type
\begin{verbatim}
$ ls /home/me/Documents/university
\end{verbatim}
An awful lot to type I'd say! To remedy this situation you can
provide a path relative to your current working directory, i.e. a
\emph{relative path}. \texttt{pwd} told us that our current working
directory is \texttt{/home/me}, so the relative path to
\texttt{university} is just \texttt{Documents/university} without a
leading `\texttt{/}'. Every path starting with a slash will be
interpreted as an absolute path, while relative paths just start with
the name of the next directory you hop into from your current
directory.
That's it for filesystem theory, now we'll enable you to help
yourself on a (GNU/)Linux system.
% end The Filesystem
\section{Help Utilities}
\label{helputilities}
Every (GNU/)Linux system comes with a lot of built-in documentation
installed by default and of course the Internet hosts millions of
HOW-TOs, tutorials, etc. We're going to focus on the offline
documentation, but also provide some hints were you usually get
reliable information online.
This section can be treated as a reference -- reading it now in
its entirety may cause you a bit of a headache as you may not be
familiar enough with commands, options and arguments right now. For
the beginning it will suffice if you skim through this section to get
a basic idea about which facilities exist.
\subsection{\texttt{man}}
\texttt{man}, which is a shorthand for ``manual page" is the
traditional UNIX documentation and knowing how to use it will help
you not only on (GNU/)Linux, but also on any other UNIX you'll
encounter like the various BSDs. In case you ask someone who is in a
hurry about a program and the only response is ``RTFM", meaning
``Read The Fucking\footnote{Or, ``Fine", for the very pure.} Manual",
this program is usually what they want you to use.
If you want to access the manual page of the \texttt{ls} program,
all you have to do is to type:
\begin{verbatim}
$ man ls
\end{verbatim}
After this the man page for \texttt{ls} should appear. To scroll down
press \texttt{SPACE} and to exit press \texttt{Q}.
We won't go into a lot of details here because your first task now is
to type
\begin{verbatim}
$ man man
\end{verbatim}
This will fire up the man page of \texttt{man}. Read it to learn how
to use this program efficiently and make sure to read the ``EXAMPLES"
section!
The main things you should remember from this is how to move within
the man pages, how to exit them, what sections are and how you can
tell the program to only return entries from certain sections. Useful
options you may want to remember are
\begin{description}
\item [\texttt{-k | --apropos}] List the programs whose name or
short description match the keyword you searched for. Note that
this is the same thing the \texttt{apropos} program
does.\footnote{You guessed it: Use \texttt{man apropos} to find
out more.}
\item [\texttt{-a | --all}] Show all man pages from all sections
which match the keyword you searched for.
\end{description}
If you read through \texttt{man}'s man page you'll understand what
these options do ;)
% end man
\subsection{\texttt{info}}
While \texttt{man} is available on any UNIX system, the info pages
were invented by the GNU project and are therefore not necessarily
available. However, all widely used (GNU/)Linux distros have them
installed by default -- at least in parts. Unfortunately, the Debian
project considers the license of the \texttt{info} files as nonfree
and must thus be installed by hand using:
\begin{verbatim}
$ sudo apt install texinfo-doc-nonfree
\end{verbatim}
Don't ask...
To get the info page of \texttt{ls} type
\begin{verbatim}
$ info ls
\end{verbatim}
You can again use \texttt{SPACE} to scroll forward and \texttt{Q} to
get out of \texttt{info}.
Your next task is now to read
\begin{verbatim}
$ info info
\end{verbatim}
As \texttt{info} is a bit more sophisticated than \texttt{man},
offering links, footnotes, menus, and other references it is
essential to read this. Furthermore, \texttt{info} has a lot more
keyboard shortcuts you can use, all of which are documented in its
info page.
The difference between man pages and info pages is not a clearcut
one. As a rule of thumb, man pages are often more concise and only
useful if you already know what a program does in general, but just
want to check available options, i.e. they are often very technical
and offer few explanations.
The info pages, on the other hand, are often much more verbose, have
sections for different kinds of options (while the man pages just
list them one after the other) and at least try to be a bit more
accessible to complete beginners.
Just compare some man pages and info pages for the same program, e.g.
\texttt{man ls} vs. \texttt{info ls} to see the differences and get a
feeling for them.
Note also, that all documentation is written by humans and some are
more capable of explaining how stuff works than others. For this
reason the quality of man pages and info pages can vary greatly from
program to program. A good example can be found in
\href{https://xkcd.com/1692/}{this xkcd comic}.
% end info
\subsection{\texttt{--help} and \texttt{-h}}
\label{--helpand-h}
Sometimes you are just not sure about what was the right option to
get what you want or you only forgot the correct spelling of the
option. In these cases you may not want to read through the whole
man or info page and most programs make this possible by providing a
\texttt{--help} or \texttt{-h} option. So, typing \texttt{COMMAND
--help} or \texttt{COMMAND -h} will in most cases print a short
description of the command and its most common options to the screen.
Just try it out to see for yourself.
% end --help and -h
\subsection{\texttt{help} and \texttt{type}}
\label{helpandtype}
In \ref{shellsterminalscommands} we mentioned that not every command
you type is a full program, but some are just shell builtins. These
are keywords understood by your shell. You can compare this with your
GUI: Most icons you click on will start a new program, but some
things are done by the GUI itself, e.g. if you click on the Windows
button, it will display a list of programs you can start, but this
list is just part of the GUI.
For these shell builtins no man pages or info pages exist. A command
you'll use \emph{very} often is \texttt{cd} (``change directory")
which enables you to change your current working directory. But if
you try to type
\begin{verbatim}
$ man cd
\end{verbatim}
you'll get a man page titled ``\texttt{BASH\_BUILTINS(1)}" which will
most likely be very confusing. To get help specifically for
\texttt{cd} (or any other shell builtin for that matter) just type
\begin{verbatim}
$ help cd
\end{verbatim}
and the help will be displayed on the terminal.
But, you may ask yourself, how am I supposed to know what is a
program and what is a shell builtin?! Don't despair! This is where
the \texttt{type} shell builtin comes into play. If you tried to
access the man and/or info pages of a command and didn't get a
satisfying result, try
\begin{verbatim}
$ type COMMAND_NAME
\end{verbatim}
e.g.
\begin{verbatim}
$ type cd
cd is a shell builtin
\end{verbatim}
And voil\`{a}, now you can be sure.
% end help and type
\subsection{\texttt{which} and \texttt{whereis}}
\label{whatisandwhereis}
Many programs are continously developed and change from one release
to another; functionality may be added, reduced, removed, deprecated,
etc. And at some points you may not know whether you want the old or
the new version. One way to solve this is to simply install both, use
them and then decide which one you like more. On a GUI this may be
indicated by two different icons for the different versions, but how
can you tell the difference on the command line?
This is where the \texttt{which} and \texttt{whereis} utilities come
in handy.
\texttt{which} tells you where in the filesystem the executable you
are going to call is located and if you have installed two versions
each of them will reside in a different place. Thus, if you type
\begin{verbatim}
$ which pwd
/bin/pwd
\end{verbatim}
\texttt{which} tells you that if you call the \texttt{pwd} program,
it will use the one in the \texttt{/bin} directory. So, in case you
have a second version of \texttt{pwd} installed, which resides in
\texttt{/usr/local/bin}, you'll have to use the absolute path to call
it.
But sometimes you won't know if there are different versions of the
same program on your machine, e.g. when you start working in a new
place with your machine already set up for you. To find out your
possibilities try
\begin{verbatim}
$ whereis COMMAND
\end{verbatim}
which will show you where versions of \texttt{COMMAND} are installed,
where their man pages are located and if the source code of the
command is stored in a standard location on the system.
Attention: The following explanation will most likely make no sense
at all if you haven't read about variables in
\ref{variablesandenvironment}. The actual difference between
\texttt{whatis} and \texttt{whereis} is that the former looks
through your \texttt{PATH} variable and returns the
first executable it finds which actually is the one your shell would
execute while the latter scans through your whole \texttt{PATH} as
well as \texttt{MANPATH} plus some standard locations commonly used
for programs on any (GNU/)Linux system, and returns everything it
finds there, separated into executables, source files, and man pages.
% end whatis and whereis
\subsection{The Internet Is Your Friend}
\label{theinternetisyourfriend}
If you have to do something real quick you may find yourself in a
situation where you don't want to deal with man pages or info pages,
but would prefer a ready-made command with all options you want
already set. As it is very likely that other persons have had the
problem at hand before you and many people post everything they
achieve online, chances are that you can find the command of your
dreams posted in some online forum, ready to be copy-pasted into your
terminal emulator.
As you might expect, there are a myriad different sources where you
can find information, but we will only deal with very few of them
which offer reliable information in most cases.
\subsubsection{Distros' Websites}
\label{distroswebsites}
Each distro has its website and often Wikis attached to them. Many
have a lot of useful information, but keep in mind that these are
distro-specific. It is also important to know how your distro is
organized; in the case of Debian, you are faced with a community
project where the whole content is provided by people spending their
spare time on providing information to others, but no one is
responsible for doing so. This often leads to very helpful replies to
questions, targeted at total beginners, but as no one is responsible
for updating this content, sometimes you find very outdated
information. Red Hat Enterprise Linux, on the other hand,
is a distro aimed at corporations, providing a \emph{lot} of
information. However, as their material is intended for
IT-professionals, you won't find a lot of explanation in many cases as
they assume you already know what you are doing/want to do and just
show you how you can do it with the tools Red Hat provides.
Thus, do a bit of research about each distro before using their
documentation and how-tos.
\begin{description}
\item [Debian:] Information about Debian can be found at:
\begin{itemize}
\item \href{https://www.debian.org}
{https://www.debian.org}
\item \href{https://wiki.debian.org/}
{https://wiki.debian.org/}
\item \href{https://debian-handbook.info/}
{https://debian-handbook.info/}. This is the Debian
Administrator's Handbook, the e-book version of which can be
downloaded freely. However, you can also pay for the e-book
version, and in case you start relying on it, it is definitely
a good idea to give the authors something back for their hard
work. The current (2018-09-12) version of the book if for
Debien 8 Jessie, but the current Debian release is Debian 9
Stretch, so some information my be outdated.
\end{itemize}
\item [Ubuntu:] Ubuntu is an enterprise distribution with a desktop
and a server version. They have a very active community as well
which answers a lot of questions in the forums.
\begin{itemize}
\item \href{https://www.ubuntu.com}{https://www.ubuntu.com}
\item \href{https://tutorials.ubuntu.com/}
{https://tutorials.ubuntu.com/}. You can filter tutorials for
different categories, e.g. server or desktop.
\item \href{https://help.ubuntu.com/}{https://help.ubuntu.com/}
\item \href{https://ubuntuforums.org/}
{https://ubuntuforums.org/}
\item \href{https://askubuntu.com/}{https://askubuntu.com/} This
is a searchable question and answer site.
\end{itemize}
\item [Red Hat Enterprise Linux:] As mentioned above, RHEL is an
enterprise distribution and one of the most prominent examples
showing how much money you can earn with free software. They have
excellent, though rather technical guides for many topics, and
some things can be only accessed if you have a support contract
with Red Hat.