Skip to content

Commit 29ff782

Browse files
committed
Updates in command line usage.
1 parent 6b3a58d commit 29ff782

File tree

1 file changed

+99
-66
lines changed

1 file changed

+99
-66
lines changed

presentation/linux_bash_metacentrum_course.tex

Lines changed: 99 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737

3838
% Other packages
3939
\usepackage{multicol}
40-
\usepackage{tabularx}
40+
\usepackage{array}
4141

4242
% In-line higlighting
4343
\usepackage{soul}
@@ -1091,7 +1091,7 @@ \section{Command line}
10911091
\begin{frame}{Launching commands and scripts}
10921092
\begin{itemize}
10931093
\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
10951095
\item Getting help with \texttt{man} command
10961096
\begin{itemize}
10971097
\item \texttt{man somecommand}
@@ -1100,8 +1100,8 @@ \section{Command line}
11001100
\item Command \texttt{info} more advanced --- type \texttt{?} for help
11011101
\end{itemize}
11021102
\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
11051105
\begin{itemize}
11061106
\item If the script is is current directory, use \texttt{./script.sh} or full path
11071107
\end{itemize}
@@ -1114,9 +1114,9 @@ \section{Command line}
11141114
\begin{itemize}
11151115
\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)
11161116
\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)
11181118
\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)
11201120
\end{itemize}
11211121
\end{frame}
11221122

@@ -1172,7 +1172,7 @@ \section{Command line}
11721172

11731173
\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}
11741174
\begin{center}
1175-
\includegraphics[width=\textwidth-4.5cm]{terminals.png}
1175+
\includegraphics[height=6cm]{terminals.png}
11761176
\end{center}
11771177
\end{frame}
11781178

@@ -1197,8 +1197,7 @@ \subsection{Screen}
11971197
\end{itemize}
11981198
\vfill
11991199
\begin{bashcode}
1200-
# Start new tmux session
1201-
tmux
1200+
tmux # Start new tmux session
12021201
# Or name the new session (useful if there are more sessions)
12031202
tmux new -s SomeName
12041203
# Detach from the session by Ctrl+B, D
@@ -1214,6 +1213,7 @@ \subsection{Screen}
12141213
\begin{itemize}
12151214
\item Get help by \texttt{Ctrl+B, ?} (\texttt{Q} to quit)
12161215
\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
12171217
\end{itemize}
12181218
\end{frame}
12191219

@@ -1233,7 +1233,7 @@ \subsection{Screen}
12331233
\end{bashcode}
12341234
\vfill
12351235
\begin{itemize}
1236-
\item Toy server: user names \texttt{cu01} --- \texttt{cu20}
1236+
\item Toy server: user names \texttt{cu01}--\texttt{cu20}
12371237
\end{itemize}
12381238
\vfill
12391239
\begin{bashcode}
@@ -1286,22 +1286,22 @@ \subsection{BASH}
12861286
\end{itemize}
12871287
\end{frame}
12881288

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
12901290
\begin{itemize}
12911291
\item Arrows up and down list in the history of commands
12921292
\item List whole history by command \texttt{history}
12931293
\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
12941294
\item \texttt{TAB} --- list command and files starting by typed characters
12951295
\item \texttt{Home}/\texttt{End} --- go to beginning/end of the line
12961296
\item \texttt{Ctrl+L} --- clear screen (like \texttt{clear} command)
1297-
\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
12981298
\item \texttt{Ctrl+C} --- cancel running task
12991299
\item \texttt{Ctrl+D} --- log out (like commands \texttt{exit} or \texttt{logout})
13001300
\item \texttt{Ctrl+U} --- move text before cursor into clipboard
13011301
\item \texttt{Ctrl+K} --- move text after cursor into clipboard
13021302
\item \texttt{Ctrl+left/right arrow} --- skip words
13031303
\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
13051305
\end{itemize}
13061306
\end{frame}
13071307

@@ -1343,28 +1343,42 @@ \subsection{BASH}
13431343
\begin{bashcode}
13441344
# Define new alias
13451345
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
13521354
# Popular aliases
1355+
eval "$(dircolors -b)" # Make output of ls colored
13531356
alias ls="ls --color=auto" # Make output of ls colored
13541357
alias l="ls -la" # Long list (add details) with hidden files
1355-
# Popular settings in ~/.bashrc (influencing bash, not other shells)
13561358
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, ...
13591361
\end{bashcode}
13601362
\end{frame}
13611363

