-
Notifications
You must be signed in to change notification settings - Fork 1
/
01-files.tex
286 lines (261 loc) · 7.34 KB
/
01-files.tex
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
% !TeX root = bash.tex
\section{I. File Tree}
\begin{frame}[fragile]
\frametitle{Files}
Each of these is a different \textbf{file}:
\begin{itemize}
\item \tt{a}
\item \tt{.a} (Hidden)
\item \tt{a.txt}
\item \tt{A.txt}
\item \tt{A.TXT}
\end{itemize}
\begin{block}{Note}
The dot and suffix are part of the filename.
\newline \newline
\textbf{Avoid spaces and special characters} (except \verb|._-|).
If you have to, surround filename in quotes:
\tt{`Lab Report (3) final FINAL-1.docx'}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{\tt{cat}: Printing a file}
Open a bash terminal inside \tt{01-files/}, then:
\begin{lstlisting}[language=bash]
$ cat a
$ cat .a
$ cat a.txt
\end{lstlisting}
\begin{block}{Explanation}
\tt{cat} is short for ``concatenate'' (to join together) but it's
mostly used to print files.
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{\tt{cp, mv, rm}: Relocating a file}
Try this inside \tt{01-files/}:
\begin{lstlisting}[language=bash]
$ ls
$ cp a b
$ ls
$ mv a.txt b.txt
$ ls
$ rm b
$ ls
\end{lstlisting}
\begin{block}{Explanation}
\begin{itemize}
\item \tt{ls} lists files
\item \textbf{copy} \tt{a} into a file called \tt{b}
\item \textbf{move} \tt{a.txt} into a file called \tt{b.txt}
\item \textbf{remove} \tt{b}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{\tt{cp, mv}: Overwriting and renaming}
When the destination does not exist, \tt{cp} and \tt{mv} simply create
that file. \textbf{Otherwise, it is destroyed and overwritten.}
\newline \newline
Try this inside \tt{01-files/}:
\begin{lstlisting}[language=bash]
$ cp a b # creates b
$ cat a
$ cp a a.txt # overwrites a.txt
$ cat a.txt
\end{lstlisting}
Renaming a file in bash works like so:
\begin{lstlisting}[language=bash]
$ mv b newb
\end{lstlisting}
\end{frame}
\begin{frame}
\frametitle{Directories}
Each of these is a \textbf{directory} (``dir'' for short):
\begin{itemize}
\item \tt{01-files/}
\item \tt{01-files/c/}
\item \tt{01-files/.c/} (Hidden dir)
\end{itemize}
\begin{block}{Convention}
For clarity, we add a slash (\tt{/}) to the end of a directory in the
slides. However, in reality it often makes no difference.
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{\tt{cd, pwd}: Changing directory}
Try this inside \tt{01-files/}:
\begin{lstlisting}[language=bash]
$ cd c/
$ pwd
$ cd ../
$ pwd
\end{lstlisting}
\begin{block}{Explanation}
\begin{itemize}
\item \tt{cd}: ``change directory''
\item \tt{pwd}: ``print working directory''
\item \tt{../} means ``parent directory''
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{\tt{ls}: Listing directories}
Try this inside \tt{01-files/}:
\begin{lstlisting}[language=bash]
$ ls
$ ls -a
$ ls -l
$ ls -la
$ ls c/
\end{lstlisting}
\begin{block}{Explanation}
\begin{itemize}
\item \tt{ls}: ``list''
\item \tt{-a} is short for \tt{--all}
\item \tt{-l} enables long listing format
\item \tt{-la} = \tt{-l} + \tt{-a}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{\tt{mkdir, rm}: Creating and deleting directories}
Try this inside \tt{01-files/}:
\begin{lstlisting}[language=bash]
$ mkdir dir/
$ rm -r dir/
$ mkdir -p dir/subdir/
$ ls dir/
\end{lstlisting}
\begin{block}{Explanation}
\begin{itemize}
\item \tt{mkdir}: ``make directory''
\item \tt{-r} is short for \tt{--recursive}
\item \tt{-p} is short for \tt{--parents}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}[fragile]
\frametitle{\tt{cp, mv}: Into and out of directories}
Try this inside \tt{01-files/}:
\begin{lstlisting}[language=bash]
$ cp a dir/
$ mv b.txt dir/
$ mv dir/a b.txt
\end{lstlisting}
\begin{block}{Explanation}
\begin{itemize}
\item Copy \tt{a} into \tt{dir/}
\item Move \tt{b.txt} into \tt{dir/}
\item Move \tt{dir/a} back into \tt{b.txt}
\end{itemize}
\end{block}
\end{frame}
\begin{frame}
\frametitle{File tree}
Think of any directory as a tree.
\begin{figure}[h]
\centering
\begin{tikzpicture}
\tikzstyle{every node}=[fill=blue!20,rounded corners]
\tikzstyle{edge from parent}=[blue!50,thick,draw]
\node {01-files/}[edge from parent fork down]
child {node {a.txt}}
child {
node {dir/}
child {node {subdir/}}
child {node {b.txt}}
};
\end{tikzpicture}
\end{figure}
\end{frame}
\begin{frame}
\frametitle{Paths}
File $\cup$ directory = \textbf{path}.
\footnote{At least in the scope of this workshop.}
\newline \newline
No paths under the same directory can bear the same name.
These \textbf{cannot} coexist:
\begin{itemize}
\item \tt{01-files/data/}, a directory
\item \tt{01-files/data}, a regular file
\end{itemize}
\end{frame}
\begin{frame}
\frametitle{Absolute \& relative paths}
\begin{itemize}
\item Paths beginning with \tt{/} are absolute: \tt{/usr/bin/cat}
\item Otherwise it is relative: \tt{01-files/}
\end{itemize}
If you know where you are, you can convert a relative path to an absolute one.
\begin{example}
Your location: \tt{/home/you/} \newline
Relative path: \tt{bash-workshop/01-files/} \newline
Absolute path: \tt{/home/you/bash-workshop/01-files/}
\end{example}
\end{frame}
\begin{frame}
\frametitle{Wildcard}
\tt{*} is a character to match any number of (including zero) characters.
\begin{block}{Exception}
Hidden paths will remain hidden unless you explicitly specify the dot:
\tt{.*}
\end{block}
\begin{example}
\begin{table}
\centering
\begin{tabular}{lcccc}
& \tt{a/} & \tt{b/} & \tt{a-copy.txt} & \tt{b.txt} \\ \hline
\tt{*} & \checkmark & \checkmark & \checkmark & \checkmark \\
\tt{a*} & \checkmark & & \checkmark & \\
\tt{*.txt} & & & \checkmark & \checkmark \\
\end{tabular}
\end{table}
\end{example}
\scriptsize{Technically it's called a glob pattern but who cares. Also there are other
weird symbols like \tt{?} or \tt{[]} but I swear \tt{*} is most of
us will ever use.}
\end{frame}
\begin{frame}
\frametitle{\tt{.} and \tt{..}}
Inside every dir\footnote{Except \tt{/}} there are two special dirs:
\begin{itemize}
\item \tt{./} — current dir
\item \tt{../} — parent dir
\end{itemize}
You can use them in relative paths.
\begin{example}
Your location: \tt{/home/you/} \newline
Relative path: \tt{../friend/bash-workshop/01-files/} \newline
(Note that \tt{/home/you/../friend/} is just \tt{/home/friend/}) \newline
Absolute path: \tt{/home/friend/bash-workshop/01-files/}
\end{example}
\end{frame}
\begin{frame}
\frametitle{Challenge}
Inside \tt{01-files/}:
\begin{itemize}
\item Enter \tt{challenge/}
\item Create \tt{backup/}
\item Copy \tt{a.txt} into \tt{dir/}
\item Move \tt{dir/} into \tt{backup/}
\item Verify using \tt{ls}
\item Delete \tt{backup/}
\item Go to the next section's directory
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{Solution}
\begin{lstlisting}[language=bash]
$ cd challenge/
$ mkdir backup/
$ cp a.txt dir/
$ mv dir/ backup/
$ ls
# Output: backup/ a.txt
$ ls backup/dir/
# Output: a.txt
$ rm -r backup/
$ cd ../../02-cli/
\end{lstlisting}
\end{frame}