-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworking_with_files_and_directories.html
More file actions
492 lines (407 loc) · 30.4 KB
/
working_with_files_and_directories.html
File metadata and controls
492 lines (407 loc) · 30.4 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="last-modified" content="2021-06-28 09:34:41 +0000">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- meta "search-domain" used for google site search function google_search() -->
<meta name="search-domain" value="https://swcarpentry.github.io/shell-novice">
<link rel="stylesheet" type="text/css" href="https://swcarpentry.github.io/shell-novice/assets/css/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="https://swcarpentry.github.io/shell-novice/assets/css/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="https://swcarpentry.github.io/shell-novice/assets/css/lesson.css" />
<link rel="stylesheet" type="text/css" href="https://swcarpentry.github.io/shell-novice/assets/css/syntax.css" />
<!-- Favicons for everyone -->
<link rel="apple-touch-icon-precomposed" sizes="57x57" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-57x57.png" />
<link rel="apple-touch-icon-precomposed" sizes="114x114" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-114x114.png" />
<link rel="apple-touch-icon-precomposed" sizes="72x72" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-72x72.png" />
<link rel="apple-touch-icon-precomposed" sizes="144x144" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-144x144.png" />
<link rel="apple-touch-icon-precomposed" sizes="60x60" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-60x60.png" />
<link rel="apple-touch-icon-precomposed" sizes="120x120" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-120x120.png" />
<link rel="apple-touch-icon-precomposed" sizes="76x76" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-76x76.png" />
<link rel="apple-touch-icon-precomposed" sizes="152x152" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/apple-touch-icon-152x152.png" />
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-196x196.png" sizes="196x196" />
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-96x96.png" sizes="96x96" />
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-32x32.png" sizes="32x32" />
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-16x16.png" sizes="16x16" />
<link rel="icon" type="image/png" href="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/favicon-128.png" sizes="128x128" />
<meta name="application-name" content="Software Carpentry - The Unix Shell"/>
<meta name="msapplication-TileColor" content="#FFFFFF" />
<meta name="msapplication-TileImage" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-144x144.png" />
<meta name="msapplication-square70x70logo" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-70x70.png" />
<meta name="msapplication-square150x150logo" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-150x150.png" />
<meta name="msapplication-wide310x150logo" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-310x150.png" />
<meta name="msapplication-square310x310logo" content="https://swcarpentry.github.io/shell-novice/assets/favicons/swc/mstile-310x310.png" />
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
<title>
Working With Files and Directories – The Unix Shell
</title>
</head>
<body>
<div class="container">
<h1 class="maintitle">Working With Files and Directories - Exercises</h1>
<article>
<blockquote class="challenge">
<h2 id="creating-files-a-different-way">Creating Files a Different Way</h2>
<p>We have seen how to create text files using the <code class="language-plaintext highlighter-rouge">nano</code> editor.
Now, try the following command:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">touch </span>my_file.txt
</code></pre></div> </div>
<ol>
<li>
<p>What did the <code class="language-plaintext highlighter-rouge">touch</code> command do?
When you look at your current directory using the GUI file explorer,
does the file show up?</p>
</li>
<li>
<p>Use <code class="language-plaintext highlighter-rouge">ls -l</code> to inspect the files. How large is <code class="language-plaintext highlighter-rouge">my_file.txt</code>?</p>
</li>
<li>
<p>When might you want to create a file this way?</p>
</li>
</ol>
<blockquote class="solution">
<h2 id="solution">Solution</h2>
<ol>
<li>
<p>The <code class="language-plaintext highlighter-rouge">touch</code> command generates a new file called <code class="language-plaintext highlighter-rouge">my_file.txt</code> in
your current directory. You
can observe this newly generated file by typing <code class="language-plaintext highlighter-rouge">ls</code> at the
command line prompt. <code class="language-plaintext highlighter-rouge">my_file.txt</code> can also be viewed in your
GUI file explorer.</p>
</li>
<li>
<p>When you inspect the file with <code class="language-plaintext highlighter-rouge">ls -l</code>, note that the size of
<code class="language-plaintext highlighter-rouge">my_file.txt</code> is 0 bytes. In other words, it contains no data.
If you open <code class="language-plaintext highlighter-rouge">my_file.txt</code> using your text editor it is blank.</p>
</li>
<li>
<p>Some programs do not generate output files themselves, but
instead require that empty files have already been generated.
When the program is run, it searches for an existing file to
populate with its output. The touch command allows you to
efficiently generate a blank text file to be used by such
programs.</p>
</li>
</ol>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="moving-files-to-a-new-folder">Moving Files to a new folder</h2>
<p>After running the following commands,
Jamie realizes that she put the files <code class="language-plaintext highlighter-rouge">sucrose.dat</code> and <code class="language-plaintext highlighter-rouge">maltose.dat</code> into the wrong folder.
The files should have been placed in the <code class="language-plaintext highlighter-rouge">raw</code> folder.</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">ls</span> <span class="nt">-F</span>
analyzed/ raw/
<span class="nv">$ </span><span class="nb">ls</span> <span class="nt">-F</span> analyzed
fructose.dat glucose.dat maltose.dat sucrose.dat
<span class="nv">$ </span><span class="nb">cd </span>analyzed
</code></pre></div> </div>
<p>Fill in the blanks to move these files to the <code class="language-plaintext highlighter-rouge">raw/</code> folder
(i.e. the one she forgot to put them in)</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mv </span>sucrose.dat maltose.dat ____/____
</code></pre></div> </div>
<blockquote class="solution">
<h2 id="solution-1">Solution</h2>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mv </span>sucrose.dat maltose.dat ../raw
</code></pre></div> </div>
<p>Recall that <code class="language-plaintext highlighter-rouge">..</code> refers to the parent directory (i.e. one above the current directory)
and that <code class="language-plaintext highlighter-rouge">.</code> refers to the current directory.</p>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="renaming-files">Renaming Files</h2>
<p>Suppose that you created a plain-text file in your current directory to contain a list of the
statistical tests you will need to do to analyze your data, and named it: <code class="language-plaintext highlighter-rouge">statstics.txt</code></p>
<p>After creating and saving this file you realize you misspelled the filename! You want to
correct the mistake, which of the following commands could you use to do so?</p>
<ol>
<li><code class="language-plaintext highlighter-rouge">cp statstics.txt statistics.txt</code></li>
<li><code class="language-plaintext highlighter-rouge">mv statstics.txt statistics.txt</code></li>
<li><code class="language-plaintext highlighter-rouge">mv statstics.txt .</code></li>
<li><code class="language-plaintext highlighter-rouge">cp statstics.txt .</code></li>
</ol>
<blockquote class="solution">
<h2 id="solution-2">Solution</h2>
<ol>
<li>No. While this would create a file with the correct name, the incorrectly named file still exists in the directory
and would need to be deleted.</li>
<li>Yes, this would work to rename the file.</li>
<li>No, the period(.) indicates where to move the file, but does not provide a new file name; identical file names
cannot be created.</li>
<li>No, the period(.) indicates where to copy the file, but does not provide a new file name; identical file names
cannot be created.</li>
</ol>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="moving-and-copying">Moving and Copying</h2>
<p>What is the output of the closing <code class="language-plaintext highlighter-rouge">ls</code> command in the sequence shown below?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">pwd</span>
</code></pre></div> </div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>/Users/jamie/data
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">ls</span>
</code></pre></div> </div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>proteins.dat
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mkdir </span>recombined
<span class="nv">$ </span><span class="nb">mv </span>proteins.dat recombined/
<span class="nv">$ </span><span class="nb">cp </span>recombined/proteins.dat ../proteins-saved.dat
<span class="nv">$ </span><span class="nb">ls</span>
</code></pre></div> </div>
<ol>
<li><code class="language-plaintext highlighter-rouge">proteins-saved.dat recombined</code></li>
<li><code class="language-plaintext highlighter-rouge">recombined</code></li>
<li><code class="language-plaintext highlighter-rouge">proteins.dat recombined</code></li>
<li><code class="language-plaintext highlighter-rouge">proteins-saved.dat</code></li>
</ol>
<blockquote class="solution">
<h2 id="solution-3">Solution</h2>
<p>We start in the <code class="language-plaintext highlighter-rouge">/Users/jamie/data</code> directory, and create a new folder called <code class="language-plaintext highlighter-rouge">recombined</code>.
The second line moves (<code class="language-plaintext highlighter-rouge">mv</code>) the file <code class="language-plaintext highlighter-rouge">proteins.dat</code> to the new folder (<code class="language-plaintext highlighter-rouge">recombined</code>).
The third line makes a copy of the file we just moved. The tricky part here is where the file was
copied to. Recall that <code class="language-plaintext highlighter-rouge">..</code> means ‘go up a level’, so the copied file is now in <code class="language-plaintext highlighter-rouge">/Users/jamie</code>.
Notice that <code class="language-plaintext highlighter-rouge">..</code> is interpreted with respect to the current working
directory, <strong>not</strong> with respect to the location of the file being copied.
So, the only thing that will show using ls (in <code class="language-plaintext highlighter-rouge">/Users/jamie/data</code>) is the recombined folder.</p>
<ol>
<li>No, see explanation above. <code class="language-plaintext highlighter-rouge">proteins-saved.dat</code> is located at <code class="language-plaintext highlighter-rouge">/Users/jamie</code></li>
<li>Yes</li>
<li>No, see explanation above. <code class="language-plaintext highlighter-rouge">proteins.dat</code> is located at <code class="language-plaintext highlighter-rouge">/Users/jamie/data/recombined</code></li>
<li>No, see explanation above. <code class="language-plaintext highlighter-rouge">proteins-saved.dat</code> is located at <code class="language-plaintext highlighter-rouge">/Users/jamie</code></li>
</ol>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="using-rm-safely">Using <code class="language-plaintext highlighter-rouge">rm</code> Safely</h2>
<p>What happens when we execute <code class="language-plaintext highlighter-rouge">rm -i thesis_backup/quotations.txt</code>?
Why would we want this protection when using <code class="language-plaintext highlighter-rouge">rm</code>?</p>
<blockquote class="solution">
<h2 id="solution-4">Solution</h2>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">rm</span>: remove regular file <span class="s1">'thesis_backup/quotations.txt'</span>? y
</code></pre></div> </div>
<p>The <code class="language-plaintext highlighter-rouge">-i</code> option will prompt before (every) removal (use <kbd>Y</kbd> to confirm deletion or <kbd>N</kbd> to keep the file).
The Unix shell doesn’t have a trash bin, so all the files removed will disappear forever.
By using the <code class="language-plaintext highlighter-rouge">-i</code> option, we have the chance to check that we are deleting only the files that we want to remove.</p>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="copy-with-multiple-filenames">Copy with Multiple Filenames</h2>
<p>For this exercise, you can test the commands in the <code class="language-plaintext highlighter-rouge">shell-lesson-data/data</code> directory.</p>
<p>In the example below, what does <code class="language-plaintext highlighter-rouge">cp</code> do when given several filenames and a directory name?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mkdir </span>backup
<span class="nv">$ </span><span class="nb">cp </span>amino-acids.txt animals.txt backup/
</code></pre></div> </div>
<p>In the example below, what does <code class="language-plaintext highlighter-rouge">cp</code> do when given three or more file names?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">ls</span> <span class="nt">-F</span>
</code></pre></div> </div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>amino-acids.txt animals.txt backup/ elements/ morse.txt pdb/ planets.txt salmon.txt sunspot.txt
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">cp </span>amino-acids.txt animals.txt morse.txt
</code></pre></div> </div>
<blockquote class="solution">
<h2 id="solution-5">Solution</h2>
<p>If given more than one file name followed by a directory name (i.e. the destination directory must
be the last argument), <code class="language-plaintext highlighter-rouge">cp</code> copies the files to the named directory.</p>
<p>If given three file names, <code class="language-plaintext highlighter-rouge">cp</code> throws an error such as the one below, because it is expecting a directory
name as the last argument.</p>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>cp: target ‘morse.txt’ is not a directory
</code></pre></div> </div>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="list-filenames-matching-a-pattern">List filenames matching a pattern</h2>
<p>When run in the <code class="language-plaintext highlighter-rouge">molecules</code> directory, which <code class="language-plaintext highlighter-rouge">ls</code> command(s) will
produce this output?</p>
<p><code class="language-plaintext highlighter-rouge">ethane.pdb methane.pdb</code></p>
<ol>
<li><code class="language-plaintext highlighter-rouge">ls *t*ane.pdb</code></li>
<li><code class="language-plaintext highlighter-rouge">ls *t?ne.*</code></li>
<li><code class="language-plaintext highlighter-rouge">ls *t??ne.pdb</code></li>
<li><code class="language-plaintext highlighter-rouge">ls ethane.*</code></li>
</ol>
<blockquote class="solution">
<h2 id="solution-6">Solution</h2>
<p>The solution is <code class="language-plaintext highlighter-rouge">3.</code></p>
<p><code class="language-plaintext highlighter-rouge">1.</code> shows all files whose names contain zero or more characters (<code class="language-plaintext highlighter-rouge">*</code>) followed by the letter <code class="language-plaintext highlighter-rouge">t</code>, then zero or more characters (<code class="language-plaintext highlighter-rouge">*</code>) followed by <code class="language-plaintext highlighter-rouge">ane.pdb</code>. This gives <code class="language-plaintext highlighter-rouge">ethane.pdb methane.pdb octane.pdb pentane.pdb</code>.</p>
<p><code class="language-plaintext highlighter-rouge">2.</code> shows all files whose names start with zero or more characters (<code class="language-plaintext highlighter-rouge">*</code>) followed by the letter <code class="language-plaintext highlighter-rouge">t</code>, then a single character (<code class="language-plaintext highlighter-rouge">?</code>), then <code class="language-plaintext highlighter-rouge">ne.</code> followed by zero or more characters (<code class="language-plaintext highlighter-rouge">*</code>). This will give us <code class="language-plaintext highlighter-rouge">octane.pdb</code> and <code class="language-plaintext highlighter-rouge">pentane.pdb</code> but doesn’t match anything which ends in <code class="language-plaintext highlighter-rouge">thane.pdb</code>.</p>
<p><code class="language-plaintext highlighter-rouge">3.</code> fixes the problems of option 2 by matching two characters (<code class="language-plaintext highlighter-rouge">??</code>) between <code class="language-plaintext highlighter-rouge">t</code> and <code class="language-plaintext highlighter-rouge">ne</code>. This is the solution.</p>
<p><code class="language-plaintext highlighter-rouge">4.</code> only shows files starting with <code class="language-plaintext highlighter-rouge">ethane.</code>.</p>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="more-on-wildcards">More on Wildcards</h2>
<p>Sam has a directory containing calibration data, datasets, and descriptions of
the datasets:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">.</span>
├── 2015-10-23-calibration.txt
├── 2015-10-23-dataset1.txt
├── 2015-10-23-dataset2.txt
├── 2015-10-23-dataset_overview.txt
├── 2015-10-26-calibration.txt
├── 2015-10-26-dataset1.txt
├── 2015-10-26-dataset2.txt
├── 2015-10-26-dataset_overview.txt
├── 2015-11-23-calibration.txt
├── 2015-11-23-dataset1.txt
├── 2015-11-23-dataset2.txt
├── 2015-11-23-dataset_overview.txt
├── backup
│ ├── calibration
│ └── datasets
└── send_to_bob
├── all_datasets_created_on_a_23rd
└── all_november_files
</code></pre></div> </div>
<p>Before heading off to another field trip, she wants to back up her data and
send some datasets to her colleague Bob. Sam uses the following commands
to get the job done:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">cp</span> <span class="k">*</span>dataset<span class="k">*</span> backup/datasets
<span class="nv">$ </span><span class="nb">cp </span>____calibration____ backup/calibration
<span class="nv">$ </span><span class="nb">cp </span>2015-____-____ send_to_bob/all_november_files/
<span class="nv">$ </span><span class="nb">cp </span>____ send_to_bob/all_datasets_created_on_a_23rd/
</code></pre></div> </div>
<p>Help Sam by filling in the blanks.</p>
<p>The resulting directory structure should look like this</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">.</span>
├── 2015-10-23-calibration.txt
├── 2015-10-23-dataset1.txt
├── 2015-10-23-dataset2.txt
├── 2015-10-23-dataset_overview.txt
├── 2015-10-26-calibration.txt
├── 2015-10-26-dataset1.txt
├── 2015-10-26-dataset2.txt
├── 2015-10-26-dataset_overview.txt
├── 2015-11-23-calibration.txt
├── 2015-11-23-dataset1.txt
├── 2015-11-23-dataset2.txt
├── 2015-11-23-dataset_overview.txt
├── backup
│ ├── calibration
│ │ ├── 2015-10-23-calibration.txt
│ │ ├── 2015-10-26-calibration.txt
│ │ └── 2015-11-23-calibration.txt
│ └── datasets
│ ├── 2015-10-23-dataset1.txt
│ ├── 2015-10-23-dataset2.txt
│ ├── 2015-10-23-dataset_overview.txt
│ ├── 2015-10-26-dataset1.txt
│ ├── 2015-10-26-dataset2.txt
│ ├── 2015-10-26-dataset_overview.txt
│ ├── 2015-11-23-dataset1.txt
│ ├── 2015-11-23-dataset2.txt
│ └── 2015-11-23-dataset_overview.txt
└── send_to_bob
├── all_datasets_created_on_a_23rd
│ ├── 2015-10-23-dataset1.txt
│ ├── 2015-10-23-dataset2.txt
│ ├── 2015-10-23-dataset_overview.txt
│ ├── 2015-11-23-dataset1.txt
│ ├── 2015-11-23-dataset2.txt
│ └── 2015-11-23-dataset_overview.txt
└── all_november_files
├── 2015-11-23-calibration.txt
├── 2015-11-23-dataset1.txt
├── 2015-11-23-dataset2.txt
└── 2015-11-23-dataset_overview.txt
</code></pre></div> </div>
<blockquote class="solution">
<h2 id="solution-7">Solution</h2>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">cp</span> <span class="k">*</span>calibration.txt backup/calibration
<span class="nv">$ </span><span class="nb">cp </span>2015-11-<span class="k">*</span> send_to_bob/all_november_files/
<span class="nv">$ </span><span class="nb">cp</span> <span class="k">*</span><span class="nt">-23-dataset</span><span class="k">*</span> send_to_bob/all_datasets_created_on_a_23rd/
</code></pre></div> </div>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="organizing-directories-and-files">Organizing Directories and Files</h2>
<p>Jamie is working on a project and she sees that her files aren’t very well
organized:</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">ls</span> <span class="nt">-F</span>
</code></pre></div> </div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>analyzed/ fructose.dat raw/ sucrose.dat
</code></pre></div> </div>
<p>The <code class="language-plaintext highlighter-rouge">fructose.dat</code> and <code class="language-plaintext highlighter-rouge">sucrose.dat</code> files contain output from her data
analysis. What command(s) covered in this lesson does she need to run so that the commands below will
produce the output shown?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">ls</span> <span class="nt">-F</span>
</code></pre></div> </div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>analyzed/ raw/
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">ls </span>analyzed
</code></pre></div> </div>
<div class="language-plaintext output highlighter-rouge"><div class="highlight"><pre class="highlight"><code>fructose.dat sucrose.dat
</code></pre></div> </div>
<blockquote class="solution">
<h2 id="solution-8">Solution</h2>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nb">mv</span> <span class="k">*</span>.dat analyzed
</code></pre></div> </div>
<p>Jamie needs to move her files <code class="language-plaintext highlighter-rouge">fructose.dat</code> and <code class="language-plaintext highlighter-rouge">sucrose.dat</code> to the <code class="language-plaintext highlighter-rouge">analyzed</code> directory.
The shell will expand *.dat to match all .dat files in the current directory.
The <code class="language-plaintext highlighter-rouge">mv</code> command then moves the list of .dat files to the ‘analyzed’ directory.</p>
</blockquote>
</blockquote>
<blockquote class="challenge">
<h2 id="reproduce-a-folder-structure">Reproduce a folder structure</h2>
<p>You’re starting a new experiment and would like to duplicate the directory
structure from your previous experiment so you can add new data.</p>
<p>Assume that the previous experiment is in a folder called ‘2016-05-18’,
which contains a <code class="language-plaintext highlighter-rouge">data</code> folder that in turn contains folders named <code class="language-plaintext highlighter-rouge">raw</code> and
<code class="language-plaintext highlighter-rouge">processed</code> that contain data files. The goal is to copy the folder structure
of the <code class="language-plaintext highlighter-rouge">2016-05-18-data</code> folder into a folder called <code class="language-plaintext highlighter-rouge">2016-05-20</code>
so that your final directory structure looks like this:</p>
<div class="language-plaintext highlighter-rouge"><div class="highlight"><pre class="highlight"><code>2016-05-20/
└── data
├── processed
└── raw
</code></pre></div> </div>
<p>Which of the following set of commands would achieve this objective?
What would the other commands do?</p>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mkdir </span>2016-05-20
<span class="nv">$ </span><span class="nb">mkdir </span>2016-05-20/data
<span class="nv">$ </span><span class="nb">mkdir </span>2016-05-20/data/processed
<span class="nv">$ </span><span class="nb">mkdir </span>2016-05-20/data/raw
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mkdir </span>2016-05-20
<span class="nv">$ </span><span class="nb">cd </span>2016-05-20
<span class="nv">$ </span><span class="nb">mkdir </span>data
<span class="nv">$ </span><span class="nb">cd </span>data
<span class="nv">$ </span><span class="nb">mkdir </span>raw processed
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mkdir </span>2016-05-20/data/raw
<span class="nv">$ </span><span class="nb">mkdir </span>2016-05-20/data/processed
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mkdir</span> <span class="nt">-p</span> 2016-05-20/data/raw
<span class="nv">$ </span><span class="nb">mkdir</span> <span class="nt">-p</span> 2016-05-20/data/processed
</code></pre></div> </div>
<div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nv">$ </span><span class="nb">mkdir </span>2016-05-20
<span class="nv">$ </span><span class="nb">cd </span>2016-05-20
<span class="nv">$ </span><span class="nb">mkdir </span>data
<span class="nv">$ </span><span class="nb">mkdir </span>raw processed
</code></pre></div> </div>
<blockquote class="solution">
<h2 id="solution-9">Solution</h2>
<p>The first two sets of commands achieve this objective.
The first set uses relative paths to create the top-level directory before
the subdirectories.</p>
<p>The third set of commands will give an error because the default behavior of <code class="language-plaintext highlighter-rouge">mkdir</code> won’t create a subdirectory
of a non-existent directory: the intermediate level folders must be created first.</p>
<p>The fourth set of commands achieve this objective. Remember, the <code class="language-plaintext highlighter-rouge">-p</code> option, followed by a path of one or more
directories, will cause <code class="language-plaintext highlighter-rouge">mkdir</code> to create any intermediate subdirectories as required.</p>
<p>The final set of commands generates the ‘raw’ and ‘processed’ directories at the same level
as the ‘data’ directory.</p>
</blockquote>
</blockquote>
</article>
</div>
<script src="https://swcarpentry.github.io/shell-novice/assets/js/jquery.min.js"></script>
<script src="https://swcarpentry.github.io/shell-novice/assets/js/bootstrap.min.js"></script>
<script src="https://swcarpentry.github.io/shell-novice/assets/js/lesson.js"></script>
</body>
</html>