More updates to shell tutorial
[swc-modular-shell-hearing.git] / 1-Shell / Readme.md
1 # The Shell
2
3 [Back To The Menu](http://github.com/thehackerwithin/UofCSCBC2012/)
4 - [Forward to Python Variables](http://github.com/thehackerwithin/UofCSCBC2012/tree/master/2a-PythonVariables/)
5
6 * * * * *
7
8 **Presented By : Milad Fatenejad**
9
10 **Most of this material came from a presentation by: ??????**
11
12 # What is the shell how do I access the shell?
13
14 The *shell* is a program that presents a command line interface
15 which allows you to control your computer using commands entered
16 with a keyboard instead of controlling graphical user interfaces
17 (GUIs) with a mouse/keyboard combination.
18
19 A *terminal* is a program you run that gives you access to the
20 shell. There are many different terminal programs that vary across
21 operating systems.
22          
23 There are many reasons to learn about the shell. In my opinion, the
24 most important reasons are that: 
25
26 1.  It is very common to encounter the shell and
27     command-line-interfaces in scientific computing, so you will
28     probably have to learn it eventually 
29
30 2.  The shell is a really powerful way of interacting with your
31     computer. GUIs and the shell are complementary - by knowing both
32     you will greatly expand the range of tasks you can accomplish with
33     your computer. You will also be able to perform many tasks more
34     efficiently.
35
36 The shell is just a program and there are many different shell
37 programs that have been developed. The most common shell (and the one
38 we will use) is called the Bourne-Again SHell (bash). Even if bash is
39 not the default shell, it usually installed on most systems and can be
40 started by typing `bash` in the terminal. Many commands, especially a
41 lot of the basic ones, work across the various shells but many things
42 are different. I recommend sticking with bash and learning it well.
43
44 To open a terminal, just double click on the "Konsole" icon on the
45 Desktop.
46
47 # The Example: Manipulating Experimental Data Files
48
49 We will spend most of our time learning about the basics of the shell
50 by manipulating some experimental data from a hearing tests. To get
51 the data for this test, you will need internet access. Just enter the
52 command:
53
54     git clone git://github.com/thehackerwithin/UofCSCBC2012.git
55
56 This will grab all of the data needed for this workshop from the
57 internet.
58
59 **Cochlear Implants**
60
61 A cochlear implant is a small electronic device that is surgically
62 implanted in the inner ear to give deaf people a sense of
63 hearing. More than a quarter of a million people have them, but there
64 is still no widely-accepted benchmark to measure their effectiveness.
65 In order to establish a baseline for such a benchmark, our supervisor
66 got teenagers with CIs to listen to audio files on their computer and
67 report:
68
69 1.  the quietest sound they could hear
70 2.  the lowest and highest tones they could hear
71 3.  the narrowest range of frequencies they could discriminate
72
73 To participate, subjects attended our laboratory and one of our lab
74 techs played an audio sample, and recorded their data - when they
75 first heard the sound, or first heard a difference in the sound.  Each
76 set of test results were written out to a text file, one set per file.
77 Each participant has a unique subject ID, and a made-up subject name.
78 Each experiment has a unique experiment ID. The experiment has
79 collected 351 files so far.
80
81 The data is a bit of a mess! There are inconsistent file names, there
82 are extraneous "NOTES" files that we'd like to get rid of, and the
83 data is spread across many directories. We are going to use shell
84 commands to get this data into shape. By the end we would like to:
85
86 1.  Put all of the data into one directory called "alldata"
87
88 2.  Have all of the data files in there, and ensure that every file
89     has a ".txt" extension
90
91 3.  Get rid of the extraneous "NOTES" files
92
93 If we can get through this example in the available time, we will move
94 onto more advanced shell topics...
95
96 # Let's get started
97
98 One very basic command is `echo`. This command is just prints text to
99 the terminal. Try entering the command:
100
101     echo Hello, World
102
103 Then press enter. You should see the text "Hello, World" printed back
104 to you. The echo command is useful for 
105
106 ## Moving around the file system
107
108 Let's learn how to move around the file system using command line
109 programs. This is really easy to do using a GUI (just click on
110 things). Once you learn the basic commands, you'll see that it is
111 really easy to do in the shell too. 
112
113 First we have to know where we are. The program `pwd` (print working
114 directory) tells you where you are sitting in the directory tree. The
115 command `ls` will list the files in files in the current
116 directory. Directories are often called "folders" because of how they
117 are represented in GUIs. Directories are just listings of files. They
118 can contain other files or directories.
119
120 Whenever you start up a terminal, you will start in a special
121 directory called the *home* directory. Every user has their own home
122 directory where they have full access to do whatever they want. In
123 this case, the `pwd` command tells us that we are in the `/home/thw`
124 directory. This is the home directory for the `thw` user. That is our
125 user name. You can always find out your user name by entering the
126 command `whoami`. 
127
128 **File Types**
129
130 When you enter the `ls` command lists the contents of the current
131 directory. There are several items in the home directory, notice that
132 they are all colored blue. This tells us that all of these items are
133 directories as opposed to files.
134
135 Lets create an empty file using the `touch` command. Enter the
136 command:
137
138     touch testfile
139
140 Then list the contents of the directory again. You should see that a
141 new entry, called `testfile`, exists. It is colored white meaning that
142 it is a file, as opposed to a directory. The `touch` command just
143 creates an empty file. 
144
145 Some terminals will not color the directory entries in this very
146 convenient way. In those terminals, use `ls -F` instead of `ls`. The
147 `-F` argument modifies the results so that a slash is placed at the
148 end of directories. If the file is *executable* meaning that it can be
149 run like a program, then a star fill be placed of the file name.
150
151 You can also use the command `ls -l` to see whether items in a
152 directory are files or directories. `ls -l` gives a lot more
153 information too, such as the size of the file and information about
154 the owner. If the entry is a directory, then the first letter will be
155 a "d". The fifth column shows you the size of the entries in
156 bytes. Notice that `testfile` has a size of zero.
157
158 Now, let's get rid of `testfile`. To remove a file, just enter the
159 command:
160
161     rm testfile
162
163 The `rm` command can be used to remove files. If you enter `ls` again,
164 you will see that `testfile` is gone.
165
166
167 **Changing Directories**
168
169 Now, let's move to a different directory. The command `cd` (change
170 directory) is used to move around. Let's move into the `UofCSCBC2012`
171 directory. Enter the following command:
172
173     cd UofCSCBC2012
174
175 Now use the `ls` command to see what is inside this directory. You
176 will see that there is an entry which is green. This means that this
177 is an executable. If you use `ls -F` you will see that this file ends
178 with a star.
179
180 This directory contains all of the material for this boot camp. Now
181 move to the directory containing the data for the shell tutorial:
182
183     cd 1-Shell
184
185 If you enter the `cd` command by itself, you will return to the home
186 directory. Try this, and then navigate back to the `1-Shell`
187 directory.
188
189 ## Arguments
190
191 Most programs take additional arguments that control their exact
192 behavior. For example, `-F` and `-l` are arguments to `ls`.  The `ls`
193 program, like many programs, take a lot of arguments. But how do we
194 know what the options are to particular commands?
195
196 Most commonly used shell programs have a manual. You can access the
197 manual using the `man` program. Try entering:
198
199     man ls
200
201 This will open the manual page for `ls`. Use the space key to go
202 forward and b to go backwards. When you are done reading, just hit `q`
203 to exit.
204
205 Programs that are run from the shell can get extremely complicated. To
206 see an example, open up the manual page for the `mplayer` program,
207 which is command line driven video player. There are about 300
208 arguments to the mplayer command. No one can possibly learn all of
209 these arguments, of course. So you will probably find yourself
210 referring back to the manual page frequently.
211
212 **Examining the contents of other directories**
213
214 By default, the `ls` commands lists the contents of the working
215 directory (i.e. the directory you are in). You can always find the
216 directory you are in using the `pwd` command. However, you can also
217 give `ls` the names of other directories to view. Navigate to the
218 home directory if you are not already there. Then enter the
219 command:
220
221     ls UofCSCBC2012
222
223 This will list the contents of the `UofCSCBC2012` directory without
224 you having to navigate there. Now enter:
225
226     ls UofCSCBC2012/1-Shell
227
228 This prints the contents of `1-Shell`. The `cd` command works in a
229 similar way. Try entering:
230
231     cd UofCSCBC2012/1-Shell
232
233 and you will jump directly to `1-Shell` without having to go through
234 the intermediate directory.
235
236 ## Full vs. Relative Paths
237
238 The `cd` command takes an argument which is the directory
239 name. Directories can be specified using either a *relative* path a
240 full *path*. The directories on the computer are arranged into a
241 hierarchy. The full path tells you where a directory is in that
242 hierarchy. Navigate to the home directory. Now, enter the `pwd`
243 command and you should see:
244
245     /home/thw
246
247 which is the full name of your home directory. This tells you that you
248 are in a directory called `thw`, which sits inside a directory called
249 `home` which sits inside the very top directory in the hierarchy. The
250 very top of the hierarchy is a directory called `/` which is usually
251 referred to as the *root directory*. So, to summarize: `thw` is a
252 directory in `home` which is a directory in `/`.
253
254 Now enter the following command:
255
256     cd /home/thw/UofCSCBC2012/1-Shell
257
258 This jumps to `1-Shell`. Now go back to the home directory. We saw
259 earlier that the command:
260
261     cd UofCSCBC2012/1-Shell
262
263 had the same effect - it took us to the `1-Shell` directory. But,
264 instead of specifying the full path
265 (`/home/thw/UofCSCBC2012/1-Shell`), we specified a *relative path*. In
266 other words, we specified the path relative to our current
267 directory. A full path always starts with a `/`. A relative path does
268 not. You can usually use either a full path or a relative path
269 depending on what is most convenient. If we are in the home directory,
270 it is more convenient to just enter the relative path since it
271 involves less typing.
272
273 Now, list the contents of the /bin directory. Do you see anything
274 familiar in there?
275
276
277 ## Saving time with shortcuts, wild cards, and tab completion
278
279 **Shortcuts**
280
281 There are some shortcuts which you should know about. Dealing with the
282 home directory is very common. So, in the shell the tilde character,
283 `~`, is a shortcut for your home directory. Navigate to the `1-Shell`
284 directory, then enter the command:
285
286     ls ~
287
288 This prints the contents of your home directory, without you having to
289 type the full path. The shortcut `..` always refers to the directory
290 above your current directory. Thus: 
291
292     ls ..
293
294 prints the contents of the `/home/thw/UofCSCBC2012`. You can chain
295 these together, so:
296
297     ls ../../
298
299 prints the contents of `/home/thw` which is your home
300 directory. Finally, the special directory `.` always refers to your
301 current directory. So, `ls`, `ls .`, and `ls ././././.` all do the
302 same thing, they print the contents of the current directory. This may
303 seem like a useless shortcut right now, but we'll see when it is
304 needed in a little while.
305
306 To summarize, the commands `ls ~`, `ls ~/.`, `ls ../../`, and `ls
307 /home/thw` all do exactly the same thing. These shortcuts are not
308 necessary, they are provided for your convenience.
309
310 **Wild cards**
311
312 Navigate to the `~/UofCSCBC2012/Shell-1/data/THOMAS` directory. This
313 directory contains our hearing test data for THOMAS. If we type `ls`,
314 we will see that there are a bunch of files which are just four digit
315 numbers. By default, `ls` lists all of the files in a given
316 directory. The `*` character is a shortcut for "everything". Thus, if
317 you enter `ls *`, you will see all of the contents of a given
318 directory. Now try this command:
319
320     ls *1
321
322 This lists every file that ends with a `1`. This command:
323
324     ls /usr/bin/*.sh
325
326 Lists every file in `/usr/bin` that ends in the characters `.sh`. And
327 this command:
328
329     ls *4*1
330
331 lists every file in the current directory which contains the number
332 `4`, and ends with the number `1`. There are four such files: `0241`,
333 `0341`, `0431`, and `0481`. 
334
335 So how does this actually work? Well...when the shell (bash) sees a
336 word that contains the `*` character, it automatically looks for files
337 that match the given pattern. In this case, it identified four such
338 files. Then, it replaced the `*4*1` with the list of files, separated
339 by spaces. In other the two commands:
340
341     ls *4*1
342     ls 0241 0341 0431 0481
343
344 are exactly identical. The `ls` command cannot tell the difference
345 between these two things.
346
347 * * * *
348 **Short Exercise**
349
350 Do each of the following using a single `ls` command without
351 navigating to a different directory.
352
353 1.  List all of the files in `/bin` that contain the letter `a`
354 2.  List all of the files in `/bin` that contain the letter `a` or the letter `b`
355 3.  List all of the files in `/bin` that contain the letter `a` AND the letter `b`
356 * * * *
357
358 **Tab Completion**
359
360 Navigate to the home directory. Typing out directory names can waste a
361 lot of time. When you start typing out the name of a directory, then
362 hit the tab key, the shell will try to fill in the rest of the
363 directory name. For example, enter:
364
365     ls U<tab>
366
367 The shell will fill in the rest of the directory name for
368 `UofCSCBC2012`. Now enter:
369
370     ls D<tab><tab>
371
372 When you hit the first tab, nothing happens. The reason is that there
373 are multiple directories in the home directory which start with
374 D. Thus, the shell does not know which one to fill in. When you hit
375 tab again, the shell will list the possible choices. 
376
377 Tab completion can also fill in the names of programs. For example,
378 enter `e<tab><tab>`. You will see the name of every program that
379 starts with an `e`. One of those is `echo`. If you enter `ec<tab>` you
380 will see that tab completion works.
381
382 ## Which program? ##
383
384 Commands like `ls`, `rm`, `echo`, and `cd` are just ordinary programs
385 on the computer. A program is just a file that you can *execute*. The
386 program `which` tells you the location of a particular program. For
387 example:
388
389     which ls
390
391 Will return "/bin/ls". Thus, we can see that `ls` is a program that
392 sits inside of the `/bin` directory. Now enter:
393
394     which mplayer
395
396 You will see that `mplayer` is a program that sits inside of the
397 `/usr/bin` directory.
398
399 So ... when we enter a program name, like `ls`, and hit enter, how
400 does the shell know where to look for that program? How does it know
401 to run `/bin/ls` when we enter `ls`. The answer is that when we enter
402 a program name and hit enter, there are a few standard places that the
403 shell automatically looks. If it can't find the program in any of
404 those places, it will print an error saying "command not found". Enter
405 the command:
406
407     echo $PATH
408
409 This will print out the value of the `PATH` environment variable. More
410 on environment variables later. Notice that a list of directories,
411 separated by colon characters, is listed. These are the places the
412 shell looks for programs to run. If your program is not in this list,
413 then an error is printed. The shell ONLY checks in the places listed
414 in the `PATH` environment variable. 
415
416 Navigate to the `1-Shell` directory and list the contents. You will
417 notice that there is a program (executable file) called `hello` in
418 this directory. Now, try to run the program by entering:
419
420     hello
421
422 You should get an error saying that hello cannot be found. That is
423 because the directory `/home/thw/UofCSCBC2012/1-Shell` is not in the
424 `PATH`. You can run the `hello` program by entering:
425
426     ./hello
427
428 Remember that `.` is a shortcut for the current working
429 directory. This tells the shell to run the `hello` program which is
430 located right here. So, you can run any program by entering the path
431 to that program. You can run `hello` equally well by specifying:
432
433     /home/thw/UofCSCBC2012/1-Shell/hello
434
435 Or by entering:
436
437     ../1-Shell/hello
438
439 When there are no `/` characters, the shell assumes you want to look
440 in one of the default places for the program.
441
442
443 ## Examining Files
444
445 We now know how to switch directories, run programs, and look at the
446 contents of directories, but how do we look at the contents of files?
447
448 The easiest way to examine a file is to just print out all of the
449 contents using the program `cat`. Enter the following command:
450
451     cat ex_data.txt
452
453 This prints out the contents of the `ex_data.txt` file. If you enter:
454
455     cat ex_data.txt ex_data.txt
456
457 It will print out the contents of `ex_data.txt` twice. `cat` just
458 takes a list of file names and writes them out one after another (this
459 is where the name comes from, `cat` is short for concatenate). 
460
461 * * * *
462 **Short Exercises**
463
464 1.  Print out the contents of the `/usr/share/dict/american-english`
465     file. What does this file contain?
466
467 2.  Without changing directories, (you should still be in `1-Shell`),
468     use one short command to print the contents of all of the files in
469     the /home/milad/UofCSCBC2012/1-Shell/data/THOMAS directory.
470 * * * *
471
472 `cat` is a terrific program, but when the file is really big, it can
473 be annoying to use. The program, `less`, is useful for this
474 case. Enter the following command:
475
476     less /usr/share/dict/american-english
477
478 `less` opens the file, and lets you navigate through it. The commands
479 are identical to the `man` program. Use "space" to go forward and hit
480 the "b" key to go backwards. The "g" key goes to the beginning of the
481 file and "G" goes to the end. Finally, hit "q" to quit.
482
483 `less` also gives you a way of searching through files. Just hit the
484 "/" key to begin a search. Enter the name of the word you would like
485 to search for and hit enter. It will jump to the next location where
486 that word is found. Try searching the `american-english` file for the
487 word "copper". If you hit "/" then "enter", `less` will just repeat
488 the previous search. `less` searches from the current location and
489 works its way forward. If you are at the end of the file and search
490 for the word "copper", `less` will not find it. You need to go to the
491 beginning of the file and search.
492
493 Remember, the `man` program uses the same commands, so you can search
494 documentation using "/" as well!
495
496 * * * *
497 **Short Exercise**
498
499 Use the commands we've learned so far to figure out what the `-fs`
500 argument for the program `mplayer` does. `mplayer` video playing program.
501  * * * * 
502
503
504 ## Redirection
505
506 We now know a lot of the basic shell commands. Let's turn to the
507 experimental data from the hearing tests that we began with. This data
508 is located in the `~/UofCSCBC2012/1-Shell/data` directory. Each
509 subdirectory corresponds to a particular participant in the
510 study. Navigate to the `Bert` subdirectory in `data`.  There are a
511 bunch of text files which contain experimental data results. Lets
512 print them all:
513
514     cat au*
515
516 Now enter the following command:
517
518     cat au* > ../all_data
519
520 This tells the shell to take the output from the `cat au*` command and
521 dump it into a new file called `../all_data`. To verify that this
522 worked, examine the `all_data` file. If `all_data` had already
523 existed, we would overwritten it. So the `>`
524
525 # Extra Commands
526
527 ## The backtick, xargs
528
529 ## Some more common commands
530
531 **which**
532
533 **alias**
534
535 **touch**
536
537 **du**
538
539 ## .bashrc
540
541 ## ssh and scp
542
543 ## Regular Expressions
544
545 # Milad's Notes:
546
547 Don't we have to clone the repo?
548
549 Introduce less early - go over searching. 
550
551 # Background, Foreground, control-Z, control-C
552
553 ## Not everything is a file or a directory...
554 - Symbolic links
555 - /dev
556
557 ## Permissions
558
559 ## Variables