pwd
/Users/kjhealy/Documents/courses/mptc
We start in the home directory of the project that houses all the materials for this seminar:
R
README.md
README.qmd
README_files
_extensions
_freeze
_quarto.yml
_site
_targets
_targets.R
_variables.yml
about
assets
assignment
content
data
deploy.sh
example
files
html
index.qmd
mptc.Rproj
renv
renv.lock
renv.lock.orig
schedule
site_libs
slides
staging
syllabus
Make a temporary directory:
touch
and cat
Use touch
to create an empty file:
List it:
Echo some text into some files:
total 16
-rw-r--r--@ 1 kjhealy staff 6 Oct 23 09:17 01-my-file.txt
-rw-r--r--@ 1 kjhealy staff 6 Oct 23 09:17 02-my-file.txt
-rw-r--r--@ 1 kjhealy staff 0 Oct 23 09:17 my-file.txt
Cat the files
Cat the files into a new file:
Append to a file with >>
instead of >
:
Result:
You can do this:
01-my-file.txt
02-my-file.txt
03-my-file.txt
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
h.txt
i.txt
j.txt
k.txt
l.txt
m.txt
my-file.txt
n.txt
o.txt
p.txt
q.txt
r.txt
s.txt
t.txt
u.txt
v.txt
w.txt
x.txt
y.txt
z.txt
Or this:
01-my-file.txt
02-my-file.txt
03-my-file.txt
A0.txt
A1.txt
A10.txt
A2.txt
A3.txt
A4.txt
A5.txt
A6.txt
A7.txt
A8.txt
A9.txt
B0.txt
B1.txt
B10.txt
B2.txt
B3.txt
B4.txt
B5.txt
B6.txt
B7.txt
B8.txt
B9.txt
C0.txt
C1.txt
C10.txt
C2.txt
C3.txt
C4.txt
C5.txt
C6.txt
C7.txt
C8.txt
C9.txt
D0.txt
D1.txt
D10.txt
D2.txt
D3.txt
D4.txt
D5.txt
D6.txt
D7.txt
D8.txt
D9.txt
E0.txt
E1.txt
E10.txt
E2.txt
E3.txt
E4.txt
E5.txt
E6.txt
E7.txt
E8.txt
E9.txt
F0.txt
F1.txt
F10.txt
F2.txt
F3.txt
F4.txt
F5.txt
F6.txt
F7.txt
F8.txt
F9.txt
G0.txt
G1.txt
G10.txt
G2.txt
G3.txt
G4.txt
G5.txt
G6.txt
G7.txt
G8.txt
G9.txt
H0.txt
H1.txt
H10.txt
H2.txt
H3.txt
H4.txt
H5.txt
H6.txt
H7.txt
H8.txt
H9.txt
I0.txt
I1.txt
I10.txt
I2.txt
I3.txt
I4.txt
I5.txt
I6.txt
I7.txt
I8.txt
I9.txt
J0.txt
J1.txt
J10.txt
J2.txt
J3.txt
J4.txt
J5.txt
J6.txt
J7.txt
J8.txt
J9.txt
K0.txt
K1.txt
K10.txt
K2.txt
K3.txt
K4.txt
K5.txt
K6.txt
K7.txt
K8.txt
K9.txt
L0.txt
L1.txt
L10.txt
L2.txt
L3.txt
L4.txt
L5.txt
L6.txt
L7.txt
L8.txt
L9.txt
M0.txt
M1.txt
M10.txt
M2.txt
M3.txt
M4.txt
M5.txt
M6.txt
M7.txt
M8.txt
M9.txt
N0.txt
N1.txt
N10.txt
N2.txt
N3.txt
N4.txt
N5.txt
N6.txt
N7.txt
N8.txt
N9.txt
O0.txt
O1.txt
O10.txt
O2.txt
O3.txt
O4.txt
O5.txt
O6.txt
O7.txt
O8.txt
O9.txt
P0.txt
P1.txt
P10.txt
P2.txt
P3.txt
P4.txt
P5.txt
P6.txt
P7.txt
P8.txt
P9.txt
Q0.txt
Q1.txt
Q10.txt
Q2.txt
Q3.txt
Q4.txt
Q5.txt
Q6.txt
Q7.txt
Q8.txt
Q9.txt
R0.txt
R1.txt
R10.txt
R2.txt
R3.txt
R4.txt
R5.txt
R6.txt
R7.txt
R8.txt
R9.txt
S0.txt
S1.txt
S10.txt
S2.txt
S3.txt
S4.txt
S5.txt
S6.txt
S7.txt
S8.txt
S9.txt
T0.txt
T1.txt
T10.txt
T2.txt
T3.txt
T4.txt
T5.txt
T6.txt
T7.txt
T8.txt
T9.txt
U0.txt
U1.txt
U10.txt
U2.txt
U3.txt
U4.txt
U5.txt
U6.txt
U7.txt
U8.txt
U9.txt
V0.txt
V1.txt
V10.txt
V2.txt
V3.txt
V4.txt
V5.txt
V6.txt
V7.txt
V8.txt
V9.txt
W0.txt
W1.txt
W10.txt
W2.txt
W3.txt
W4.txt
W5.txt
W6.txt
W7.txt
W8.txt
W9.txt
X0.txt
X1.txt
X10.txt
X2.txt
X3.txt
X4.txt
X5.txt
X6.txt
X7.txt
X8.txt
X9.txt
Y0.txt
Y1.txt
Y10.txt
Y2.txt
Y3.txt
Y4.txt
Y5.txt
Y6.txt
Y7.txt
Y8.txt
Y9.txt
Z0.txt
Z1.txt
Z10.txt
Z2.txt
Z3.txt
Z4.txt
Z5.txt
Z6.txt
Z7.txt
Z8.txt
Z9.txt
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
h.txt
i.txt
j.txt
k.txt
l.txt
m.txt
my-file.txt
n.txt
o.txt
p.txt
q.txt
r.txt
s.txt
t.txt
u.txt
v.txt
w.txt
x.txt
y.txt
z.txt
You can write loops in the shell as well:
01-my-file.txt
02-my-file.txt
03-my-file.txt
A0.txt
A1.txt
A10.txt
A2.txt
A3.txt
A4.txt
A5.txt
A6.txt
A7.txt
A8.txt
A9.txt
B0.txt
B1.txt
B10.txt
B2.txt
B3.txt
B4.txt
B5.txt
B6.txt
B7.txt
B8.txt
B9.txt
C0.txt
C1.txt
C10.txt
C2.txt
C3.txt
C4.txt
C5.txt
C6.txt
C7.txt
C8.txt
C9.txt
D0.txt
D1.txt
D10.txt
D2.txt
D3.txt
D4.txt
D5.txt
D6.txt
D7.txt
D8.txt
D9.txt
E0.txt
E1.txt
E10.txt
E2.txt
E3.txt
E4.txt
E5.txt
E6.txt
E7.txt
E8.txt
E9.txt
F0.txt
F1.txt
F10.txt
F2.txt
F3.txt
F4.txt
F5.txt
F6.txt
F7.txt
F8.txt
F9.txt
G0.txt
G1.txt
G10.txt
G2.txt
G3.txt
G4.txt
G5.txt
G6.txt
G7.txt
G8.txt
G9.txt
H0.txt
H1.txt
H10.txt
H2.txt
H3.txt
H4.txt
H5.txt
H6.txt
H7.txt
H8.txt
H9.txt
I0.txt
I1.txt
I10.txt
I2.txt
I3.txt
I4.txt
I5.txt
I6.txt
I7.txt
I8.txt
I9.txt
J0.txt
J1.txt
J10.txt
J2.txt
J3.txt
J4.txt
J5.txt
J6.txt
J7.txt
J8.txt
J9.txt
K0.txt
K1.txt
K10.txt
K2.txt
K3.txt
K4.txt
K5.txt
K6.txt
K7.txt
K8.txt
K9.txt
L0.txt
L1.txt
L10.txt
L2.txt
L3.txt
L4.txt
L5.txt
L6.txt
L7.txt
L8.txt
L9.txt
M0.txt
M1.txt
M10.txt
M2.txt
M3.txt
M4.txt
M5.txt
M6.txt
M7.txt
M8.txt
M9.txt
N0.txt
N1.txt
N10.txt
N2.txt
N3.txt
N4.txt
N5.txt
N6.txt
N7.txt
N8.txt
N9.txt
O0.txt
O1.txt
O10.txt
O2.txt
O3.txt
O4.txt
O5.txt
O6.txt
O7.txt
O8.txt
O9.txt
P0.txt
P1.txt
P10.txt
P2.txt
P3.txt
P4.txt
P5.txt
P6.txt
P7.txt
P8.txt
P9.txt
Q0.txt
Q1.txt
Q10.txt
Q2.txt
Q3.txt
Q4.txt
Q5.txt
Q6.txt
Q7.txt
Q8.txt
Q9.txt
R0.txt
R1.txt
R10.txt
R2.txt
R3.txt
R4.txt
R5.txt
R6.txt
R7.txt
R8.txt
R9.txt
S0.txt
S1.txt
S10.txt
S2.txt
S3.txt
S4.txt
S5.txt
S6.txt
S7.txt
S8.txt
S9.txt
T0.txt
T1.txt
T10.txt
T2.txt
T3.txt
T4.txt
T5.txt
T6.txt
T7.txt
T8.txt
T9.txt
U0.txt
U1.txt
U10.txt
U2.txt
U3.txt
U4.txt
U5.txt
U6.txt
U7.txt
U8.txt
U9.txt
V0.txt
V1.txt
V10.txt
V2.txt
V3.txt
V4.txt
V5.txt
V6.txt
V7.txt
V8.txt
V9.txt
W0.txt
W1.txt
W10.txt
W2.txt
W3.txt
W4.txt
W5.txt
W6.txt
W7.txt
W8.txt
W9.txt
X0.txt
X1.txt
X10.txt
X2.txt
X3.txt
X4.txt
X5.txt
X6.txt
X7.txt
X8.txt
X9.txt
Y0.txt
Y1.txt
Y10.txt
Y2.txt
Y3.txt
Y4.txt
Y5.txt
Y6.txt
Y7.txt
Y8.txt
Y9.txt
Z0.txt
Z1.txt
Z10.txt
Z2.txt
Z3.txt
Z4.txt
Z5.txt
Z6.txt
Z7.txt
Z8.txt
Z9.txt
a.txt
b.txt
c.txt
d.txt
e.txt
f.txt
g.txt
h.txt
i.txt
j.txt
k.txt
l.txt
m.txt
my-file.txt
n.txt
o.txt
p.txt
q.txt
r.txt
s.txt
t.txt
u.txt
v.txt
w.txt
x.txt
y.txt
z.txt
cut
We can cut files or STDIN
. Here we get a long listing of the files in a directory, cut the output using whitespace as the field delimiter (-w
), select fields 6 and 7 (-f6-7
) which are the modification dates of the file and then get the unique values with a count of their frequency.
rm
command is dangerous!
The rm
command deletes the named files. rm -f
forces the deletion without any further confirmation. The -r
switch deletes files and directories recursively starting from the top level directory provided and descending into every directory below. The combination of the -r
and -f
switches makes it easy to accidentally delete way too much. As in, if you push it, the contents of your entire hard drive.
---
title: "Example 02: The Shell"
engine: knitr
---
We start in the home directory of the project that houses all the materials for this seminar:
```{zsh}
pwd
```
```{zsh}
ls
```
Make a temporary directory:
```{zsh}
mkdir tmp
```
## `touch` and `cat`
Use `touch` to create an empty file:
```{zsh}
touch tmp/my-file.txt
```
List it:
```{zsh}
ls -l tmp
```
Echo some text into some files:
```{zsh}
echo Hello > tmp/01-my-file.txt
echo There > tmp/02-my-file.txt
ls -l tmp
```
Cat the files
```{zsh}
cat tmp/01-my-file.txt tmp/02-my-file.txt
```
Cat the files into a new file:
```{zsh}
cat tmp/01-my-file.txt tmp/02-my-file.txt > tmp/03-my-file.txt
cat tmp/03-my-file.txt
```
_Append_ to a file with `>>` instead of `>`:
```{zsh}
cat tmp/01-my-file.txt tmp/02-my-file.txt >> tmp/03-my-file.txt
```
Result:
```{zsh}
cat tmp/03-my-file.txt
```
## Brace expansion
You can do this:
```{zsh}
#| class: scrollout
touch tmp/{a..z}.txt
ls tmp/
```
Or this:
```{zsh}
#| class: scrollout
touch tmp/{A..Z}{0..10}.txt
ls tmp/
```
## A loop
You can write loops in the shell as well:
```{zsh}
for f in {a..z}{0..10}
do
echo hello > "tmp/$f.txt"
done
```
```{zsh}
#| class: scrollout
ls tmp/
```
```{zsh}
cat tmp/u3.txt
```
## Using `cut`
We can cut files or `STDIN`. Here we get a long listing of the files in a directory, cut the output using whitespace as the field delimiter (`-w`), select fields 6 and 7 (`-f6-7`) which are the modification dates of the file and then get the unique values with a count of their frequency.
```{zsh}
ls -lt assets/01-file-system/ | cut -w -f6-7 | uniq -c
```
## Clean up
::: {.callout-important}
## The `rm` command is dangerous!
The `rm` command deletes the named files. `rm -f` forces the deletion without any further confirmation. The `-r` switch deletes files and directories _recursively_ starting from the top level directory provided and descending into every directory below. The combination of the `-r` and `-f` switches makes it easy to accidentally delete way too much. As in, if you push it, the contents of your entire hard drive.
:::
```{zsh}
## Be VERY CAREFUL with the syntax of this command
rm -rf tmp/
```