index.html 8.64 KB
Newer Older
nilsl's avatar
add  
nilsl committed
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
<!DOCTYPE html>
<html>
  <head>
    <title>Linux Toolkit</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <link rel="stylesheet" href="css/remark.css">
    <link rel="stylesheet" media="print" href="css/remark.print.css">
  </head>
  <body>
    <textarea id="source">

class: center, middle

# Linux Toolbox

<img src="images/thealternative-logo.jpg" width="30%">

---

# We will see

...a short **Bash** introduction, to get the necessary intuition for what most tools in this course do.

...how most classic Linux tools **compose**.

...a wide spectrum of available **tools** and what you can do with them.

---

# Goals

Reduce unknown unknowns (`what's available?`, `what do I search for?`, etc).

Give a rough intuition for what classic Linux tools can do.

Provide pointers for where to learn the tools.

Learn vocabulary.

Non-goal: teach you how to use the tools - I would love to, but we don't have time!

---


```bash
file=$(ls -tp1 ~/Downloads | grep -v "/$" | head -1)
dirs='Images,Videos,Documents,Documents/books,DELETE,OPEN'
choice=$(echo $dirs | rofi -dmenu -sep ',' -p "move '$file' where? ")
[[ "$choice" = "" ]] && exit
[[ "$choice" = DELETE ]] && rm ~/Downloads/"$file" && exit
[[ "$choice" = OPEN ]] && xdg-open ~/Downloads/"$file" && exit
mv ~/Downloads/"$file" "$choice"
```

safe the above in `/home/nils/filer.sh` and add a few lines of text in `~/.xbindkeysrc`:

```
"bash /home/nils/filer.sh"
  control+shift+f
```

---

# How to use this talk

- pick and choose the tools that seem interesting to you and try to learn them
- come back to the slides and use them as a glossary for unknown words
- ...or just be amused with what people come up with

---

# Some philosophy

Many of the classical Linux tools adhere to the "UNIX principle":

<blockquote>Do one thing and do it well.</blockquote>

In a wider, non-programming sense: It's useful to  
learn things that are small, orthogonal, and compose.


# Bash

Related words: terminal, terminal emulator, shell, command line, ...

<img src="images/bash.png">

In a nutshell:  
Bash is a tool that let's you interact with your computer via text.

**Try it out:** Open a terminal (just search for `Terminal` in your launcher)

---

# Bash Commands

Bash let's you interact with your computer by typing commands.

There are **many** commands. Some examples:  
`ls`, `pwd`, `echo`, `cd`, `mv`, `sleep`, `alias`, ...

Some commands are built into Bash, others can be downloaded.

Some commands are really just programs: try typing `firefox` and hitting Enter if you have it installed.

---

# TODO ls & cd video

---

# TODO: vocab

---

# Bash For Programming

Bash can even be used as a (terrible) programming language, using commands like `for`, `if`, `while`, `[[` etc.  

It also makes use of special constructs that aren't commands (e.g. arithmetic expansion: `echo $((5+3))`)

We'll only bother with `|` and `>` ("piping" and "redirecting").

---

# | and >

Two incredibly useful Bash features!

Both redirect command output:  
- `|` redirects to a new command ("piping")
- `>` redirects into a file ("file redirection" or "redirection" for short)

<img src="images/cat.png" width="30%">
<img src="images/catpipe.png" width="30%">
<img src="images/catpiperedirection.png" width="30%">

---

# bash summary

bash is a different way of interacting with your system

bash runs in your terminal.

bash is a shell. there are others: Zsh, fish, Dash, csh

