-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathposter-bash-tricks.html
522 lines (474 loc) · 25.2 KB
/
poster-bash-tricks.html
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
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
<html>
<head>
<title>bash tricks poster</title>
<style>
.examples {padding:5px 10px; }
.examples code {display:block;}
h1 {font-size:3em;}
.section {width:23%;float:left;padding-right:20px;}
table {width:100%}
.desc span, code, code i, .inner b {font-family:Courier, monospace;font-weight:normal;}
.desc { font-family: Verdana;}
table { border-collapse:collapse;}
code i {display:block;float:right;width:65%;opacity:0.4;}
.inner {margin-top:10px;margin-bottom:10px;}
.inner b {opacity:0.4;}
.wide code i {width:80%;}
code {clear:both;}
table td {padding:0 5px ;}
h3 {margin:0;line-height:16px;}
body {
margin-left:30px;
font-size: 12px;
line-height: 20px;
}
h2 {margin-top:20px;}
.desc span {display:block;}
.desc > span {margin-bottom:6px;}
.content > b {margin-top:10px;display:block}
.inner {display:block;margin-left:20px;}
.inner b {}
.breaker {clear:left;}
.breaker-small {clear:right;}
@media print {
body {font-size: 14px;line-height:22px;}
}
</style>
<!--
uncomment the style you want (dark or white)
depending on the quality of the printer, you might want to use "white" style :-)
-->
<link href="poster-dark-style.css" rel="stylesheet" type="text/css" />
<!-- link href="poster-white-style.css" rel="stylesheet" type="text/css" / -->
<!--
how to generate A1 PDF from this file:
wkhtmltopdf -O Landscape -s A1 poster-bash-tricks.html poster-printable.pdf
-->
</head>
<body>
<div id="poster">
<div id="header">
<h1>bash.tricks <span>linux shell by example</span></h1>
</div>
<div id="main">
<div class="section">
<h2>basic commands</h2>
<div class="content">
<b>system info</b>
<div class="examples">
<code> cat /etc/issue<i>show linux distribution name</i></code>
<code> cat /proc/version <i> show kernel version, etc..</i></code>
<code> cat /proc/cpuinfo <i> processor information</i></code>
<code> cat /etc/passwd <i> list users on the system</i></code>
<code> cat /etc/services <i>list reserved ports</i></code>
<code> date <i> display system time</i></code>
<code> date --date="-5 days" +%Y%m%d <i>get date in the past & format output</i> </code>
<code> hostname <i> show current machine hostname</i></code>
<code> free <i> show memory usage</i></code>
<code> uptime <i> show time since the last boot</i></code>
<code> uname -a <i> show information about system</i></code>
</div>
<b> hardware info </b>
<div class="examples">
<code> lspci <i> list all PCI devices</i></code>
<code> lshw <i> list all hardware</i></code>
<code> lshw -class network <i> list network hardware</i></code>
<code> dmidecode <i> List all hardware data from BIOS</i></code>
</div>
<b> users </b>
<div class="examples">
<code> whoami <i>show current username</i></code>
<code> w / who <i>show logged in users</i></code>
<code> finger user_name <i> System info about user</i></code>
<code> write somuser <i>Write a message to a logged in user</i></code>
<code> last <i> show last logins on your system</i></code>
<code> lastb <i> ("=last bad") show unsuccessful login attempts </i></code>
</div>
<b> drives </b>
<div class="examples">
<code> df -h <i> show harddrive usage</i></code>
<code> mount <i> show mount points</i></code>
<code> fdisk -l <i> show harddrive and partition info</i></code>
<code> hdparm -t /dev/hdX <i>show HD speed (~100MB/s is normal) </i></code>
<code> hdparm -iI /dev/hdX <i> Harddrive information</i></code>
</div>
<b> files </b>
<div class="examples">
<code> pwd <i>display current path</i></code>
<code> stat $filename <i>show filesystem info about file</i></code>
<code> file $filename <i>show file type information</i></code>
<code> time $command <i>measure execution time of command</i></code>
<code> rename 's/\.png$/.jpg/' * <i>batch rename files</i></code>
<code> du <i> size of current directory </i></code>
<code> du --max-depth=1 <i> list sizes of direct subdirs</i></code>
<code> tail -f /var/log/some <i> watch log file changes </i></code>
</div>
<b> console commands debugging </b>
<div class="examples">
<code>which [cmd] <i> show path of command [cmd]</i></code>
<code>ldd [path-to-cmd] <i> show libraries used by [cmd] </i></code>
</div>
<b> package management </b>
<div class="examples">
<code> apt-cache search $regexp <i>search available packages for $regexp</i></code>
<code> apt-get install [pkg] <i>install package named [pkg]</i></code>
</div>
<b> send mails </b>
<div class="examples">
<code> mail -s "mail subject" some@one.com <i> send email interactively. send = CTRL+D</i></code>
<code> mail -s "mail subject" some@one.com < mailbody.txt <i>Send file contents as email</i></code>
<code> echo "bodytext" | mail -s "mail subject" some@one.com <i> send mail, without need for a file</i></code>
</div>
<b> environment variables </b>
<div class="examples">
<code> PATH=some/new/path:$PATH <i> Add something to the PATH variable</i></code>
</div>
</div>
</div>
<div class="section">
<h2>bash</h2>
<div class="content wide">
<div class="desc">
keyboard commands on the bash console. you can also use them inside a screen session
</div>
<b>basic bash commands</b>
<div class="examples">
<code> cd $dir <i>change into directory</i></code>
<code> history <i>show last executed commands</i></code>
<code> !$nr <i>execute command nr $nr from history</i></code>
</div>
<b>keyboard shortcuts</b>
<div class="examples">
<code> cursor up / down <i>scroll through last commands</i></code>
<code> SHIFT + page up / down <i>view console history</i></code>
<code> CTRL + A <i>jump to first character in line</i></code>
<code> CTRL + E <i>jump to last character in line</i></code>
<code> CTRL + K <i>cut (&memorize) from cursor to end of line</i></code>
<code> CTRL + U <i>cut (&memorize) from cursor to start of line</i></code>
<code> CTRL + Y <i>paste the recently memorized content</i></code>
<code> CTRL + L <i>clear screen</i></code>
<code> CTRL + D <i>quit terminal session (same as entering "exit")</i></code>
<code> CTRL + S <i>stop console scrolling</i></code>
<code> CTRL + Q <i>continue console scrolling</i></code>
</div>
</div>
</div>
<div class="section">
<h2>grep</h2>
<div class="content">
<div class="desc">
<span>grep <b>[options]</b> searchstring <b>[file(s)]</b></span>
search file(s) and show all lines that contain the searchstring. If searching in more than one file, also shows filename.
grep can also accept input from STDIN.
</div>
<b> examples </b>
<div class="examples wide">
<code> grep string *.html -r<i> find string in html files (recursively), show filenames and lines</i></code>
<code> grep -P "$regexp" *.sh <i> show $regexp matches in .sh files in current dir </i></code>
<code> ls | grep string <i> show all filenames that contain string </i></code>
</div>
<b> options </b>
<div class="examples wide">
<code> grep -r<i> find recursively in subdirs</i></code>
<code> grep -o<i> show only the found string / the matching pattern</i></code>
<code> grep -i<i> case insensitive</i></code>
<code> grep -v<i> invert match</i></code>
<code> grep -P<i> perl regular expression matching</i></code>
</div>
</div>
</div>
<div class="section">
<h2>find</h2>
<div class="content">
<div class="desc">
<span>find <b>[path]</b> <b>[criteria]</b> <b>[action]</b></span>
all files in <b>path</b> + its subdirectories, if they match <b>criteria</b>, apply <b>action</b>. Default action is "print path+filename". Default path is "current directory".
</div>
<b>search parameters</b>
<div class="examples">
<code> -name <span>whatever</span> <i># search filename case sensitive </i></code>
<code> -<b>i</b>name <span>whatever</span> <i># filename case <b>insensitive</b></i></code>
<code> -ipath <span>whatever</span> <i># search path case insensitive</i></code>
<code> -regex / -iregex <i># regular expression search</i></code>
<code> -mtime <span>+7</span> <i># modified more than 7 days ago</i></code>
<code> -size <span>+10M</span> <i># files that are >10MB</i></code>
<code> -size <span>-1G</span> <i># file size less than 1GB</i></code>
<code> -user <span>tom </span> <i># files that belong to user tom</i></code>
<code> -type f <i># find only files</i> </code>
<code> -type d <i># find only directories</i> </code>
<code> -type l <i># find only symbolic links</i> </code>
</div>
<b>actions</b>
<div class="examples">
<code> -exec tar rvf arch.tar '{}' \;<i>execute tar command for all files, execute in current dir</i> </code>
<code> -execdir <i>execute in directory of file</i> </code>
<code> -delete <i>delete matched files</i></code>
</div>
</div>
</div>
<div class="section">
<h2>processes</h2>
<div class="content">
<b>process basics</b>
<div class="examples">
<code> ps fauxw <i>list process tree</i></code>
<code> top <i>show processes with most resource usage</i></code>
<code> htop <i>show processes with colors and fancyness</i></code>
<code> strace -p $pid <i> see commands executed by $pid </i></code>
<code> lsof -n -P |grep "*" <i> list used ports </i></code>
</div>
<b>netstat</b>
<div class="examples">
<code> netstat -lpnv <i>list all listening applications</i></code>
<code> <i>-l only listening sockets</i></code>
<code> <i>-p show process names and ids</i></code>
<code> <i>-n numeric, dont resolve hostnames</i></code>
<code> <i>-v verbose </i></code>
<code> netstat -tulpen <i>nice list without the clutter</i></code>
</div>
</div>
<h2>sed</h2>
<div class="content">
<div class="desc">
add or replace strings in files or STDOUT
</div>
<b>examples</b>
<div class="examples">
<code> sed 's/some/other/' file <i>replace some with other in file, print result</i></code>
<code> sed -r 's/perl-regex/replacement/' file <i>replace with perl regex</i></code>
</div>
</div>
<h2>vim</h2>
<div class="content">
<div class="desc">
Vim is a text editor like EMACS. VIM has it's own poster :)
</div>
<b>use vim for prettifying files</b>
<div class="examples">
<code> :1,$!xmllint --format --recover - 2>/dev/null <i>Prettify XML</i></code>
<code> :%!python -m json.tool <i>Prettify JSON</i></code>
</div>
</div>
</div>
<div class="section">
<h2>networking</h2>
<div class="content">
<div class="desc">
Remeber to use <b>sudo</b> for commands like ifconfig, nmap or tcpdump.
</div>
<b>network information and configuration</b>
<div class="examples">
<code> lspci | egrep -i --color 'network|ethernet' <i> show network hardware </i></code>
<code> cat /proc/net/dev<i> show network traffic </i></code>
<code> ifconfig <i># show network hardware config</i></code>
<code> ifconfig eth0<i># show network interface infos</i></code>
<code> ifconfig eth0 down <i># disable interface eth0</i></code>
<code> ifconfig eth0 up <i># enable interface eth0</i></code>
</div>
</div>
<div class="content wide">
<b>scan local network</b>
<div class="examples">
<code> iwlist scan <i># show wireless access points</i></code>
<code> nmap -sn 192.168.100.0/24 <i># List other computers on the network</i></code>
<code> nmap -F -O -sV 192.168.100.198 <i># Show details of one computer</i></code>
</div>
</div>
<div class="content">
<b>check network traffic</b>
<div class="examples">
<code> tcpdump -D <i># list available devices</i></code>
<code> tcpdump -i wlan0 <i># show 68 bytes (=default) of all packets</i></code>
<code> tcpdump -i wlan0 -s 0<i># show all traffic </i></code>
<code> tcpdump -i wlan0 -s 0 -w out.dump tcp port 80 <i># log full traffic on port 80 to file </i></code>
</div>
</div>
<div class="content wide">
<b>ssh related</b>
<div class="examples">
<code> ssh login@machine -L 1521:machine:1521 <i># tunnel local port 1521 to other machines port 1521</i></code>
<code> scp -l 800 hugefile.tgz example.com:~/ <i># Limit SCP upload speed to 800kbit/sec (= ~100kByte/sec)</i></code>
</div>
</div>
</div>
<div class="section">
<h2>file transfer</h2>
<div class="content">
<b>wget</b>
<div class="examples">
<code> wget "ftp://user:pass@ftp.ähh.com/*" -b -r <i># download all files from ftp (-b = in background, -r recursively) </i></code>
<code> wget "ftp://ftp.ähh.com:21/somedir/*" -r --user=user --password=pass <i># some versions need credentials as parameters </i></code>
</div>
<b>scp</b>
<div class="examples">
<code> scp ./* user@server.net:/sub/dir/ <i># upload content of current dir </i></code>
<code> scp user@server.net:/var/lib/ibdata1 test <i># download remote file ibdat to local file test </i></code>
</div>
<b>special characters in passwords</b>
<div class="desc">
unsafe characters in password/username can usually be url-encoded (="% + hex-value of the ascii code"). See this table for the most common unsafe characters:
</div>
<div class="parameters">
<h3>Parameters</h3>
<table >
<tr><th>'!'</th><td>(%21)</td></tr>
<tr><th>'%'</th><td>(%25)</td></tr>
<tr><th>':'</th><td>(%3A)</td></tr>
<tr><th>'@'</th><td>(%40)</td></tr>
</table>
</div>
</div>
</div>
<div class="breaker" />
<div class="section">
<h2>screen</h2>
<div class="content">
<div class="desc">
screen is a terminal-multiplexer. you can use it to save your bash or ssh sessions, keep sessions running on the server if you log out, and much more.
</div>
<b>manage screen sessions</b>
<div class="examples">
<code>screen -S sessionname <i>start session (with optional name "sessionname")</i></code>
<code>screen -ls <i>list all my screens</i></code>
<code>screen -r [sessionname] <i>reattach a running screen to current terminal</i></code>
</div>
</div>
<div class="content wide">
<b>keyboard commands inside screen</b>
<div class="examples">
<code>CTRL+A d<i> deatach, but keep screen running</i></code>
<code>CTRL+A c<i> create new window</i></code>
<code>CTRL+A n<i> go to next window in current screen</i></code>
<code>CTRL+A p<i> go to previous window in current screen</i></code>
<code>CTRL+A ESC<i> enter "copy mode"</i></code>
<div class="inner">
<b>inside "copy mode":</b>
<code>cursor<i>move around</i></code>
<code>enter <!--/ space--><i>start and stop selection, copy to buffer</i></code>
<code>0<i>jump to first column</i></code>
<code>$<i>jump to end of line</i></code>
<code>page up<i>view the console history</i></code>
<code>ESC<i>exit copy mode</i></code>
</div>
<code>CTRL+A ]<i>paste buffer content</i></code>
<code>CTRL+d <i>close current screen window, or close complete screen if it was the last window</i></code>
</div>
</div>
</div>
<div class="section">
<h2>tmux</h2>
<div class="content">
<div class="desc">
tmux is a modern terminal-multiplexer, it replaces screen. Additionally it can split windows.
</div>
</div>
<div class="content">
<b>basic usage</b>
<div class="examples">
<code> tmux ls<i> list running sessions</i></code>
<code> tmux new -s session-name<i> create new named session</i></code>
<code> tmux a [-t session-name]<i> resume session-name [can be name or number]</i></code>
</div>
</div>
<div class="content wide">
<b>keyboard commands inside a tmux session</b>
<div class="examples">
<code>CTRL+b ? <i>get help</i></code>
<code>CTRL+b s <i>list sessions</i></code>
<code>CTRL+b $ <i>rename the current session</i></code>
<code>CTRL+b d <i>detach from the current session</i></code>
<code>CTRL+b %<i> split window vertically</i></code>
<code>CTRL+b "<i> split window horizontally</i></code>
<code>CTRL+b z<i> toggle fullscreen current split</i></code>
<code>CTRL+b cursor<i> go to split window</i></code>
<code>CTRL+b [<i> enter "copy mode"</i></code>
<div class="inner">
<b>inside "copy mode":</b>
<code>cursor<i>move around</i></code>
<code>CTRL+SPACE<i> visual selection</i></code>
<code>ALT+w<i> copy selection</i></code>
<code>ESC<i>exit copy mode</i></code>
</div>
<code>CTRL+b ]<i>paste selection</i></code>
</div>
</div>
</div>
<div class="section">
<h2>tar</h2>
<div class="content">
<div class="desc">
<span>tar [options]f filename.tar dir</span>
copy all files from directory "dir" into archive file "filename.tar".
</div>
<b>examples</b>
<div class="examples">
<code>tar cf a.tar dir <i># create archive a.tar with all files from dir (+subdirs)</i> </code>
<code>tar c<b>z</b>f a.tgz dir <i>archive and gzip files</i> </code>
<code>tar czf a.tgz --newer-mtime 02/25/2025 dir <i>archive all files that were created or modified after 25.02.2025 </i> </code>
<code>tar czf a.tgz --exclude="temp/*" --exclude="bak/*" dir <i>except files in temp or bak dirs</i> </code>
<code>tar <b>t</b>zf a.tgz <i>print a list of all files in a.tgz</i></code>
<code>tar -xzf a.tgz <i>extract, keep directory structure</i></code>
</div>
</div>
<h2>gzip / bzip2</h2>
<div class="content">
<div class="desc">
<span>gzip [options] filename</span>
Compress or uncompress files. gzip and bzip2 have almost same syntax.
</div>
<b>examples</b>
<div class="examples">
<code>gzip $file <i>compress $file into $file.gz, remove $file</i> </code>
<code>gzip -d $file.gz <i>uncompress $file.gz into $file</i> </code>
<code>gzip -dc $file | more<i>uncompress $file to STDOUT</i> </code>
</div>
</div>
</div>
<div class="section">
<h2>awk</h2>
<div class="content wide">
<div class="examples">
<code>
awk -F"\t" '{print $3}' filename <i>Print third column of tab separated file (-F specify separator char)</i>
</code>
<code>
cat <b>access_log</b> | awk '{urls[<b>$1</b>]++ } END {for (a in urls) {printf ("%50s\n", a ) }}'
<i>print number of occurrence for all urls in <b>first</b> column of <b>access_log</b></i>
</code>
<code> ls dir | awk '{print "mv "$1" "$1".new"}' | sh <i> rename files to ".new"</i></code>
<code>
ls -l *old* | awk '{print "mv "$1" "$1}' | sed s/old/new/2 | sh
<i> rename inside of filename</i>
</code>
<code>
kill `ps auxw | grep $processname | egrep -v grep | awk '{print $2}'`
<i> kill all processes with name $processname</i>
</code>
</div>
</div>
</div>
<div class="section">
<h2>advanced exaples</h2>
<div class="content">
<div class="examples">
<code>
for i in `seq 20 10 100`; do echo $i; done
<i>print the numbers from 20 to 100 in steps of 10</i>
</code>
<code>
perl -e 'while(true){print (`clear; ps aux|grep $processname|grep -v grep`); sleep (1); }'
<i> constantly updated list of processes named $processname</i>
</code>
<code>
df -P | awk '{print $5}'| tr -d '%' | grep '[0-9]' | xargs -I {} sh -c 'if \[ 0 -lt {} \];then echo ne disk is {}% full:;df; fi' | mail -E -s 'disk full' some@user.com
<i>use this cronjob to send you an email you when the disk is full</i>
</code>
</div>
</div>
</div>
</div>
<div id="footer">
</div>
</div>
</body>
</html>