forked from floswald/ScPoProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-filework.qmd
283 lines (195 loc) Β· 5.37 KB
/
03-filework.qmd
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
---
title: "Working with Files and Directories"
format:
revealjs:
theme: _extensions/metropolis-theme/metropolis.scss
chalkboard: true
logo: /images/ScPo-logo.png
footer: ScPo Intro To Programming 2023
incremental: false
code-line-numbers: false
highlight-style: github
author: Florian Oswald and The Software Carpentry
---
## Intro
::: {.callout-note}
# Questions
- How can I create, copy, and delete files and directories?
- How can I edit files?
:::
::: {.callout-tip}
# Objectives
- Create a directory hierarchy that matches a given diagram.
- Create files in that hierarchy using an editor or by copying and renaming existing files.
- Delete, copy and move specified files and/or directories.
:::
---
## Creating Directories
* Let's create a directory `thesis` here:
```bash
$ cd ~/shell-lesson-data/exercise-data/writing
$ ls -F
```
this outputs:
```
haiku.txt LittleWomen.txt
```
* Use `mkdir` to create:
```bash
$ mkdir thesis
```
used like this, `thesis` is created in the current directory. While with the `-p` flag we created nested subdirectories:
```bash
$ mkdir -p ../project/data ../project/results
```
---
## Good File Names
::: {.callout-warning}
# Bad File Names
1. Don't use spaces. `north pacific gyre` is not a good one. Use `north-pacific-gyre` instead.
2. Don't begin with `-`.
3. Stick with letters, numbers, `.`, `-`, and `_`
:::
---
## Creating a Text File
* Let's go into the `thesis` directory and create a text file called `draft.txt`.
```bash
$ cd thesis
$ nano draft.txt
```
::: {.callout-note}
# TEXT Editor
`nano` is a super simple editor, and you can use it *only* to edit text files (That's normal for *text editors* π). You will probably switch to a more powerful editor later on (I recommend `VSCode`), but `nano` is a good starting point.
Notice that `^` key is the `Ctrl` key, so `^X` means `Ctrl + X`.
:::
---
## Filename Extensions
### Task
1. Go to your home directory: `cd`
2. create an *empty* file with the `touch` command:
```bash
$ # this is a comment, by the way
$ cd # so, going home.
$ touch new_doc.pdf # creating an empty file.
```
3. Open your file browser and double click on `new_doc.pdf`. What is going to happen?
---
* Ok, let's get rid of that file now.
* use the `rm` command (more later)
```bash
$ rm new_doc.pdf
```
* Caution: `rm` is forever gone.
* You can add `-i` *interactive* to be safe(r).
---
## Moving Files and Directories
* Let's go back to the `writing` directory
```bash
$ cd ~/shell-lesson-data/exercise-data/writing
```
* Let's *rename* `draft.txt` to `quotes.txt` with `mv`.
```bash
$ mv thesis/draft.txt thesis/quotes.txt
```
* Now let's actually *move* it into the current dir:
```bash
$ mv thesis/quotes.txt .
```
* Notice: `mv x y` means `x` is gone afterwards!
---
::: {.callout-caution}
# Challenge
Jamie placed `maltose.dat` and `sucrose.datfiles` in the `analyzed` folder by mistake. He wants to move those back to the `raw` folder now:
```bash
$ ls -F
analyzed/ raw/
$ ls -F analyzed
fructose.dat glucose.dat maltose.dat sucrose.dat
$ cd analyzed
```
What has to go in the blanks to achieve this?
```bash
$ mv sucrose.dat maltose.dat ____/____
```
:::
<details>
<summary>Show Solution</summary>
::: {.callout-note}
# Solution
```bash
$ mv sucrose.dat maltose.dat ../raw
```
:::
</details>
---
## Copying Files and Directories
* `cp x y` is similar to `mv x y`, but you keep `x`.
```bash
$ cp quotes.txt thesis/quotations.txt
$ ls quotes.txt thesis/quotations.txt
```
* the `-r` option means *recursively* and copies entire folders:
```bash
$ cp -r thesis thesis_backup
$ ls thesis thesis_backup
```
* Notice that `rm -r mydir` will delete everything inside the `mydir` folder!
---
## Using *Wildcards*
* the `*` character is a *wildcard*, i.e it matches all characters:
```bash
$ cd shell-lesson-data/exercise-data/
$ ls proteins/p*
proteins/pentane.pdb proteins/propane.pdb
```
---
## Reproducing a Folder Structure
Suppose we want to create the following structure on our computer:
```bash
2016-05-20/
βββ data
βββ processed
βββ raw
```
::: {.callout-caution}
# Challenge
Which sequence will achieve this result?
```
1.
$ mkdir 2016-05-20
$ mkdir 2016-05-20/data
$ mkdir 2016-05-20/data/processed
$ mkdir 2016-05-20/data/raw
```
```
2.
$ mkdir 2016-05-20/data/raw
$ mkdir 2016-05-20/data/processed
```
```
3.
$ mkdir -p 2016-05-20/data/raw
$ mkdir -p 2016-05-20/data/processed
```
:::
---
## Nice Trick
* Oh by the way.
* If you are on MacOS, try to this on the command line
```bash
$ open .
```
* Pretty handy!
---
::: {.callout-tip}
- `cp [old] [new]` copies a file.
- `mkdir [path]` creates a new directory.
- `mv [old] [new]` moves (renames) a file or directory.
- `rm [path]` removes (deletes) a file.
- `*` matches zero or more characters in a filename, so `*.txt` matches all files ending in `.txt`.
- `?` matches any single character in a filename, so `?.txt` matches `a.txt` but not `any.txt`.
- Use of the Control key may be described in many ways, including `Ctrl-X`, `Control-X`, and `^X`.
- The shell does not have a trash bin: once something is deleted, it's really gone.
- Most files' names are `something.extension`. The extension isn't required, and doesn't guarantee anything, but is normally used to indicate the type of data in the file.
- Depending on the type of work you do, you may need a more powerful text editor than Nano.
:::