resources: 
- our bash guide (https://thealternative.ch/guides/bash.php)
- Lhunath and GreyCat's bash guide (http://mywiki.wooledge.org/FullBashGuide)

---

# bash power

Move all `.png` files from Downloads to Images:  
```bash
mv ~/Downloads/*.png ~/Images`
```

go to our images, make a new directory, and move newest five images there:
```bash
cd ~/Images
mkdir newImages
ls -1t *.png | head -5 | xargs -I {} mv {} newImages
```

rename all images in a folder to `*_with_family`:  
```bash
for file in *.png; do
        mv "$file" "$file_with_family"
done
```

---

# Tool Galore

We will now talk about multiple tools and how to combine them using bash (mostly).

---

# Scripting Languages

bash is not ideal as a programming language.for more complex jobs,a real programming language is needed.

two main choices:  
- Python (recommended for beginners)
- Ruby (only if you already know programming; compact syntax)

these fill niches that bash doesn't, but are still close to the system.

<img src="images/polygon.png">

---

# Notifications

typically linux has a notification demon integrated.

```bash
notify-send "Hello"
```

this is often useful for things that you want to run in the background.  
Example: Timer

```bash
sleep 300; notify-send "Time is up!"
```

---

# Rofi

can be used to display a list of things  
and then let's us choose one of them.

```bash
echo "one\ntwo\nthree" | rofi -dmenu
```

on its own is not useful, but can serve as a powerful launcher when combined with other things:  

```bash
ls -1 ~/Documents/books/*.pdf | rofi -dmenu | xargs -I {} evince {}
```

https://github.com/davatorium/rofi

---

# xbindkeys

A **keydeamon**! Let's us configure keyboard shortcuts.

in file `~/.xbindkeysrc`:

```bash
"notify-send test"
  control+t

"ls -1 ~/Documents/books/*.pdf | rofi -dmenu | xargs -I {} evince {}"
  control+Mod1+b
```

<img src="images/booklauncher.png">

---


```bash
file=$(ls -tp1 ~/Downloads | grep -v "/$" | head -1)
dirs='Images,Videos,Documents,Documents/books,DELETE,OPEN'
choice=$(echo $dirs | rofi -dmenu -sep ',' -p "move '$file' where? ")
[[ "$choice" = "" ]] && exit
[[ "$choice" = DELETE ]] && rm ~/Downloads/"$file" && exit
[[ "$choice" = OPEN ]] && xdg-open ~/Downloads/"$file" && exit
mv ~/Downloads/"$file" "$choice"
```

safe the above in `/home/nils/filer.sh` and add a few lines of text in `~/.xbindkeysrc`:

```
"bash /home/nils/filer.sh"
  control+shift+f
```

---


# grep

a command to look for **regular expressions**.

think of regular expressions as being text patterns on steroids:

`grep "^[0-9]" data.csv` prints all the lines starting with a number

we can try a multiple greps together to chain filters:

<img src="images/">
<img src="images/">
<img src="images/">

---

# sed

**s**tream **ed**itor

this tool can search for regular expressions like we did with `grep`,  
and perform various operations on each output line separately

---

# find

a general purpose tool for finding various files according to search filters.

especially nice if used with xargs-style option or regular expressions:

Remove all files (`-type f`) that end in .php (`*.php`):  
```bash
find . -type f -name "*.php" -exec rm {} \;
```

Display all files that contain a comment that contains `TODO`:  
```bash
find ~/Documents -type f -exec grep -l "//.*TODO" {} \; 
```

---

# file conversion

Florian Moser's avatar
Florian Moser committed
317
sometimes you want to convert between similar file formats. there are a lot of tools for this. most are specific for some kind of file content.here are some common ones:
nilsl's avatar
add  
nilsl committed
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335

- `ffmpeg` for audio and video
- `pandoc` for many hierarchical text formats (mark down, html, latex)
- `pandoc` for image files

<hr>

Using a bash for-loop, convert all `wav`-files to `mp3`:  
```bash
for file in *.wav; do
        ffmpeg -i "$file" "${file%.*}.mp3"
done
```

(what `ffmpeg` sees: `ffmpeg -i song.wav song.mp3`)

---

Florian Moser's avatar
Florian Moser committed
336
# wget
nilsl's avatar
add  
nilsl committed
337

Florian Moser's avatar
Florian Moser committed
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
mirror webpage

```bash
wget --mirror --convert-links --page-requisites \
--no-parent -e robots=off https://thealternative.ch
```


---

# git

Git is a version control system: Keep track of who changed what, when and why, and revert changes easily. 

```bash
git init
git add *
git commit -m "Initial commit"
git remote add git@gitlab.ethz.com/thealternative/courses.git
git push -u origin --all
```

*Visit the git course next week!*

---

# ssh

`Euler` is an ETH supercomputer:

```bash
ssh nilsl@euler.ethz.ch
```

Connect with SSH and execute commands in the console of the server.

---

# tmux

Execute long-running processes in the background while your terminal is closed. Especially useful in combination with SSH.

```bash
tmux new -s new_session
tmux attach new_session
```

---

# xdotools

Automate key presses & mouse movements.
```
xdotool key ctrl+c key alt+Tab key ctrl+v
xdotool mousemove 20 100 click 1 sleep 2 mosemove 100 20
```

---

# Longlist of interesting tools

**stow** lets you manage config files easily.  
**ranger** is a console-based file manager.  
**i3** is an alternative window-manager.  
**borg** is a backup tool.


---

# Longlist of interesting tools with not helpful descriptions

**ranger** is a cringe file manager.  
**i3** is gnome for vegans.  
**rm -rf /** saves storage space.
nilsl's avatar
add  
nilsl committed
412
413
414
415
416
417

    </textarea>
    <script src="js/remark.min.js" type="text/javascript"></script>
    <script src="js/remark.driver.js" type="text/javascript"></script>
  </body>
</html>