More updates
[swc-modular-shell.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 and wild cards
278
279 **Shortcuts**
280 There are some shortcuts which you should know about. Dealing with the
281 home directory is very common. So, in the shell the tilde character,
282 `~`, is a shortcut for your home directory. Navigate to the `1-Shell`
283 directory, then enter the command:
284
285     ls ~
286
287 This prints the contents of your home directory, without you having to
288 type the full path. The shortcut `..` always refers to the directory
289 above your current directory. Thus: 
290
291     ls ..
292
293 prints the contents of the `/home/thw/UofCSCBC2012`. You can chain
294 these together, so:
295
296     ls ../../
297
298 prints the contents of `/home/thw` which is your home
299 directory. Finally, the special directory `.` always refers to your
300 current directory. So, `ls`, `ls .`, and `ls ././././.` all do the
301 same thing, they print the contents of the current directory. This may
302 seem like a useless shortcut right now, but we'll see when it is
303 needed in a little while.
304
305 To summarize, the commands `ls ~`, `ls ~/.`, `ls ../../`, and `ls
306 /home/thw` all do exactly the same thing. These shortcuts are not
307 necessary, they are provided for your convenience.
308
309
310
311 ## Examining Files
312
313 The easiest way to examine a file ...
314
315 * * * *
316 **Short Exercise**
317
318 Use the commands we've learned so far to figure out what the `-Wall`
319 argument for `gcc` does.
320 * * * *
321
322 # Extra Commands
323
324 ## The backtick, xargs
325
326 ## Some more common commands
327
328 **which**
329
330 **alias**
331
332 **touch**
333
334 **du**
335
336 ## .bashrc
337
338 ## ssh and scp
339
340 ## Regular Expressions
341
342 # Milad's Notes:
343
344 Don't we have to clone the repo?
345
346 Introduce less early - go over searching. 
347
348 # Background, Foreground, control-Z, control-C
349
350 ## Not everything is a file or a directory...
351 - Symbolic links
352 - /dev
353
354 ## Permissions
355
356 ## Variables