13621364
\begin{frame}[fragile]{Aliases and BASH settings II}
13631365
\begin{bashcode}
13641366
# Easier history listing
13651367
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
1371+
export GCC_COLORS='error=01;31:warning=01;35:note=01;36:caret=01;
1372+
32:locus=01:quote=01' # No spaces, single line
1373+
# Some applications read the EDITOR variable to determine your favourite
1374+
# text editor. Select e.g. nano, emacs, vim, ...
1375+
export EDITOR=nano
13671376
\end{bashcode}
1377+
\begin{itemize}
1378+
\item \texttt{$\sim$/.bashrc} can contain anything (technically it's just BASH script), functions, whatever needed
1379+
\item \texttt{$\sim$/.bashrc} commonly contain definitions of variables
1380+
\item Other shells than BASH (KSH, ZSH,~\ldots) have their own configuration files
1381+
\end{itemize}
13681382
\end{frame}
13691383
13701384
\subsection{Directories}
@@ -1387,16 +1401,29 @@ \subsection{Directories}
13871401
# Try parameters -l, -a, -1, -F, -h (with -l or -s), --help
13881402
rm -r # Recursive delete - remove also non-empty directories
13891403
mv from to # Move files/directories (also for renaming)
1404+
mv docs to/sub/directory/ # Move 'docs' to 'to/sub/directory/'
13901405
\end{bashcode}
13911406
\end{frame}
13921407
13931408
\begin{frame}[fragile]{Directories and files}
13941409
\begin{bashcode}
13951410
cp from to # Copy, -r (recursive, including subdirectories)
13961411
# -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
13971417
file somefile # Information about questioned file (what it is, ...)
13981418
xdg-open somefile # Open file by graphical application as in GUI
13991419
\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}
14001427
\end{frame}
14011428
14021429
\begin{frame}[allowframebreaks]{Tasks on the remote server}
@@ -1415,9 +1442,12 @@ \subsection{Directories}
14151442
\item Create other directory, see it and then remove (\texttt{rmdir}).
14161443
\item Can you access directories of another users? Why? If yes, what are your permissions there? Explain it.
14171444
\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.
14181446
\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})?
14191447
\item Practice moving between \texttt{/home/scripts\_data} and your home directory. Use \texttt{cd} and \texttt{TAB}.
14201448
\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.
14211451
\end{enumerate}
14221452
\end{frame}
14231453
@@ -1441,48 +1471,40 @@ \subsection{Directories}
14411471
14421472
\subsection{Archives}
14431473
1444-
\begin{frame}{Compressing files into archives}
1474+
\begin{frame}{Compressing and decompressing archives}
14451475
\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\\
1451-
*.tar.xz & tar cvf - file1 file2 | lzma > archive.tar.xz\\
1452-
*.gz & gzip file\\
1453-
*.bz2 & bzip2 file\\
1454-
*.xz & lzma file\\
1455-
*.zip & zip -r archive.zip file1 file2\\
1456-
*.rar & rar a archive.rar file1 file2
1476+
\begin{tabular}{m{2.25cm}m{6.3cm}m{5.3cm}}
1477+
\textbf{Archive} & \textbf{Compressing command} & \textbf{Decompressing command}\\
1478+
*.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
14571489
\end{tabular}
14581490
\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}
14631491
\end{frame}
14641492
14651493
\begin{frame}{Compressing and decompressing archives}
1466-
\begin{center}
1467-
\begin{tabular}{llll}
1468-
\textbf{Archive} & \textbf{Decompressing command}\\
1469-
*.tar & tar xvf archive.tar\\
1470-
*.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}
14791501
\hfill
1502+
\begin{center}
14801503
\includegraphics[height=1.75cm]{tar.png}
1481-
\begin{flushright}
1482-
\url{https://xkcd.com/1168/}
1483-
\end{flushright}
1484-
\hfill
14851504
\end{center}
1505+
\begin{flushright}
1506+
\url{https://xkcd.com/1168/}
1507+
\end{flushright}
14861508
\end{frame}
14871509
14881510
\begin{frame}{Tasks with archives}
@@ -1493,8 +1515,8 @@ \subsection{Archives}
14931515
\item Compare sizes of original files and compressed outputs.
14941516
\item Which compression tool seems to be the best? In terms of compressing ratio and time needed for compression.
14951517
\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.
14981520
\end{enumerate}
14991521
\end{frame}
15001522
@@ -1535,13 +1557,13 @@ \subsection{Searching}
15351557
\item -\texttt{exec} runs some command with results (some operation, not just listing)
15361558
\begin{itemize}
15371559
\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
15391561
\item Those constructs might require protection by escape (\enquote{\textbackslash}) or quotes not to be expanded by shell
15401562
\end{itemize}
15411563
\end{itemize}
15421564
\end{frame}
15431565
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}
15451567
\begin{bashcode}
15461568
# Find in /home/$USER/ all JPG files containing string "oxalis"
15471569
find /home/$USER/ -name "*oxalis*.jpg" -print
@@ -1555,19 +1577,30 @@ \subsection{Searching}
15551577
find ~/Documents -name "*.r" -print | xargs grep -nH DNA # Or
15561578
find ~/Documents -name "*.r" -exec grep -nH DNA '{}' \;
15571579
# How many directories are there in the books directory
1558-
find books/ -type d -print | wc -l # wc -l calculate lines
1580+
find books/ -type d -print | wc -l # wc -l calculates lines
15591581
# Change permissions of all files within "files" directory to 640
15601582
find files/ -type f -exec chmod 640 '{}' \;
1561-
# Find all executable files within current directory and list them
1562-
find . -executable -type f -print
15631583
\end{bashcode}
15641584
\end{frame}
15651585
1566-
\begin{frame}[fragile]{Find examples (apply some of them to the toy data) II}
1586+
\begin{frame}[fragile]{Find examples II}{Apply some (or something similar) of them to the toy data}
15671587
\begin{bashcode}
1568-
find doc/ -type d -empty -execdir rmdir '{}' \; # Delete empty dirs
1569-
man find # See another options. Much more...
1588+
# Find all executable files within current directory and list them
1589+
find . -executable -type f -print
1590+
# Delete empty directories within 'doc' directory
1591+
find doc/ -type d -empty -execdir rmdir '{}' \;
1592+
# Copy all *.sh files from /home to ~/scripts
1593+
find /home -name "*.sh" -exec cp '{}' ~/scripts/
1594+
# Search for file long_text.txt (exact name) in your home directory
1595+
find ~ -name long_text.txt -type f -print
1596+
# Find in current directory files from 1 to 100 MB
1597+
find . -type f -size +1M -size -100M
1598+
# See another options. Much more...
1599+
man find
15701600
\end{bashcode}
1601+
\begin{itemize}
1602+
\item \texttt{find} is extremely versatile and useful tool
1603+
\end{itemize}
15711604
\end{frame}
15721605
15731606
\begin{frame}{Searching tasks}

0 commit comments

Comments
 (0)