You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: presentation/linux_bash_metacentrum_course.tex
+99-66Lines changed: 99 additions & 66 deletions
Original file line number
Diff line number
Diff line change
@@ -37,7 +37,7 @@
37
37
38
38
% Other packages
39
39
\usepackage{multicol}
40
-
\usepackage{tabularx}
40
+
\usepackage{array}
41
41
42
42
% In-line higlighting
43
43
\usepackage{soul}
@@ -1091,7 +1091,7 @@ \section{Command line}
1091
1091
\begin{frame}{Launching commands and scripts}
1092
1092
\begin{itemize}
1093
1093
\item Parameters of commands are separated by space and preceded by one or two dash(es)
1094
-
\item Parameter -\texttt{h} or -{-}\texttt{help} usually gives help for particular command
1094
+
\item Parameter \texttt{-h} or \texttt{-{-}help} usually gives help for particular command
1095
1095
\item Getting help with \texttt{man} command
1096
1096
\begin{itemize}
1097
1097
\item\texttt{man somecommand}
@@ -1100,8 +1100,8 @@ \section{Command line}
1100
1100
\item Command \texttt{info} more advanced --- type \texttt{?} for help
1101
1101
\end{itemize}
1102
1102
\item Parameters can be combined, order doesn't matter (same variants: \texttt{ls -la}; \texttt{ls -al}; \texttt{ls -a -l}; \texttt{ls -l -a})
1103
-
\item\enquote{Long} parameters (-{-}\texttt{XXX}) must stay separated
1104
-
\item Commands must be in PATH (slide~\ref{PATH}) --- actual directory isn't
1103
+
\item\enquote{Long} parameters (\texttt{-{-}XXX}) must stay separated
1104
+
\item Commands (applications) must be in PATH (slide~\ref{PATH}) --- actual directory isn't
1105
1105
\begin{itemize}
1106
1106
\item If the script is is current directory, use \texttt{./script.sh} or full path
1107
1107
\end{itemize}
@@ -1114,9 +1114,9 @@ \section{Command line}
1114
1114
\begin{itemize}
1115
1115
\item macOS contains outdated versions of many command line utilities with limited functionality comparing to what we are going to use (what is available in modern Linux distributions)
1116
1116
\item Several projects provide Linux style way of installation and update of various (not only) command line tools, probably the best is \href{https://brew.sh/}{Homebrew}
1117
-
\item Homebrew contains also plenty of scientific packages, there is also specialized similar \href{https://brewsci.github.io/homebrew-bio/}{source for bioinformatics}
1117
+
\item Homebrew contains also plenty of scientific packages, there is also specialized similar \href{https://brewsci.github.io/homebrew-bio/}{source for bioinformatics} (and another sciences)
1118
1118
\item Tools installed via Homebrew are installed into \texttt{/usr/local} not to interact with system packages
1119
-
\item Derived project is \href{https://docs.brew.sh/Homebrew-on-Linux}{Linuxbrew} (works also on Windows subsystem for Linux) useful especially for installation of special (scientific) software unavailable in main Linux repositories (software resources)
1119
+
\item Derived project is \href{https://docs.brew.sh/Homebrew-on-Linux}{Linuxbrew} (works also on Windows subsystem for Linux) useful especially for installation of some special (scientific) software unavailable in main Linux repositories (software resources)
1120
1120
\end{itemize}
1121
1121
\end{frame}
1122
1122
@@ -1172,7 +1172,7 @@ \section{Command line}
1172
1172
1173
1173
\begin{frame}{The command line can has various look and feel\ldots}{Change colors, font size, etc. for your terminal to like it more and work comfortably}
# Or name the new session (useful if there are more sessions)
1203
1202
tmux new -s SomeName
1204
1203
# Detach from the session by Ctrl+B, D
@@ -1214,6 +1213,7 @@ \subsection{Screen}
1214
1213
\begin{itemize}
1215
1214
\item Get help by \texttt{Ctrl+B, ?} (\texttt{Q} to quit)
1216
1215
\item Split window by \texttt{Ctrl+B, \%} and navigate between them by \texttt{Ctrl+B, L/R arrow}
1216
+
\item It has plenty of options
1217
1217
\end{itemize}
1218
1218
\end{frame}
1219
1219
@@ -1233,7 +1233,7 @@ \subsection{Screen}
1233
1233
\end{bashcode}
1234
1234
\vfill
1235
1235
\begin{itemize}
1236
-
\item Toy server: user names \texttt{cu01} --- \texttt{cu20}
1236
+
\item Toy server: user names \texttt{cu01}--\texttt{cu20}
1237
1237
\end{itemize}
1238
1238
\vfill
1239
1239
\begin{bashcode}
@@ -1286,22 +1286,22 @@ \subsection{BASH}
1286
1286
\end{itemize}
1287
1287
\end{frame}
1288
1288
1289
-
\begin{frame}[allowframebreaks]{Nice BASH features for easier work (selection)}
1289
+
\begin{frame}[allowframebreaks]{Nice BASH features for easier work (selection)}% TODO Add more BASH features
1290
1290
\begin{itemize}
1291
1291
\item Arrows up and down list in the history of commands
1292
1292
\item List whole history by command \texttt{history}
1293
1293
\item\texttt{Ctrl+R} --- reverse search in history --- type to search last command(s) containing typed character(s) --- repeat typing \texttt{Ctrl+R} to search deeper in history
1294
1294
\item\texttt{TAB} --- list command and files starting by typed characters
1295
1295
\item\texttt{Home}/\texttt{End} --- go to beginning/end of the line
\item\texttt{Ctrl+Shift+C}/\texttt{V} --- copy/paste the text
1297
+
\item\texttt{Ctrl+Shift+C}/\texttt{V} --- copy/paste the text from terminal emulator
1298
1298
\item\texttt{Ctrl+C} --- cancel running task
1299
1299
\item\texttt{Ctrl+D} --- log out (like commands \texttt{exit} or \texttt{logout})
1300
1300
\item\texttt{Ctrl+U} --- move text before cursor into clipboard
1301
1301
\item\texttt{Ctrl+K} --- move text after cursor into clipboard
1302
1302
\item\texttt{Ctrl+left/right arrow} --- skip words
1303
1303
\item\texttt{Ctrl+T} --- flip current and left character
1304
-
\item\texttt{Ctrl+X+E} --- start text editor in current directory
1304
+
\item\texttt{Ctrl+X+E} --- start text editor (default, defined in \texttt{$\sim$/.bashrc}) in current directory
1305
1305
\end{itemize}
1306
1306
\end{frame}
1307
1307
@@ -1343,28 +1343,42 @@ \subsection{BASH}
1343
1343
\begin{bashcode}
1344
1344
# Define new alias
1345
1345
alias ll="ls -l"
1346
-
# Since now, instead of "ls -l" we can write just "ll"
1347
-
# To make the change above permanent, write it into ~/.profile or
1348
-
# ~/.bash_profile or ~/.bashrc and reload the configuration by e.g.
1349
-
source ~/.bashrc # to reload BASH settings
1350
-
# If there are many aliases, they can be stored e.g. in ~/.alias
1351
-
test -s ~/.alias && . ~/.alias || true # Check for extra alias file
1346
+
\end{bashcode}
1347
+
\begin{itemize}
1348
+
\item Can be stored in \texttt{$\sim$/.bashrc} (or \texttt{$\sim$/.profile} or \texttt{$\sim$/.bash\_profile})
1349
+
\item If there are plenty of them, aliases can go to \texttt{$\sim$/.alias} and \texttt{$\sim$/.bashrc} then contain \texttt{test -s $\sim$/.alias \&\& . $\sim$/.alias || true}
1350
+
\end{itemize}
1351
+
\begin{bashcode}
1352
+
# After adding new aliases to ~/.bashrc or ~/.alias or so reload it
1353
+
source ~/.bashrc # Reload BASH settings to load newly aliases
1352
1354
# Popular aliases
1355
+
eval "$(dircolors -b)" # Make output of ls colored
1353
1356
alias ls="ls --color=auto" # Make output of ls colored
1354
1357
alias l="ls -la" # Long list (add details) with hidden files
1355
-
# Popular settings in ~/.bashrc (influencing bash, not other shells)
1356
1358
alias grep='grep --color=auto' # Enable color in grep
1357
-
# Always human readable output of df (disk free)
1358
-
alias df='df -h'
1359
+
alias df='df -h' # Always human readable output of df (disk free)
1360
+
# Add aliases pointing to software installed outside PATH, ...
1359
1361
\end{bashcode}
1360
1362
\end{frame}
1361
1363
1362
1364
\begin{frame}[fragile]{Aliases and BASH settings II}
1363
1365
\begin{bashcode}
1364
1366
# Easier history listing
1365
1367
alias his="history | grep" # Use e.g. 'his ls' to list last 'ls' usage
1366
-
# Add aliases pointing to software installed outside PATH, ...
1368
+
# Add ~/.local/bin to PATH (directories with commands and scripts)
1369
+
export PATH=$PATH:~/.local/bin
1370
+
# Colored GCC warnings and errors when compiling from source code
rm -r # Recursive delete - remove also non-empty directories
1389
1403
mv from to # Move files/directories (also for renaming)
1404
+
mv docs to/sub/directory/ # Move 'docs' to 'to/sub/directory/'
1390
1405
\end{bashcode}
1391
1406
\end{frame}
1392
1407
1393
1408
\begin{frame}[fragile]{Directories and files}
1394
1409
\begin{bashcode}
1395
1410
cp from to # Copy, -r (recursive, including subdirectories)
1396
1411
# -a (keeps all attributes), -v (verbose)
1412
+
# Copy 'XXX' (recursively with subdirectories and everything) in the
1413
+
# upper directory into 'sub/directory/'
1414
+
cp -a ../XXX sub/directory/
1415
+
# Copy 'doc.txt' from home directory into current directory
1416
+
cp ~/doc.txt . # Dot stands for current directory
1397
1417
file somefile # Information about questioned file (what it is, ...)
1398
1418
xdg-open somefile # Open file by graphical application as in GUI
1399
1419
\end{bashcode}
1420
+
\begin{itemize}
1421
+
\item When using \texttt{cd}, \texttt{cp}, \texttt{mv}, \ldots use \texttt{<TAB><TAB>} key suggesting matching names of files and directories and save repeated and unneeded typing
1422
+
\item In command line, \textbf{user is always in some directory} --- \alert{it's crucial to train fluent moving among directories and manipulation with files}
1423
+
\begin{itemize}
1424
+
\item If lost among directories, run \texttt{pwd} to find out current directory and \texttt{ls} to see what is around
1425
+
\end{itemize}
1426
+
\end{itemize}
1400
1427
\end{frame}
1401
1428
1402
1429
\begin{frame}[allowframebreaks]{Tasks on the remote server}
@@ -1415,9 +1442,12 @@ \subsection{Directories}
1415
1442
\item Create other directory, see it and then remove (\texttt{rmdir}).
1416
1443
\item Can you access directories of another users? Why? If yes, what are your permissions there? Explain it.
1417
1444
\item What are some permissions in \texttt{/}? Why?
1445
+
\item Define some alias (by running \texttt{alias} command, not by edit of \texttt{$\sim$/.bashrc}) and use it.
1418
1446
\item Create directory in your home directory and share it with another user so she/he can write there anything (using e.g. \texttt{touch somefile} or \texttt{mkdir somedirectory}) (work e.g. in pairs). Use everywhere as restricted permissions as possible. Can you figure out solution with or without ACL (slide~\ref{acl})?
1419
1447
\item Practice moving between \texttt{/home/scripts\_data} and your home directory. Use \texttt{cd} and \texttt{TAB}.
1420
1448
\item Within \texttt{/home/scripts\_data} list by single command only \texttt{jpg} and \texttt{txt} files.
1449
+
\item Create in your home directory new directory \texttt{scripts} and copy there with single command all shell scripts (\texttt{*.sh}) files from \texttt{/home/scripts\_data}
1450
+
\item Copy anywhere into your home \texttt{/home/scripts\_data} and by single command remove all \texttt{jpg} and \texttt{sh} files there.
1421
1451
\end{enumerate}
1422
1452
\end{frame}
1423
1453
@@ -1441,48 +1471,40 @@ \subsection{Directories}
1441
1471
1442
1472
\subsection{Archives}
1443
1473
1444
-
\begin{frame}{Compressing files into archives}
1474
+
\begin{frame}{Compressing and decompressing archives}
1445
1475
\begin{center}
1446
-
\begin{tabular}{llll}
1447
-
\textbf{Archive} & \textbf{Compressing command}\\
1448
-
*.tar & tar cvf archive.tar file1 file2\\
1449
-
*.tar.gz\alert{/}*.tgz & tar czvf archive.tar.gz\alert{/}.tgz file1 file2\\
1450
-
*.tar.bz\alert{/}*.tbz\alert{/}*.tar.bz2 & tar cjvf archive.tar.bz\alert{/}.tbz\alert{/}.tar.bz2 file1 file2\\
*.tar & tar cvf archive.tar file1 file2 file3 & tar xvf archive.tar\\
1479
+
*.tar.gz\alert{/} *.tgz & tar czvf archive.tar.gz\alert{/}.tgz file1 file2 & tar xzvf archive.tar.gz\alert{/}.tgz\\
1480
+
*.tar.bz\alert{/} *.tbz\alert{/} *.tar.bz2 & tar cjvf archive.tar.bz\alert{/}.tbz\alert{/}.tar.bz2 file1 file2 file3 file4 & tar xjvf archive.tar.bz\alert{/}.tbz\alert{/}.tar.bz2\\
1481
+
*.tar.xz & tar cvJf archive.tar.xz file1 file2 file4 & tar xvJf archive.tar.xz\\
1482
+
*.tar.lzma & tar cvf - file1 file2 file3 file4 | lzma > archive.tar.lzma & lzcat archive.tar.lzma | tar xvf -\\
1483
+
*.gz & gzip file & gunzip archive.gz\\
1484
+
*.bz2 & bzip2 file & bunzip2 archive.bz2\\
1485
+
*.xz & xz -zv file & xz -d archive.xz\\
1486
+
*.lzma & lzma file & unlzma archive.lzma\\
1487
+
*.zip & zip -r archive.zip file1 file2 & unzip archive.zip\\
1488
+
*.rar & rar a archive.rar file1 file2 & unrar x archive.rar
1457
1489
\end{tabular}
1458
1490
\end{center}
1459
-
\begin{itemize}
1460
-
\item\texttt{gzip}, \texttt{bzip2} and \texttt{lzma} are able to pack only one file --- use them together with \texttt{tar} to pack multiple files (when used \textbf{without} \texttt{tar} \alert{move} file into archive)
1461
-
\item\texttt{lzma} has excellent compression, but can be very slow
1462
-
\end{itemize}
1463
1491
\end{frame}
1464
1492
1465
1493
\begin{frame}{Compressing and decompressing archives}
*.tar.gz\alert{/}*.tgz & tar xzvf archive.tar.gz\alert{/}.tgz\\
1471
-
*.tar.bz\alert{/}*.tbz\alert{/}*.tar.bz2 & tar xjvf archive.tar.bz\alert{/}.tbz\alert{/}.tar.bz2\\
1472
-
*.tar.xz & lzcat archive.tar.xz | tar xvf -\\
1473
-
*.gz & gunzip archive.gz\\
1474
-
*.bz2 & bunzip2 archive.bz2\\
1475
-
*.xz & unlzma archive.xz\\
1476
-
*.zip & unzip archive.zip\\
1477
-
*.rar & unrar x archive.rar
1478
-
\end{tabular}
1494
+
\begin{itemize}
1495
+
\item\texttt{gzip}, \texttt{bzip2}, \texttt{xz} and \texttt{lzma} are able to pack only one file --- use them together with \texttt{tar} to pack multiple files (when used \textbf{without} \texttt{tar} they \alert{move} file into archive)
1496
+
\item In Linux, \texttt{gzip} (and less \texttt{bzip2}) are the most commonly used
1497
+
\item Rar is not used at Linux/UNIX at all
1498
+
\item Zip is probably the most portable between Linux/UNIX and Windows
1499
+
\item\texttt{lzma} and \texttt{xz} have excellent compression, but can be very slow, use similar algorithm, often confused
1500
+
\end{itemize}
1479
1501
\hfill
1502
+
\begin{center}
1480
1503
\includegraphics[height=1.75cm]{tar.png}
1481
-
\begin{flushright}
1482
-
\url{https://xkcd.com/1168/}
1483
-
\end{flushright}
1484
-
\hfill
1485
1504
\end{center}
1505
+
\begin{flushright}
1506
+
\url{https://xkcd.com/1168/}
1507
+
\end{flushright}
1486
1508
\end{frame}
1487
1509
1488
1510
\begin{frame}{Tasks with archives}
@@ -1493,8 +1515,8 @@ \subsection{Archives}
1493
1515
\item Compare sizes of original files and compressed outputs.
1494
1516
\item Which compression tool seems to be the best? In terms of compressing ratio and time needed for compression.
1495
1517
\item Is more effective compression of text files or images? Why?
1496
-
\item Why is even plain \texttt{tar} (without compression, it requires \texttt{gzip}, \texttt{bzip2} or \texttt{lzma} to add compression) useful with FAT disks?
1497
-
\item Search the Internet to find out how to unpack \texttt{rar} or \texttt{arj} archives from command line.
1518
+
\item Why is even plain \texttt{tar} (without compression, it requires \texttt{gzip}, \texttt{bzip2}, \texttt{xz} or \texttt{lzma} to add compression) useful with FAT disks?
1519
+
\item Search the Internet to find out how to unpack \texttt{arj} archives from command line.
1498
1520
\end{enumerate}
1499
1521
\end{frame}
1500
1522
@@ -1535,13 +1557,13 @@ \subsection{Searching}
1535
1557
\item -\texttt{exec} runs some command with results (some operation, not just listing)
1536
1558
\begin{itemize}
1537
1559
\item All following arguments are argument of the command until \enquote{\texttt{;}} is encountered
1538
-
\item\texttt{\{\}} is replaced by the current file name being processed
1560
+
\item\texttt{'\{\}'} is replaced by the current file name being processed
1539
1561
\item Those constructs might require protection by escape (\enquote{\textbackslash}) or quotes not to be expanded by shell
1540
1562
\end{itemize}
1541
1563
\end{itemize}
1542
1564
\end{frame}
1543
1565
1544
-
\begin{frame}[fragile]{Find examples (apply some of them to the toy data) I}
1566
+
\begin{frame}[fragile]{Find examples I}{Apply some (or something similar) of them to the toy data}
1545
1567
\begin{bashcode}
1546
1568
# Find in /home/$USER/ all JPG files containing string "oxalis"
1547
1569
find /home/$USER/ -name "*oxalis*.jpg" -print
@@ -1555,19 +1577,30 @@ \subsection{Searching}
1555
1577
find ~/Documents -name "*.r" -print | xargs grep -nH DNA # Or
1556
1578
find ~/Documents -name "*.r" -exec grep -nH DNA '{}' \;
1557
1579
# How many directories are there in the books directory
0 commit comments