sp-tutorial 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.markdown +42 -0
- data/Rakefile +36 -0
- data/TODO +6 -0
- data/VERSION.yml +4 -0
- data/config.ru +15 -0
- data/lib/config.ru +15 -0
- data/lib/public/doc/Cyklop-otf-0_91.zip +0 -0
- data/lib/public/doc/ex/sp_lab01_zad.odt +0 -0
- data/lib/public/doc/ex/sp_lab02_zad.odt +0 -0
- data/lib/public/doc/ex/sp_lab03_zad.odt +0 -0
- data/lib/public/doc/ex/sp_lab04_zad.odt +0 -0
- data/lib/public/doc/latexsheet.pdf +0 -0
- data/lib/public/doc/survival.pdf +0 -0
- data/lib/public/images/Thatll_Flat_Git_It_Vol_20.jpg +0 -0
- data/lib/public/images/alan_kay.jpg +0 -0
- data/lib/public/images/alan_perlis.jpg +0 -0
- data/lib/public/images/albert_einstein.jpg +0 -0
- data/lib/public/images/algorithm.png +0 -0
- data/lib/public/images/biuletyn-snall.jpg +0 -0
- data/lib/public/images/bop.jpg +0 -0
- data/lib/public/images/borenstein.jpg +0 -0
- data/lib/public/images/commits.png +0 -0
- data/lib/public/images/gitk-branches.png +0 -0
- data/lib/public/images/jkew.jpg +0 -0
- data/lib/public/images/jwz.gif +0 -0
- data/lib/public/images/knuth.jpg +0 -0
- data/lib/public/images/marcin_wolinski.jpg +0 -0
- data/lib/public/images/objects-example.png +0 -0
- data/lib/public/images/perlis.gif +0 -0
- data/lib/public/images/real_programmers.png +0 -0
- data/lib/public/images/richard_stallman.jpg +0 -0
- data/lib/public/images/sp.png +0 -0
- data/lib/public/images/sp.svg +117 -0
- data/lib/public/images/spowrotem.jpg +0 -0
- data/lib/public/images/staging_area.png +0 -0
- data/lib/public/images/the_thinker.jpg +0 -0
- data/lib/public/images/tparr.jpg +0 -0
- data/lib/public/images/wide-gitk.gif +0 -0
- data/lib/public/javascripts/sp.js +1 -0
- data/lib/public/stylesheets/fonts/Cyklop-Italic.otf +0 -0
- data/lib/public/stylesheets/icons/doc.png +0 -0
- data/lib/public/stylesheets/icons/email.png +0 -0
- data/lib/public/stylesheets/icons/external.png +0 -0
- data/lib/public/stylesheets/icons/feed.png +0 -0
- data/lib/public/stylesheets/icons/im.png +0 -0
- data/lib/public/stylesheets/icons/pdf.png +0 -0
- data/lib/public/stylesheets/icons/visited.png +0 -0
- data/lib/public/stylesheets/icons/xls.png +0 -0
- data/lib/public/stylesheets/ie.css +27 -0
- data/lib/public/stylesheets/print.css +30 -0
- data/lib/public/stylesheets/screen.css +249 -0
- data/lib/public/stylesheets/sp.css +194 -0
- data/lib/public/stylesheets/src/grid.png +0 -0
- data/lib/public/stylesheets/uv.css +121 -0
- data/lib/sp-tutorial.rb +78 -0
- data/lib/views/answers.rdiscount +7 -0
- data/lib/views/exercises.rdiscount +154 -0
- data/lib/views/favicon.ico.rdiscount +0 -0
- data/lib/views/git.rdiscount +558 -0
- data/lib/views/labs01.rdiscount +54 -0
- data/lib/views/labs02.rdiscount +32 -0
- data/lib/views/labs03.rdiscount +28 -0
- data/lib/views/labs04.rdiscount +41 -0
- data/lib/views/latex.rdiscount +498 -0
- data/lib/views/layout.rdiscount +41 -0
- data/lib/views/ll.rdiscount +78 -0
- data/lib/views/main.rdiscount +67 -0
- data/lib/views/scripts.rdiscount +618 -0
- data/lib/views/unix-commands.rdiscount +604 -0
- data/lib/views/unix-guru.rdiscount +696 -0
- data/sp-tutorial.gemspec +125 -0
- metadata +186 -0
@@ -0,0 +1,696 @@
|
|
1
|
+
#### {% title "Jak zostać Uniksowym guru w kwadrans" %}
|
2
|
+
|
3
|
+
[unix guru]: http://www.cs.usfca.edu/~parrt/course/601/lectures/unix.util.html "How To Look Like A UNIX Guru"
|
4
|
+
|
5
|
+
|
6
|
+
# Jak zostać Uniksowym guru w kwadrans
|
7
|
+
|
8
|
+
<blockquote>
|
9
|
+
{%= image_tag "/images/richard_stallman.jpg", :alt => "[Richard Stallman]" %}
|
10
|
+
<p>
|
11
|
+
Richard M. Stallman, Linus Torvalds,
|
12
|
+
and Donald E. Knuth engage in a discussion on whose impact
|
13
|
+
on the computerized world was the greatest.<br/>
|
14
|
+
Stallman: "God told me I have programmed
|
15
|
+
the best editor in the world!"
|
16
|
+
Torvalds: "Well, God told *me* that I have programmed
|
17
|
+
the best operating system in the world!"
|
18
|
+
Knuth: "Wait, wait – I never said that."
|
19
|
+
</p>
|
20
|
+
<p class="author">[from rec.humor.funny]</p>
|
21
|
+
</blockquote>
|
22
|
+
|
23
|
+
UNIX is an extremely popular platform for deploying server software
|
24
|
+
partly because of its security and stability, but also because it has
|
25
|
+
a rich set of command line and scripting tools. Programmers use these
|
26
|
+
tools for manipulating the file system, processing log files, and
|
27
|
+
generally automating as much as possible.
|
28
|
+
|
29
|
+
If you want to be a serious server developer, you will need to have a
|
30
|
+
certain facility with a number of UNIX tools; about 15. You will start
|
31
|
+
to see similarities among them, particularly regular expressions, and
|
32
|
+
soon you will feel very comfortable. Combining the simple commands,
|
33
|
+
you can build very powerful tools very quickly—much faster than you
|
34
|
+
could build the equivalent functionality in C or Java, for example.
|
35
|
+
|
36
|
+
|
37
|
+
## Wszystko jest strumieniem
|
38
|
+
|
39
|
+
The first thing you need to know is that UNIX is based upon the idea
|
40
|
+
of a *stream*. Everything is a *stream*, or appears to be. Device
|
41
|
+
drivers look like streams, terminals look like streams, processes
|
42
|
+
communicate via streams, etc… The input and output of a program are
|
43
|
+
streams that you can redirect into a device, a file, or another
|
44
|
+
program.
|
45
|
+
|
46
|
+
Here is an example device, the null device, that lets you throw output
|
47
|
+
away. For example, you might want to run a program but ignore the
|
48
|
+
output.
|
49
|
+
|
50
|
+
:::shell-unix-generic
|
51
|
+
ls > /dev/null # ignore output of ls
|
52
|
+
|
53
|
+
where "`# ignore output of ls`" is a comment.
|
54
|
+
|
55
|
+
Most of the commands covered in this lecture process `stdin`
|
56
|
+
and send results to `stdout`. In this manner, you can
|
57
|
+
incrementally process a data stream by hooking the output of one tool
|
58
|
+
to the input of another via a *pipe*. For example, the
|
59
|
+
following piped sequence prints the number of files in the current
|
60
|
+
directory modified in August.
|
61
|
+
|
62
|
+
:::shell-unix-generic
|
63
|
+
ls -l | grep ' 10-' | wc -l
|
64
|
+
|
65
|
+
Imagine how long it would take you to write the equivalent C or Java
|
66
|
+
program. You can become an extremely productive UNIX programmer if
|
67
|
+
you learn to combine the simple command-line tools. Even when
|
68
|
+
programming on a PC, I use MKS's UNIX shell and command library to
|
69
|
+
make it look like a UNIX box. Worth the cash.
|
70
|
+
|
71
|
+
|
72
|
+
## Uzyskiwanie pomocy
|
73
|
+
|
74
|
+
If you need to know about a command, ask for the "man" page. For
|
75
|
+
example, to find out about the `ls` command, type
|
76
|
+
|
77
|
+
:::shell-unix-generic
|
78
|
+
man ls
|
79
|
+
LS(1) User Commands LS(1)
|
80
|
+
|
81
|
+
NAME
|
82
|
+
ls - list directory contents
|
83
|
+
|
84
|
+
SYNOPSIS
|
85
|
+
ls [OPTION]... [FILE]...
|
86
|
+
|
87
|
+
DESCRIPTION
|
88
|
+
List information about the FILEs (the current directory
|
89
|
+
by default). Sort entries alphabetically if none
|
90
|
+
of -cftuvSUX nor --sort.
|
91
|
+
...
|
92
|
+
|
93
|
+
You will get a summary of the command and any arguments.
|
94
|
+
|
95
|
+
If you cannot remember the command's name, try using `apropos`
|
96
|
+
which finds commands and library routines related to that word.
|
97
|
+
For example, to find out how to do checksums, type
|
98
|
+
|
99
|
+
apropos '\bgit\b'
|
100
|
+
gitrepository [] (5) - layout - Git Repository Layout
|
101
|
+
gittutorial [] (7) - A tutorial introduction to git
|
102
|
+
gitworkflows [] (7) - An overview of recommended workflows with git
|
103
|
+
|
104
|
+
|
105
|
+
## Special Directories and files
|
106
|
+
|
107
|
+
A shortcut for my home directory, `/home/pracinf/wbzyl`, is
|
108
|
+
`~wbzyl`.
|
109
|
+
|
110
|
+
When you are using the shell, there is the notion of <em>current
|
111
|
+
directory</em>. The dot '`.`' character is a shorthand for the current
|
112
|
+
directory and '`..`' is a shorthand for the directory above the
|
113
|
+
current. So to access file `test` in the current directory, `./test`
|
114
|
+
is the same as plain `test`. If `test` is a directory above, use
|
115
|
+
`../test`.
|
116
|
+
|
117
|
+
`/` is the root directory; there is no drive specification in UNIX.
|
118
|
+
|
119
|
+
The `.bash_profile` file is very important as it is how your shell
|
120
|
+
session is initialized including your ever-important `PATH`
|
121
|
+
environment variable. My `bash` shell initialization file is
|
122
|
+
`~wbzyl/.bash_profile` and has set up code like the following:
|
123
|
+
|
124
|
+
:::shell-unix-generic
|
125
|
+
PATH=$PATH:$HOME/bin
|
126
|
+
export PATH
|
127
|
+
|
128
|
+
The `export` means that the assignment to `PATH` is visible to
|
129
|
+
all child processes (that is, visible to all programs you run from the
|
130
|
+
shell).
|
131
|
+
|
132
|
+
|
133
|
+
## Podstawowe polecenia
|
134
|
+
|
135
|
+
### cd
|
136
|
+
|
137
|
+
Changing a directory is done with `cd` <em>dir</em> where <em>dir</em> can be "." or
|
138
|
+
".." to move to current directory (do nothing) or go up a directory.
|
139
|
+
|
140
|
+
### ls
|
141
|
+
|
142
|
+
Display files in a directory with `ls`. The `-l` option is used to
|
143
|
+
display details of the files:
|
144
|
+
|
145
|
+
:::shell-unix-generic
|
146
|
+
total 1234
|
147
|
+
drwxr-xr-x 5 wbzyl pracinf 4096 08-04 00:08 public_git
|
148
|
+
drwx--x--x 2 wbzyl pracinf 4096 08-02 20:20 public_html
|
149
|
+
drwxr-xr-x 7 wbzyl pracinf 4096 08-02 10:10 rpm
|
150
|
+
drwx--x--x 2 wbzyl pracinf 4096 08-02 10:00 tmp
|
151
|
+
...
|
152
|
+
|
153
|
+
"pracinfo" is wbzyl's group.
|
154
|
+
|
155
|
+
If you want to see hidden files (those starting with "."), use `-a`.
|
156
|
+
|
157
|
+
Combinations are possible: use `ls -la` to see details of all files
|
158
|
+
including hidden ones.
|
159
|
+
|
160
|
+
### wyświetlanie zawartości plików
|
161
|
+
|
162
|
+
There are 4 useful ways to display the contents or portions of a file.
|
163
|
+
The first is the very commonly used command `cat`. For example, to
|
164
|
+
display my email box, type:
|
165
|
+
|
166
|
+
:::shell-unix-generic
|
167
|
+
cat /var/mail/wbzyl
|
168
|
+
|
169
|
+
If a file is really big, you will probably want to use `more`,
|
170
|
+
which spits the file out in screen-size chunks.
|
171
|
+
|
172
|
+
:::shell-unix-generic
|
173
|
+
more /var/mail/wbzyl
|
174
|
+
|
175
|
+
If you only want to see the first few lines of a file or the last few
|
176
|
+
lines use `head` and `tail`.
|
177
|
+
|
178
|
+
:::shell-unix-generic
|
179
|
+
head /var//mail/wbzyl
|
180
|
+
tail /var/mail/wbzyl
|
181
|
+
|
182
|
+
You can specify a number as an argument to get a specific number of
|
183
|
+
lines:
|
184
|
+
|
185
|
+
:::shell-unix-generic
|
186
|
+
head -30 /var/mail/wbzyl
|
187
|
+
|
188
|
+
The most useful incantation of `tail` prints the last few lines of a
|
189
|
+
file and then waits, printing new lines as they are appended to the
|
190
|
+
file. This is great for watching a log file:
|
191
|
+
|
192
|
+
:::shell-unix-generic
|
193
|
+
tail -f /var/mail/wbzyl
|
194
|
+
|
195
|
+
If you need to know how many characters, words, or lines are in a
|
196
|
+
file, use `wc`:
|
197
|
+
|
198
|
+
:::shell-unix-generic
|
199
|
+
wc /var/mail/mail
|
200
|
+
100 1000 40000 /var/mail/wbzyl
|
201
|
+
|
202
|
+
Where the numbers are, in order, lines, words, then characters. For
|
203
|
+
clarity, you can use `wc -l` to print just the number of lines.
|
204
|
+
|
205
|
+
### pushd, popd
|
206
|
+
|
207
|
+
Instead of `cd` you can use `pushd` to save the current
|
208
|
+
dir and then automatically `cd` to the specified directory.
|
209
|
+
For example,
|
210
|
+
|
211
|
+
:::shell-unix-generic
|
212
|
+
pwd
|
213
|
+
/home/pracinf/wbzyl
|
214
|
+
pushd /tmp
|
215
|
+
/tmp ~
|
216
|
+
pwd
|
217
|
+
/tmp
|
218
|
+
popd
|
219
|
+
~
|
220
|
+
pwd
|
221
|
+
/home/pracinf/wbzyl
|
222
|
+
|
223
|
+
### top (htop)
|
224
|
+
|
225
|
+
To watch a dynamic display of the processes on your box in action, use
|
226
|
+
`top` or `htop`.
|
227
|
+
|
228
|
+
### ps
|
229
|
+
|
230
|
+
To print out (wide display) all processes running on a box, use
|
231
|
+
|
232
|
+
:::shell-unix-generic
|
233
|
+
ps auxww # double w
|
234
|
+
|
235
|
+
### chmod
|
236
|
+
|
237
|
+
To change the privileges of a file or directory, use `chmod`. The
|
238
|
+
privileges are 3 digit octal words with 3 bits per digit: rwxrwxrwx
|
239
|
+
where the first digit is for the file owner, the 2nd for the group,
|
240
|
+
and 3rd for anybody. 644 is a common word value file which means
|
241
|
+
110100100 or
|
242
|
+
|
243
|
+
:::shell-unix-generic
|
244
|
+
rw-r--r--
|
245
|
+
|
246
|
+
When you do `ls -l` you will see these bits.
|
247
|
+
|
248
|
+
755 is a common word value for directories:
|
249
|
+
|
250
|
+
:::shell-unix-generic
|
251
|
+
rwxr-xr-x
|
252
|
+
|
253
|
+
where directories need to be executable for `cd` to be able to enter that
|
254
|
+
dir. 755 is a shorthand for the more readable argument
|
255
|
+
|
256
|
+
u=rwx,go=rx
|
257
|
+
|
258
|
+
u is user, g is group, o is other.
|
259
|
+
|
260
|
+
Use `chmod -R` for recursively applying to all the dirs below the
|
261
|
+
argument as well.
|
262
|
+
|
263
|
+
|
264
|
+
## Searching streams
|
265
|
+
|
266
|
+
One of the most useful tools available on UNIX and the one you may use
|
267
|
+
the most is `grep`. This tool matches regular expressions
|
268
|
+
(which includes simple words) and prints matching lines to
|
269
|
+
`stdout`.
|
270
|
+
|
271
|
+
The simplest incantation looks for a particular character sequence in
|
272
|
+
a set of files. Here is an example that looks for
|
273
|
+
lines begining with 'Return-Path:' in the IMAP mails
|
274
|
+
in the current directory.
|
275
|
+
|
276
|
+
:::shell-unix-generic
|
277
|
+
grep '^Return-Path:' *,{S,RS,ST,FRST}
|
278
|
+
|
279
|
+
You may find the dot '.' regular expression useful. It matches any
|
280
|
+
single character but is typically combined with the star, which
|
281
|
+
matches zero or more of the preceding item. Be careful to enclose the
|
282
|
+
expression in single quotes so the command-line expansion doesn't
|
283
|
+
modify the argument. The following example, looks for references to
|
284
|
+
any a forum page in a server log file:
|
285
|
+
|
286
|
+
:::shell-unix-generic
|
287
|
+
sudo egrep '/forum/.*' /var/log/httpd/access_log
|
288
|
+
|
289
|
+
or equivalently:
|
290
|
+
|
291
|
+
:::shell-unix-generic
|
292
|
+
sudo cat /var/log/access_log | grep '/forum/.*'
|
293
|
+
|
294
|
+
The second form is useful when you want to process a collection of
|
295
|
+
files as a single stream as in:
|
296
|
+
|
297
|
+
:::shell-unix-generic
|
298
|
+
sudo cat /var/log/httpd/*_log | grep '/forum/.*'
|
299
|
+
|
300
|
+
If you need to look for a string at the beginning of a line, use caret '^':
|
301
|
+
|
302
|
+
:::shell-unix-generic
|
303
|
+
grep '^153.19.7.230' /var/log/httpd/access_log
|
304
|
+
|
305
|
+
This finds all lines in all access logs that begin with IP address
|
306
|
+
153.19.7.230.
|
307
|
+
|
308
|
+
If you would like to invert the pattern matching to find lines that do
|
309
|
+
not match a pattern, use `-v`. Here is an example that finds
|
310
|
+
references to non image `GET`s in a log file:
|
311
|
+
|
312
|
+
:::shell-unix-generic
|
313
|
+
sudo cat /var/log/access_log | grep -v '/images'
|
314
|
+
|
315
|
+
Now imagine that you have an http log file and you would like to
|
316
|
+
filter out page requests made by nonhuman spiders. If you have a file
|
317
|
+
called `spider.ips`, you can find all nonspider page views via:
|
318
|
+
|
319
|
+
:::shell-unix-generic
|
320
|
+
cat /var/log/httpd/access_log | grep -v -f /tmp/spider.ips
|
321
|
+
|
322
|
+
Finally, to ignore the case of the input stream, use `-i`.
|
323
|
+
|
324
|
+
|
325
|
+
## Przekształcanie strumieni tekstu
|
326
|
+
|
327
|
+
Morphing a text stream is a fundamental UNIX operation.
|
328
|
+
|
329
|
+
### tr
|
330
|
+
|
331
|
+
For manipulating whitespace, you will find `tr` very useful.
|
332
|
+
|
333
|
+
If you have columns of data separated by spaces and you would like the
|
334
|
+
columns to collapse so there is a single column of data, tell
|
335
|
+
`tr` to replace space with newline `tr ' ' '\n'`.
|
336
|
+
Consider input file `falski.txt`:
|
337
|
+
|
338
|
+
:::shell-unix-generic
|
339
|
+
ala ma kota
|
340
|
+
ola ma psa
|
341
|
+
|
342
|
+
To get all those names in a column, use
|
343
|
+
|
344
|
+
:::shell-unix-generic
|
345
|
+
cat falski.txt | tr ' ' '\n'
|
346
|
+
|
347
|
+
If you would like to collapse all sequences of spaces into one single
|
348
|
+
space, use `tr -s ' '`.
|
349
|
+
|
350
|
+
To convert a PC file to UNIX, you have to get rid of the '\r'
|
351
|
+
characters. Use `tr -d '\r'`.
|
352
|
+
|
353
|
+
### sed
|
354
|
+
|
355
|
+
If dropping or translating single characters is not enough, you can
|
356
|
+
use `sed` (stream editor) to replace or delete text chunks matched by
|
357
|
+
regular expressions. For example, to replace all references to text
|
358
|
+
*TT* by *kbd* in the file *ug.rdiscount*, use
|
359
|
+
|
360
|
+
:::shell-unix-generic
|
361
|
+
cat /ug.rdiscount | sed -r 's/TT/kbd/g'
|
362
|
+
|
363
|
+
If there are multiple references to *TT* on a single line, use the `g`
|
364
|
+
suffix to indicate "global" on that line otherwise only the first
|
365
|
+
occurrence will be removed:
|
366
|
+
|
367
|
+
:::shell-unix-generic
|
368
|
+
... | sed -r 's/TT/kbd/g'
|
369
|
+
|
370
|
+
If you would like to replace references to *view.html* with
|
371
|
+
*index.html*, use
|
372
|
+
|
373
|
+
:::shell-unix-generic
|
374
|
+
... | sed 's/view.html/index.html/'
|
375
|
+
|
376
|
+
If you want any `.pas` file converted to `.p`, you must match the file
|
377
|
+
name with a regular expression and refer to it via `\1`:
|
378
|
+
|
379
|
+
:::shell-unix-generic
|
380
|
+
... | sed 's/\(.*\).pas/\1.p/'
|
381
|
+
|
382
|
+
The `\(...\)` grouping collects text that you can refer to with `\1`.
|
383
|
+
|
384
|
+
If you want to kill everything from the ',' character to end of line,
|
385
|
+
use the end-of-line marker `$`:
|
386
|
+
|
387
|
+
:::shell-unix-generic
|
388
|
+
... | sed 's/,.*$//' # kill from comma to end of line
|
389
|
+
|
390
|
+
|
391
|
+
## Tarballs
|
392
|
+
|
393
|
+
Note: *The name comes from a similar word, **hairball** (stuff that
|
394
|
+
cats throw up), I'm pretty sure.*
|
395
|
+
|
396
|
+
To collect a bunch of files and directories together, use
|
397
|
+
`tar`. To tar up and gzip your entire home directory and
|
398
|
+
put the gzipped tarball into `/tmp`, do this
|
399
|
+
|
400
|
+
:::shell-unix-generic
|
401
|
+
cd ~/.. # go one dir above dir you want to tar.gz; why?
|
402
|
+
tar zcvf /tmp/wbzyl.backup.tar.gz wbzyl
|
403
|
+
|
404
|
+
By convention, use `.tar.gz` as the extension.
|
405
|
+
To ungzip and untar this file use
|
406
|
+
|
407
|
+
:::shell-unix-generic
|
408
|
+
cd ~/tmp
|
409
|
+
tar zxvf /tmp/wbzyl.backup.tar.gz
|
410
|
+
|
411
|
+
`tar` untars things in the **current** directory!
|
412
|
+
|
413
|
+
After running the untar, you will find a new directory,
|
414
|
+
`~/tmp/wbzyl`, that is a copy of your home directory. Note that
|
415
|
+
the way you tar things up dictates the directory structure when
|
416
|
+
untarred. The fact that I mentioned `wbzyl` in the tar
|
417
|
+
creation means that I'll have that dir when untarred. In contrast,
|
418
|
+
the following will also make a copy of my home directory, but without
|
419
|
+
having a `parrt` root dir:
|
420
|
+
|
421
|
+
:::shell-unix-generic
|
422
|
+
cd ~wbzyl
|
423
|
+
tar cvf /tmp/wbzyl.backup.tar *
|
424
|
+
|
425
|
+
It is a good idea to tar things up with a root directory so that when
|
426
|
+
you untar you don't generate a million files in the current directly.
|
427
|
+
To see what's in a tarball, use
|
428
|
+
|
429
|
+
:::shell-unix-generic
|
430
|
+
tar ztvf /tmp/wbzyl.backup.tar.gz
|
431
|
+
|
432
|
+
If you have a big file to compress, use `gzip`:
|
433
|
+
|
434
|
+
:::shell-unix-generic
|
435
|
+
gzip bigfile
|
436
|
+
|
437
|
+
After execution, your file will have been renamed `bigfile.gz`.
|
438
|
+
To uncompress, use
|
439
|
+
|
440
|
+
:::shell-unix-generic
|
441
|
+
gzip -d bigfile.gz
|
442
|
+
|
443
|
+
To display a text file that is currently `gzip`'d, use `zcat`:
|
444
|
+
|
445
|
+
:::shell-unix-generic
|
446
|
+
zcat bigfile.gz
|
447
|
+
|
448
|
+
|
449
|
+
## Kopiowanie plików między komputerami
|
450
|
+
|
451
|
+
### rsync
|
452
|
+
|
453
|
+
When you need to have a directory on one machine mirrored on another
|
454
|
+
machine, use `rsync`. It compares all the files in a directory
|
455
|
+
subtree and copies over any that have changed to the mirrored
|
456
|
+
directory on the other machine. For example, here is how you could
|
457
|
+
"pull" all css screencasts files from `renia.local` to the box from
|
458
|
+
which you execute the `rsync` command:
|
459
|
+
|
460
|
+
:::shell-unix-generic
|
461
|
+
hostname -f
|
462
|
+
wlodek.local
|
463
|
+
rsync -raz -e ssh -v wlodek@renia.local:screencasts/css-tricks.com \
|
464
|
+
~/backup/
|
465
|
+
ls ~/backup/css-tricks.com/
|
466
|
+
VideoCast-57-css3.m4v
|
467
|
+
...
|
468
|
+
|
469
|
+
`rsync` will delete or truncate files to ensure the files stay
|
470
|
+
the same. This is bad if you erase a file by mistake--it will wipe
|
471
|
+
out your backup file. Change the options above to `-rabz` to
|
472
|
+
tell `rsync` to make a copy of any existing file before it
|
473
|
+
overwrites it. Or change a filename of any existing file:
|
474
|
+
|
475
|
+
:::shell-unix-generic
|
476
|
+
rsync -rabz -e ssh -v --suffix .rsync_`date '+%Y%m%d'` \
|
477
|
+
wlodek@renia.local:screencasts/css-tricks.com ~/backup/
|
478
|
+
|
479
|
+
where ``date '+%Y%m%d'`` (in reverse single quotes) means
|
480
|
+
"execute this `date` command".
|
481
|
+
|
482
|
+
To exclude certain patterns from the sync, use `--exclude`:
|
483
|
+
|
484
|
+
:::shell-unix-generic
|
485
|
+
rsync -rabz --exclude=tmp/ --suffix .rsync_`date '+%Y%m%d'` \
|
486
|
+
-e ssh -v wbzyl@renia.local:screencasts/css-tricks.com' ~/backup/
|
487
|
+
|
488
|
+
### scp
|
489
|
+
|
490
|
+
To copy a file or directory manually, use `scp`:
|
491
|
+
|
492
|
+
:::shell-unix-generic
|
493
|
+
scp lecture.html wbzyl@sigma.ug.edu.pl:public_html/
|
494
|
+
|
495
|
+
Just like `cp`, use `-r` to copy a directory recursively.
|
496
|
+
|
497
|
+
|
498
|
+
## Pozostałe programy
|
499
|
+
|
500
|
+
### find
|
501
|
+
|
502
|
+
Most GUIs for Linux or PCs have a search facility, but from the
|
503
|
+
command-line you can use `find`. To find all files with suffix
|
504
|
+
`.c` starting in directory `~/projects`, use:
|
505
|
+
|
506
|
+
:::shell-unix-generic
|
507
|
+
find ~/projects -name '.c'
|
508
|
+
|
509
|
+
The default "action" is to `-print`.
|
510
|
+
|
511
|
+
You can specify a globbing pattern to match. For example, to look
|
512
|
+
under your home directory for any xml files, use:
|
513
|
+
|
514
|
+
:::shell-unix-generic
|
515
|
+
find ~ -name '*.xml' -print
|
516
|
+
|
517
|
+
Note the use of the single quotes to prevent command-line
|
518
|
+
expansion--you want the '*' to go to the `find` command.
|
519
|
+
|
520
|
+
You can execute a command for every file or directory found that
|
521
|
+
matches a name. For example, do delete all xml files, do this:
|
522
|
+
|
523
|
+
:::shell-unix-generic
|
524
|
+
find ~ -name '*.xml' -exec rm {} \;
|
525
|
+
|
526
|
+
where "{}" stands for "current file that matches". The end of the
|
527
|
+
command must be terminated with ';' but because of the command-line
|
528
|
+
expansion, you'll need to escape the ';'.
|
529
|
+
|
530
|
+
You can also specify time information in your query. Here is a shell
|
531
|
+
script that uses `find` to delete all files older than 14 days.
|
532
|
+
|
533
|
+
:::shell-unix-generic
|
534
|
+
#!/bin/sh
|
535
|
+
BACKUP_DIR=$HOME/backup
|
536
|
+
# number of days to keep backups
|
537
|
+
AGE=14 # days
|
538
|
+
AGE_MINS=$[ $AGE * 60 * 24 ]
|
539
|
+
# delete dirs/files
|
540
|
+
find $BACKUP_DIR/* -cmin +$AGE_MINS -type d -exec rm -rf {} \;
|
541
|
+
|
542
|
+
### fuser
|
543
|
+
|
544
|
+
If you want to know who is using a port such as HTTP (80), use
|
545
|
+
`fuser`. You must be root to use this:
|
546
|
+
|
547
|
+
:::shell-unix-generic
|
548
|
+
sudo /sbin/fuser -n tcp 80
|
549
|
+
80/tcp: 1812 1884 1892 1898
|
550
|
+
|
551
|
+
The output indicates the list of processes associated with that port.
|
552
|
+
|
553
|
+
### whereis
|
554
|
+
|
555
|
+
Sometimes you want to use a command but it's not in your `PATH`
|
556
|
+
and you can't remember where it is. Use `whereis` to look in
|
557
|
+
standard unix locations for the command.
|
558
|
+
|
559
|
+
:::shell-unix-generic
|
560
|
+
whereis fuser
|
561
|
+
fuser: /sbin/fuser /usr/man/man1/fuser.1 /usr/man/man1/fuser.1.gz
|
562
|
+
whereis ls
|
563
|
+
ls: /bin/ls /usr/man/man1/ls.1 /usr/man/man1/ls.1.gz
|
564
|
+
|
565
|
+
`whereis` also shows `man` pages.
|
566
|
+
|
567
|
+
### which, type
|
568
|
+
|
569
|
+
Sometimes you might be executing the wrong version of a command and
|
570
|
+
you want to know which version of the command your `PATH`
|
571
|
+
indicates should be run. Use `which` to ask:
|
572
|
+
|
573
|
+
:::shell-unix-generic
|
574
|
+
which ls
|
575
|
+
alias ls='ls --color=tty'
|
576
|
+
/bin/ls
|
577
|
+
type ls
|
578
|
+
ls is aliased to `ls --color=auto'
|
579
|
+
|
580
|
+
If nothing is found in your path, you'll see:
|
581
|
+
|
582
|
+
:::shell-unix-generic
|
583
|
+
which fuser
|
584
|
+
/usr/bin/which: no fuser in (/usr/local/bin:/usr/X11R6/bin)
|
585
|
+
|
586
|
+
### kill, pkill
|
587
|
+
|
588
|
+
To send a signal to a process, use `kill`. Typically you'll
|
589
|
+
want to just say `kill pid` where `pid` can be found
|
590
|
+
from `ps` or `top` (see below).
|
591
|
+
|
592
|
+
Use `kill -9 pid` when you can't get the process to die;
|
593
|
+
this means kill it with "extreme prejudice".
|
594
|
+
|
595
|
+
### tracepath, traceroute
|
596
|
+
|
597
|
+
If you are having trouble getting to a site, use `traceroute`
|
598
|
+
to watch the sequence of hops used to get to a site:
|
599
|
+
|
600
|
+
:::shell-unix-generic
|
601
|
+
/usr/sbin/tracepath sigma.ug.edu.pl
|
602
|
+
1: wlodek.local 0.391ms pmtu 1500
|
603
|
+
...
|
604
|
+
5: do.gda-ar3.z.gda-r2.tpnet.pl (213.25.12.95) 10.854ms asymm 6
|
605
|
+
6: TP-tp-edu-gw.task.gda.pl (153.19.0.2) 42.373ms asymm 13
|
606
|
+
7: tp-jra10ge.task.gda.pl (153.19.252.33) 34.889ms asymm 12
|
607
|
+
8: area1-ug1-swr.task.gda.pl (153.19.254.228) 34.365ms asymm 12
|
608
|
+
9: sigma.ug.edu.pl (153.19.7.230) 36.318ms reached
|
609
|
+
Resume: pmtu 1500 hops 9 back 52
|
610
|
+
|
611
|
+
sudo /bin/traceroute sigma.ug.edu.pl
|
612
|
+
traceroute to sigma.ug.edu.pl (153.19.7.230), 30 hops max, 60 byte packets
|
613
|
+
1 wlodek.local 1.708 ms 2.529 ms 3.330 ms
|
614
|
+
...
|
615
|
+
5 do.gda-ar3.z.gda-r2.tpnet.pl (213.25.12.95) 21.905 ms 22.052 ms 30.636 ms
|
616
|
+
6 TP-tp-edu-gw.task.gda.pl (153.19.0.2) 42.546 ms 33.290 ms 33.401 ms
|
617
|
+
7 tp-jra10ge.task.gda.pl (153.19.252.33) 38.472 ms 38.876 ms 39.371 ms
|
618
|
+
8 area1-ug1-swr.task.gda.pl (153.19.254.228) 41.536 ms 41.720 ms 41.916 ms
|
619
|
+
9 sigma.ug.edu.pl (153.19.7.230) 39.618 ms 39.921 ms 40.236 ms
|
620
|
+
|
621
|
+
|
622
|
+
### Jaki jest mój adres IP?
|
623
|
+
|
624
|
+
:::shell-unix-generic
|
625
|
+
/sbin/ifconfig
|
626
|
+
|
627
|
+
or
|
628
|
+
|
629
|
+
:::shell-unix-generic
|
630
|
+
/sbin/ifconfig eth0
|
631
|
+
|
632
|
+
Under the `eth0` interface, you'll see the `inet addr`:
|
633
|
+
|
634
|
+
:::shell-unix-generic
|
635
|
+
eth0 Link encap:Ethernet HWaddr 00:0E:35:95:80:44
|
636
|
+
inet addr:192.188.100.1 Bcast:192.188.100.255 Mask:255.255.255.0
|
637
|
+
inet6 addr: fe00::40a:32ca:c950:1234/64 Scope:Link
|
638
|
+
|
639
|
+
|
640
|
+
## Użyteczne potoki: generowanie histogramu
|
641
|
+
|
642
|
+
A histogram is set of count, value pairs indicating how often the
|
643
|
+
value occurs. The basic operation will be to sort, then count how
|
644
|
+
many values occur in a row and then reverse sort so that the value
|
645
|
+
with the highest count is at the top of the report.
|
646
|
+
|
647
|
+
:::shell-unix-generic
|
648
|
+
... | sort |uniq -c|sort -r -n
|
649
|
+
|
650
|
+
Note that `sort` sorts on the whole line, but the first column
|
651
|
+
is obviously significant just as the first letter in someone's last
|
652
|
+
name significantly positions their name in a sorted list.
|
653
|
+
|
654
|
+
`uniq -c` collapses all repeated sequences of values but prints
|
655
|
+
the number of occurrences in front of the value. Recall the previous
|
656
|
+
sorting:
|
657
|
+
|
658
|
+
:::shell-unix-generic
|
659
|
+
cut -d ' ' -f 7 access_log | egrep '^/sp/' | \
|
660
|
+
sort | \
|
661
|
+
uniq
|
662
|
+
/sp/
|
663
|
+
/sp/exercises
|
664
|
+
/sp/git
|
665
|
+
/sp/images/alan_kay.jpg
|
666
|
+
/sp/images/alan_perlis.jpg
|
667
|
+
/sp/images/albert_einstein.jpg
|
668
|
+
...
|
669
|
+
|
670
|
+
Now add `-c` to `uniq`:
|
671
|
+
|
672
|
+
:::shell-unix-generic
|
673
|
+
cut -d ' ' -f 7 access_log | egrep '^/sp/' | \
|
674
|
+
sort | \
|
675
|
+
uniq -c
|
676
|
+
22 /sp/
|
677
|
+
6 /sp/exercises
|
678
|
+
2 /sp/git
|
679
|
+
1 /sp/images/alan_kay.jpg
|
680
|
+
2 /sp/images/alan_perlis.jpg
|
681
|
+
4 /sp/images/albert_einstein.jpg
|
682
|
+
...
|
683
|
+
|
684
|
+
Now all you have to do is reverse sort the lines according to the first column numerically.
|
685
|
+
|
686
|
+
:::shell-unix-generic
|
687
|
+
cut -d ' ' -f 7 access_log | egrep '^/sp/' | \
|
688
|
+
sort | \
|
689
|
+
uniq -c | \
|
690
|
+
sort -r -n
|
691
|
+
51 /sp/stylesheets/uv.css
|
692
|
+
51 /sp/stylesheets/sp.css
|
693
|
+
51 /sp/stylesheets/screen.css
|
694
|
+
51 /sp/stylesheets/print.css
|
695
|
+
50 /sp/stylesheets/icons/external.png
|
696
|
+
...
|