-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinux.tex
More file actions
755 lines (632 loc) · 19.5 KB
/
linux.tex
File metadata and controls
755 lines (632 loc) · 19.5 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
\documentclass{beamer}
\mode<presentation>
{
\usetheme{Madrid}
}
\usepackage{graphics, graphicx}
\usepackage{booktabs}
\DeclareGraphicsExtensions{.pdf,.png,.jpg,.gif}
\title{Linux Beginner Guide}
\author{Jaewoong Lee}
\institute[UNIST]
{
Ulsan National Institute of Science and Technology
\medskip
\newline
\textit{jaewoong@unist.ac.kr}
}
\date{\today}
\begin{document}
\begin{frame}
\titlepage
\end{frame}
\begin{frame}
\frametitle{Introduction}
In this guide, I assume that you can open a Linux shell in one of these ways:
\begin{enumerate}
\item A current Ubuntu LTS desktop, WSL, or course-provided Linux server
\item A terminal running bash or zsh
\item A basic terminal editor such as Vim, Neovim, or nano
\item SSH access if the lab uses a remote server
\end{enumerate}
With this guide, you can use and understand a Linux system. \\
The commands are written for a beginner lab in 2026. Your prompt, user name, host name, or Linux distribution may look different, and that is fine.
\end{frame}
\begin{frame}
\frametitle{Overview}
\tableofcontents
\end{frame}
\section{Linux?}
\begin{frame}
\frametitle{Linux?}
\begin{figure}[h!]
\centering
\includegraphics[width=0.4 \linewidth]{figures/linus.jpg}
\caption{Linus Torvalds, Inventor of Linux}
\end{figure}
Linux is a widely used operating system family, alongside Windows and macOS. \\
Linux is open source and is used on servers, cloud systems, laptops, embedded devices, and phones. \\
Android, a mobile operating system, is based on Linux.
\end{frame}
\begin{frame}
\frametitle{Ubuntu?}
\begin{figure}[h!]
\centering
\includegraphics[width=0.3 \linewidth]{figures/ubuntu.jpg}
\caption{Logo of Ubuntu}
\end{figure}
Ubuntu is a beginner-friendly Linux distribution. \\
It is common in classrooms, cloud images, WSL, and personal machines, but most commands in this guide also work on other Linux distributions.
\end{frame}
\section{Basic Linux Command}
\begin{frame}
\frametitle{Where we start}
\begin{figure}[h!]
\centering
\includegraphics[width=0.5 \linewidth]{figures/1.png}
\caption{Here is where we start}
\end{figure}
After you open a terminal or connect to a server via SSH, you may see a prompt like this. \\
Here is where we start! \\
The text before `@' is the user name, and the text after `@' is the server or computer name.
\end{frame}
\begin{frame}
\frametitle{pwd}
\begin{figure}[h!]
\centering
\includegraphics[width=0.5 \linewidth]{figures/2.png}
\caption{Result of \textit{pwd} Command}
\end{figure}
\textit{pwd} is abbr. of "Print Working Directory". \\
You can see where you are with \textit{pwd} command. \\
Also, "/home/username" is your \textit{home folder}, a.k.a. '$\sim$'.
\end{frame}
\begin{frame}
\frametitle{ls}
\begin{figure}[h!]
\centering
\includegraphics[width=0.7 \linewidth]{figures/3.png}
\caption{Result of \textit{ls} Command}
\end{figure}
\textit{ls} stands for "List". \\
\textit{ls} command lists current directory contents. \\
If current directory is empty, the result will be nothing.
\end{frame}
\begin{frame}
\frametitle{Configuration}
Before starting the lab, make a small working directory. No custom dotfiles, shell theme, or plugin setup is required.
\begin{example}
\$ mkdir -p $\sim$/linux-lab \\
\$ cd $\sim$/linux-lab \\
\$ pwd
\end{example}
Note that you should input only the command after '\$'. \\
If you use your own Ubuntu machine and a command is missing, install the basic tools with \textit{sudo apt install vim curl wget screen}.
\end{frame}
\begin{frame}
\frametitle{Configuration \textit{(Cont.)}}
\begin{figure}[h!]
\centering
\includegraphics[width=0.7 \linewidth]{figures/4.png}
\caption{Shell Prompt}
\end{figure}
Your prompt may look different from this image. The important part is that you know which directory you are in.
\end{frame}
\begin{frame}
\frametitle{Tip!}
\begin{figure}[h!]
\centering
\includegraphics[width=0.7 \linewidth]{figures/5.png}
\caption{Right Command vs. Wrong Command}
\end{figure}
In this screenshot, valid commands are highlighted in green. Treat this as a helpful hint, not as a replacement for reading the command before pressing Enter.
\end{frame}
\begin{frame}
\frametitle{mkdir}
\textit{mkdir} stands for "Make Directory". \\
You can make a directory which named 'test' as following:
\begin{example}
\$ mkdir test \\
\$ ls
\end{example}
\textit{mkdir} returns nothing when it succeeds. Literally, \textit{mkdir} command only makes a directory. \\
You can check that the directory has been made with \textit{ls} command.
\end{frame}
\begin{frame}
\frametitle{cd}
\textit{cd} is abbr. of "Change Directory". \\
You can change your working directory to 'test' as following:
\begin{example}
\$ pwd \\
\$ cd test\\
\$ pwd \\
\end{example}
Also, you can go your home folder at once with \textit{cd}, no matter where you are.
\begin{figure}[h!]
\centering
\includegraphics[width=0.7 \linewidth]{figures/6.png}
\caption{\textit{cd} will guide you to home folder}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Tip!}
If you hit the "Tab" key, bash and zsh can complete file names, directory names, and commands. \\
Following example shows what tab completion gives.
\begin{figure}
\centering
\includegraphics[width=0.7 \linewidth]{figures/10.png}
\caption{Shortcut with Tab}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{man and -$ $-help}
You can get detailed information about command as following:
\begin{example}
\$ man ls \\
and/or \\
\$ ls -$ $-help
\end{example}
This guide gives simple information about Linux commands. When you want more detail about a command, use these commands.
\end{frame}
\begin{frame}
\frametitle{Directory Structure}
Try following commands:
\begin{example}
\$ cd test\\
\$ ls -al
\end{example}
Then, you can see like this:
\begin{figure}[h!]
\centering
\includegraphics[width=0.3 \linewidth]{figures/7.png}
\caption{Result of \textit{ls} command}
\end{figure}
All directory has '.' and '..', even though the directory is empty. \\
'.' means current directory itself; and, '..' means parent directory.
\end{frame}
\begin{frame}
\frametitle{touch}
\textit{touch} command make new file or touch the file.
Try following example:
\begin{example}
\$ cd \\
\$ touch t \\
\$ ls
\end{example}
Then, you can see that the file which name 't' has been made.
\begin{figure}[h!]
\centering
\includegraphics[width=0.5 \linewidth]{figures/8.png}
\caption{Result of \textit{touch} Command}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{mv}
\textit{mv} command moves/renames file. \textit{mv} is used as:
\begin{example}
\$ mv SRC(source) DST(destination)
\end{example}
Try following commands:
\begin{example}
\$ mv t tmp \\
\$ ls \\
\$ mv tmp test/ \\
\$ ls
\end{example}
Then, you will realize that the file 'tmp' is gone. I hope that you already know where the file goes. :)
\end{frame}
\begin{frame}
\frametitle{cp}
\textit{cp} command copies SRC to DST. \textit{cp} is used as:
\begin{example}
\$ cp SRC DST
\end{example}
Try following commands:
\begin{example}
\$ cd $\sim$/test/ \\
\$ ls \\
\$ cp tmp tmp2 \\
\$ ls
\end{example}
Then, you can realize that a new file 'tmp2' has been made.
\end{frame}
\begin{frame}
\frametitle{rm}
\textit{rm} stands for 'Remove'. As its name, you can delete files or directories.
\begin{example}
\$ rm tmp2
\end{example}
When you want to delete a directory, use '-r' option:
\begin{example}
\$ rm -r directoryname
\end{example}
There is no way to restore removed files!! Beware what you remove!!
\end{frame}
\begin{frame}
\frametitle{sudo}
\textit{sudo} is abbr. of "Substitute User do"; but, many people know as "Super User do". \\
\textit{sudo} allows a system administrator to delegate authority to give certain user the ability to run some command as another user.
\begin{figure}[h!]
\centering
\includegraphics[width=0.3 \linewidth]{figures/sudo.png}
\caption{XKCD: Sandwich}
\end{figure}
THINK what will happen after \textit{sudo} command!!
\end{frame}
\section{Edit File with Vim}
\begin{frame}
\frametitle{Editor}
Common choices include nano, Vim/Neovim, Emacs, and VS Code Remote SSH.
\begin{figure}[h!]
\centering
\includegraphics[width=0.7 \linewidth]{figures/editor.png}
\caption{Descriptions of Editor}
\end{figure}
This guide uses Vim because it is available on many servers.
\end{frame}
\begin{frame}
\frametitle{Editor \textit{(Cont.)}}
\begin{figure}[h!]
\centering
\includegraphics[width=0.7 \linewidth]{figures/curves.jpg}
\caption{Learning Curves among Editors}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{First Meet with Vim}
With these commands, you can make/edit file.
\begin{example}
\$ vim notes.txt
\end{example}
If it is your first time to open Vim, then you may see something like this.
\begin{figure}[h!]
\centering
\includegraphics[width=0.5 \linewidth]{figures/9.png}
\caption{First Time of Vim}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Modes of Vim}
\begin{figure}[h!]
\centering
\includegraphics[width=0.7 \linewidth]{figures/modes.png}
\caption{Three Modes in Vim}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{How to Edit with Vim}
Editing with Vim follows these steps:
\begin{enumerate}
\item Press 'i'
\item Edit the file
\item Press 'ESC'
\item Enter ':w' which means \textit{write}
\item Enter ':q' which means \textit{quit}
\end{enumerate}
\end{frame}
\begin{frame}
\frametitle{Editor Practice (Optional)}
No Vim plugin is required for this lab. If you want a guided practice session, run:
\begin{example}
\$ vimtutor
\end{example}
If \textit{vimtutor} is not installed on your own Ubuntu machine, install Vim with:
\begin{example}
\$ sudo apt install vim
\end{example}
\end{frame}
\section{IO Redirections}
\begin{frame}
\frametitle{cat}
\textit{cat} stands for con\textbf{cat}enate. \textit{cat} command reads files, and writing them to standard output. \\
Consider following example:
\begin{example}
\$ cd $\sim$/test/ \\
\$ cat tmp
\end{example}
You can see contents of file.
\end{frame}
\begin{frame}
\frametitle{Input/Output to file}
If you want redirect output to file, use following example:
\begin{example}
\$ cat tmp $>$ output
\end{example}
However, this method \textit{overwrites} the contents of file. \\
If you want preserve the file contents, use following:
\begin{example}
\$ cat tmp $>>$ output
\end{example}
Also, $<$ means input from file.
\end{frame}
\begin{frame}
\frametitle{Output to file \textit{Cont.}}
In some cases, you should divide standard output and standard error. \\
In these cases, use following commands:
\begin{example}
\$ commands 1$>$ STDOUT 2$>$ STDERR
\end{example}
\begin{example}
\$ cat tmp 1$>$ ex.stdout 2$>$ ex.stderr
\end{example}
If you want to save both standard output and standard error in one file:
\begin{example}
\$ commands 1$>$log 2$>$\&1
\end{example}
\end{frame}
\begin{frame}
\frametitle{more / less}
\textit{more} and \textit{less} are commands for seeing the contents of file.
Consider following examples:
\begin{example}
\$ more tmp
\end{example}
\begin{example}
\$ less tmp
\end{example}
Also, hit "Q" when you want to leave \textit{more} or \textit{less}.
\end{frame}
\begin{frame}
\frametitle{Pipe}
Use pipe ($|$) to indicate input as output of previous command.
\begin{example}
\$ command1 $|$ command2
\end{example}
The output of command 1 will be the input of command2. \\
Consider following example:
\begin{example}
\$ cat tmp $|$ less
\end{example}
\end{frame}
\section{How to Download from Web}
\begin{frame}
\frametitle{Two Ways for Download}
There are two main ways for download.
\begin{enumerate}
\item curl
\item wget
\end{enumerate}
\end{frame}
\begin{frame}
\frametitle{curl}
\begin{example}
\$ curl https://example.com
\end{example}
\textit{curl} returns to standard output. If you want to get file, consider following:
\begin{example}
\$ curl -L https://example.com -o example.html
\end{example}
\end{frame}
\begin{frame}
\frametitle{wget}
\textit{wget} returns a downloaded file as output.
\begin{example}
\$ wget https://example.com -O example.html \\
\$ ls
\end{example}
\end{frame}
\begin{frame}
\frametitle{gzip}
\textit{gzip} is used for file compression and decompression.
When compressing:
\begin{example}
\$ gzip tmp
\end{example}
When decompressing:
\begin{example}
\$ gzip -d tmp.gz
\end{example}
However, the examples hereinabove delete the original files. If you want \textit{keep} original file, consider following:
\begin{example}
\$ gzip -k tmp
\end{example}
\end{frame}
\begin{frame}
\frametitle{TAR Files}
\textit{TAR} stands for "Tape Archives". \\
Originally, it was used for tape; today, it is used to bundle many files into one archive. \\
TAR.GZ files are commonly used for source code and lab data. Replace LAB-URL with the URL from your instructor:
\begin{example}
\$ wget LAB-URL -O lab-files.tar.gz
\end{example}
(TGZ is for TAR.GZ)
\end{frame}
\begin{frame}
\frametitle{TAR Files \textit{(Cont.)}}
You might think you must decompress the GZ file and then extract the TAR file. However, you can extract a TGZ or TAR.GZ file at once:
\begin{example}
\$ tar -xzf lab-files.tar.gz
\end{example}
Then, you can see the extracted directory with \textit{ls}.
\end{frame}
\section{User Permissions}
\begin{frame}
\frametitle{Permissions}
With the following command, we can know how permissions are set:
\begin{example}
\$ ls -al
\end{example}
\begin{figure}[h!]
\centering
\includegraphics[width=0.4 \linewidth]{figures/11.png}
\caption{File Permissions}
\end{figure}
The way to read this result is following:
\begin{figure}[h!]
\centering
\includegraphics[width=0.5 \linewidth]{figures/permissions.jpg}
\caption{How to Read Permissions}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{chmod}
\textit{chmod} stands for "Change Mode". You can modify permissions of files.
Before input command, you should calculate simple arithmetic:
\begin{figure}[h!]
\centering
\includegraphics[width=0.4 \linewidth]{figures/permissions2.png}
\caption{Simple Arithmetic}
\end{figure}
Then, you will get three digits for permission. Moreover, 660 or 770 are usually used.
\begin{example}
\$ chmod \textcolor{red}{three\_digits} filename
\end{example}
\end{frame}
\begin{frame}
\frametitle{chmod \textit{(Cont.)}}
Or, you can do as followings:
\begin{example}
\$ chmod rwx-$ $-$ $-$ $-$ $-$ $- tmp
\end{example}
\begin{example}
\$ chmod g=rwx tmp
\end{example}
\begin{example}
\$ chmod +x tmp
\end{example}
\end{frame}
\begin{frame}
\frametitle{chown}
\textit{chown} stands for "change ownership". As its name, you can modify ownership of file.
When you want to change only USER:
\begin{example}
\$ chown USER tmp
\end{example}
When you want to change both USER and GROUP:
\begin{example}
\$ chown USER:GROUP tmp
\end{example}
\end{frame}
\section{Process Control}
\begin{frame}
\frametitle{Ctrl-C}
When you have started command, but you realize that you should stop the command, then use "Ctrl-C".
\begin{example}
\$ sleep 99999 \\
\$ \^{}C
\end{example}
In terminal output, \^{} means "Control"; \^{}C means Ctrl-C. \\
Ctrl-C sends SIGINT, which stands for "Signal Interruption"; and, the process is going to terminate after receiving SIGINT.
\end{frame}
\begin{frame}
\frametitle{\&}
Consider long-time procedure, such as:
\begin{example}
\$ sleep 99999
\end{example}
However, with '\&', you do not have to wait procedure.
\begin{example}
\$ sleep 99999 \&
\end{example}
Then, the process is executing on background.
\end{frame}
\begin{frame}
\frametitle{jobs}
\textit{jobs} commands shows the background process.
\begin{example}
\$ jobs
\end{example}
\begin{figure}[h!]
\centering
\includegraphics[width=0.5 \linewidth]{figures/12.png}
\caption{Result of \textit{jobs}}
\end{figure}
You use record the [number] for handle the process. \\
\end{frame}
\begin{frame}
\frametitle{kill}
\textit{kill} command kills the process.
When you want to kill background process, consider following:
\begin{example}
\$ kill \%number
\end{example}
The number is from the \textit{jobs} command.
\begin{example}
\$ kill process\_number
\end{example}
You can kill as above when you know exact process number (PID).
\end{frame}
\begin{frame}
\frametitle{nohup}
When you lost from SSH connection, all executing process receive SIGHUP, which stands for Signal Hangup, and will be terminate even they runs on background. \\
To prevent SIGHUP, use \textit{nohup} command.
\begin{example}
\$ nohup sleep 99999 \&
\end{example}
However, \textit{nohup} command makes 'nohup.out' automatically. You can change output file name with IO redirection.
\end{frame}
\begin{frame}
\frametitle{screen}
\textit{screen} prevents unintentional connection lost.
Make screen session with simple command:
\begin{example}
\$ screen
\end{example}
When you want to detach the screen session, use following, instead of \textit{exit}:
\begin{example}
\$ \^{}a, d
\end{example}
When you restore the screen session, use following command:
\begin{example}
\$ screen -r
\end{example}
\end{frame}
\section{Process Control with Slurm}
\begin{frame}
\frametitle{Slurm}
We use a shared server or cluster, not a dedicated machine. Therefore, CPU, memory, GPU, and time should be distributed across all users. \\
Slurm is a job scheduler. It places your job in a queue, starts it when resources are available, and records the result.
\end{frame}
\begin{frame}
\frametitle{Script File}
Script file is a text file containing a collection of shell statements and Slurm options.
Make a script file via Vim as:
\begin{example}
\$ vim tmp.sh
\end{example}
Also, the file contents should be:
\begin{example}
\#!/bin/bash \\
\#SBATCH -$ $-job-name=linux-lab \\
\#SBATCH -$ $-mem=1G \\
\#SBATCH -$ $-cpus-per-task=1 \\
hostname \\
sleep 30
\end{example}
Lines beginning with \#SBATCH tell Slurm what resources the job needs.
\end{frame}
\begin{frame}
\frametitle{sbatch}
You can submit the script file to the queue with \textit{sbatch} command:
\begin{example}
\$ sbatch tmp.sh
\end{example}
When you want to designate the log file names, consider following:
\begin{example}
\$ sbatch -$ $-output=outputlog.txt -$ $-error=errorlog.txt tmp.sh
\end{example}
\end{frame}
\begin{frame}
\frametitle{squeue}
You can check your jobs with \textit{squeue} command:
\begin{example}
\$ squeue -u \$USER
\end{example}
If you want to check jobs from all users, consider following:
\begin{example}
\$ squeue
\end{example}
Note that you should record the JOBID for further handling.
\end{frame}
\begin{frame}
\frametitle{scancel}
If you want to cancel submitted jobs, you can use \textit{scancel} command:
\begin{example}
\$ scancel JOBID
\end{example}
If you want to cancel all jobs from you, consider following:
\begin{example}
\$ scancel -u \$USER
\end{example}
\end{frame}
\end{document}