Fix one more s,Shell-1,shell,
[swc-modular-shell-hearing.git] / shell / Readme.md
1 # The Shell
2
3 [Back To The Setup](http://swcarpentry.github.com/boot-camps/2013-01-12-chicago/)
4 - [Forward to Python](https://github.com/thehackerwithin/boot-camps/tree/2013-01-chicago/python/intro)
5
6 * * * * *
7
8 **Updated and presented by : Radhika Khetani**
9
10 **Modified by : Sasha Wood**
11
12 **This presentation originally developed by: Milad Fatenejad**
13
14 # What is the shell how do I access the shell?
15
16 The *shell* is a program that presents a command line interface
17 which allows you to control your computer using commands entered
18 with a keyboard instead of controlling graphical user interfaces
19 (GUIs) with a mouse/keyboard combination.
20
21 Use the GUI to open the tutorial on github.  Single click on the "Firefox 
22 Web Browser".  Type in the URL:
23     github.com/thehackerwithin/boot-camps/tree/2013-01-chicago
24
25 Click on the directory named `shell`.
26
27 A *terminal* is a program you run that gives you access to the
28 shell. There are many different terminal programs that vary across
29 operating systems.
30          
31 There are many reasons to learn about the shell. In my opinion, the
32 most important reasons are that: 
33
34 1.  It is very common to encounter the shell and
35     command-line-interfaces in scientific computing, so you will
36     probably have to learn it eventually 
37
38 2.  The shell is a really powerful way of interacting with your
39     computer. GUIs and the shell are complementary - by knowing both
40     you will greatly expand the range of tasks you can accomplish with
41     your computer. You will also be able to perform many tasks more
42     efficiently.
43
44 The shell is just a program and there are many different shell
45 programs that have been developed. The most common shell (and the one
46 we will use) is called the Bourne-Again SHell (bash). Even if bash is
47 not the default shell, it is usually installed on most systems and can be
48 started by typing `bash` in the terminal. Many commands, especially a
49 lot of the basic ones, work across the various shells but many things
50 are different. I recommend sticking with bash and learning it well.
51 ([Here is a link for more information](http://en.wikipedia.org/wiki/Bash_(Unix_shell))
52
53 To open a terminal, just single click on the "Terminal" icon on the
54 Desktop.
55
56 # The Example: Manipulating Experimental Data Files
57
58 We will spend most of our time learning about the basics of the shell
59 by manipulating some experimental data from a hearing test. To get
60 the data for this test, you will need internet access. Just enter the
61 command:
62
63     git clone https://github.com/thehackerwithin/boot-camps.git
64
65 Followed by:
66
67     cd boot-camps
68     git checkout 2013-01-chicago
69
70 These 2 commands will grab all of the data needed for this workshop from the
71 internet.
72
73 # Let's get started
74
75 One very basic command is `echo`. This command just prints text to
76 the terminal. Try the command:
77
78     echo Hello, World
79
80 Then press enter. You should see the text "Hello, World" printed back
81 to you. The echo command is useful for printing from a shell script,
82 for displaying variables, and for generating known values to pass
83 to other programs.
84
85 ## Moving around the file system
86
87 Let's learn how to move around the file system using command line
88 programs. This is really easy to do using a GUI (just click on
89 things). Once you learn the basic commands, you'll see that it is
90 really easy to do in the shell too. 
91
92 First we have to know where we are. The program `pwd` (print working
93 directory) tells you where you are sitting in the directory tree. The
94 command `ls` will list the files in files in the current
95 directory. Directories are often called "folders" because of how they
96 are represented in GUIs. Directories are just listings of files. They
97 can contain other files or directories.
98
99 Whenever you start up a terminal, you will start in a special
100 directory called the *home* directory. Every user has their own home
101 directory where they have full access to do whatever they want. In
102 this case, the `pwd` command tells us that we are in the `/home/swc`
103 directory. This is the home directory for the `swc` user. That is our
104 user name. You can always find out your user name by entering the
105 command `whoami`. 
106
107 **File Types**
108
109 When you enter the `ls` command lists the contents of the current
110 directory. There are several items in the home directory, notice that
111 they are all colored blue. This tells us that all of these items are
112 directories as opposed to files.
113
114 Lets create an empty file using the `touch` command. Enter the
115 command:
116
117     touch testfile
118
119 Then list the contents of the directory again. You should see that a
120 new entry, called `testfile`, exists. It is colored white meaning that
121 it is a file, as opposed to a directory. The `touch` command just
122 creates an empty file. 
123
124 Some terminals will not color the directory entries in this very
125 convenient way. In those terminals, use `ls -F` instead of `ls`. The
126 `-F` argument modifies the results so that a slash is placed at the
127 end of directories. If the file is *executable* meaning that it can be
128 run like a program, then a star fill be placed of the file name.
129
130 You can also use the command `ls -l` to see whether items in a
131 directory are files or directories. `ls -l` gives a lot more
132 information too, such as the size of the file and information about
133 the owner. If the entry is a directory, then the first letter will be
134 a "d". The fifth column shows you the size of the entries in
135 bytes. Notice that `testfile` has a size of zero.
136
137 Now, let's get rid of `testfile`. To remove a file, just enter the
138 command:
139
140     rm testfile
141
142 The `rm` command can be used to remove files. If you enter `ls` again,
143 you will see that `testfile` is gone.
144
145
146 **Changing Directories**
147
148 Now, let's move to a different directory. The command `cd` (change
149 directory) is used to move around. Let's move into the `boot-camps`
150 directory. Enter the following command:
151
152     cd boot-camps
153
154 Now use the `ls` command to see what is inside this directory. You
155 will see that there is an entry which is green. This means that this
156 is an executable. If you use `ls -F` you will see that this file ends
157 with a star.
158
159 This directory contains all of the material for this boot camp. Now
160 move to the directory containing the data for the shell tutorial:
161
162     cd shell
163
164 If you enter the `cd` command by itself, you will return to the home
165 directory. Try this, and then navigate back to the `shell`
166 directory.
167
168 ## Arguments
169
170 Most programs take additional arguments that control their exact
171 behavior. For example, `-F` and `-l` are arguments to `ls`.  The `ls`
172 program, like many programs, take a lot of arguments. But how do we
173 know what the options are to particular commands?
174
175 Most commonly used shell programs have a manual. You can access the
176 manual using the `man` program. Try entering:
177
178     man ls
179
180 This will open the manual page for `ls`. Use the space key to go
181 forward and b to go backwards. When you are done reading, just hit `q`
182 to exit.
183
184 Programs that are run from the shell can get extremely complicated. To
185 see an example, open up the manual page for the `find` program,
186 which we will use later this session. No one can possibly learn all of
187 these arguments, of course. So you will probably find yourself
188 referring back to the manual page frequently.
189
190 **Examining the contents of other directories**
191
192 By default, the `ls` commands lists the contents of the working
193 directory (i.e. the directory you are in). You can always find the
194 directory you are in using the `pwd` command. However, you can also
195 give `ls` the names of other directories to view. Navigate to the
196 home directory if you are not already there. Then enter the
197 command:
198
199     ls boot-camps
200
201 This will list the contents of the `boot-camps` directory without
202 you having to navigate there. Now enter:
203
204     ls boot-camps/shell
205
206 This prints the contents of `shell`. The `cd` command works in a
207 similar way. Try entering:
208
209     cd boot-camps/shell
210
211 and you will jump directly to `shell` without having to go through
212 the intermediate directory.
213
214 ## Full vs. Relative Paths
215
216 The `cd` command takes an argument which is the directory
217 name. Directories can be specified using either a *relative* path a
218 full *path*. The directories on the computer are arranged into a
219 hierarchy. The full path tells you where a directory is in that
220 hierarchy. Navigate to the home directory. Now, enter the `pwd`
221 command and you should see:
222
223     /home/swc
224
225 which is the full name of your home directory. This tells you that you
226 are in a directory called `swc`, which sits inside a directory called
227 `home` which sits inside the very top directory in the hierarchy. The
228 very top of the hierarchy is a directory called `/` which is usually
229 referred to as the *root directory*. So, to summarize: `swc` is a
230 directory in `home` which is a directory in `/`.
231
232 Now enter the following command:
233
234     cd /home/swc/boot-camps/shell
235
236 This jumps to `shell`. Now go back to the home directory. We saw
237 earlier that the command:
238
239     cd boot-camps/shell
240
241 had the same effect - it took us to the `shell` directory. But,
242 instead of specifying the full path
243 (`/home/swc/boot-camps/shell`), we specified a *relative path*. In
244 other words, we specified the path relative to our current
245 directory. A full path always starts with a `/`. A relative path does
246 not. You can usually use either a full path or a relative path
247 depending on what is most convenient. If we are in the home directory,
248 it is more convenient to just enter the relative path since it
249 involves less typing.
250
251 Now, list the contents of the /bin directory. Do you see anything
252 familiar in there?
253
254
255 ## Saving time with shortcuts, wild cards, and tab completion
256
257 **Shortcuts**
258
259 There are some shortcuts which you should know about. Dealing with the
260 home directory is very common. So, in the shell the tilde character,
261 `~`, is a shortcut for your home directory. Navigate to the `shell`
262 directory, then enter the command:
263
264     ls ~
265
266 This prints the contents of your home directory, without you having to
267 type the full path. The shortcut `..` always refers to the directory
268 above your current directory. Thus: 
269
270     ls ..
271
272 prints the contents of the `/home/swc/boot-camps`. You can chain
273 these together, so:
274
275     ls ../../
276
277 prints the contents of `/home/swsc` which is your home
278 directory. Finally, the special directory `.` always refers to your
279 current directory. So, `ls`, `ls .`, and `ls ././././.` all do the
280 same thing, they print the contents of the current directory. This may
281 seem like a useless shortcut right now, but we'll see when it is
282 needed in a little while.
283
284 To summarize, the commands `ls ~`, `ls ~/.`, `ls ../../`, and `ls
285 /home/swc` all do exactly the same thing. These shortcuts are not
286 necessary, they are provided for your convenience.
287
288 **Our data set: Cochlear Implants**
289
290 A cochlear implant is a small electronic device that is surgically
291 implanted in the inner ear to give deaf people a sense of
292 hearing. More than a quarter of a million people have them, but there
293 is still no widely-accepted benchmark to measure their effectiveness.
294 In order to establish a baseline for such a benchmark, our supervisor
295 got teenagers with CIs to listen to audio files on their computer and
296 report:
297
298 1.  the quietest sound they could hear
299 2.  the lowest and highest tones they could hear
300 3.  the narrowest range of frequencies they could discriminate
301
302 To participate, subjects attended our laboratory and one of our lab
303 techs played an audio sample, and recorded their data - when they
304 first heard the sound, or first heard a difference in the sound.  Each
305 set of test results were written out to a text file, one set per file.
306 Each participant has a unique subject ID, and a made-up subject name.
307 Each experiment has a unique experiment ID. The experiment has
308 collected 351 files so far.
309
310 The data is a bit of a mess! There are inconsistent file names, there
311 are extraneous "NOTES" files that we'd like to get rid of, and the
312 data is spread across many directories. We are going to use shell
313 commands to get this data into shape. By the end we would like to:
314
315 1.  Put all of the data into one directory called "alldata"
316
317 2.  Have all of the data files in there, and ensure that every file
318     has a ".txt" extension
319
320 3.  Get rid of the extraneous "NOTES" files
321
322 If we can get through this example in the available time, we will move
323 onto more advanced shell topics...
324
325 **Wild cards**
326
327 Navigate to the `~/boot-camps/shell/data/THOMAS` directory. This
328 directory contains our hearing test data for THOMAS. If we type `ls`,
329 we will see that there are a bunch of files which are just four digit
330 numbers. By default, `ls` lists all of the files in a given
331 directory. The `*` character is a shortcut for "everything". Thus, if
332 you enter `ls *`, you will see all of the contents of a given
333 directory. Now try this command:
334
335     ls *1
336
337 This lists every file that ends with a `1`. This command:
338
339     ls /usr/bin/*.sh
340
341 Lists every file in `/usr/bin` that ends in the characters `.sh`. And
342 this command:
343
344     ls *4*1
345
346 lists every file in the current directory which contains the number
347 `4`, and ends with the number `1`. There are four such files: `0241`,
348 `0341`, `0431`, and `0481`. 
349
350 So how does this actually work? Well...when the shell (bash) sees a
351 word that contains the `*` character, it automatically looks for files
352 that match the given pattern. In this case, it identified four such
353 files. Then, it replaced the `*4*1` with the list of files, separated
354 by spaces. In other the two commands:
355
356     ls *4*1
357     ls 0241 0341 0431 0481
358
359 are exactly identical. The `ls` command cannot tell the difference
360 between these two things.
361
362 * * * *
363 **Short Exercise**
364
365 Do each of the following using a single `ls` command without
366 navigating to a different directory.
367
368 1.  List all of the files in `/bin` that contain the letter `a`
369 2.  List all of the files in `/bin` that contain the letter `a` or the letter `b`
370 3.  List all of the files in `/bin` that contain the letter `a` AND the letter `b`
371
372 * * * *
373
374 **Tab Completion**
375
376 Navigate to the home directory. Typing out directory names can waste a
377 lot of time. When you start typing out the name of a directory, then
378 hit the tab key, the shell will try to fill in the rest of the
379 directory name. For example, enter:
380
381     cd S<tab>
382
383 The shell will fill in the rest of the directory name for
384 `boot-camps`. Now enter:
385
386     ls 3<tab><tab>
387
388 When you hit the first tab, nothing happens. The reason is that there
389 are multiple directories in the home directory which start with
390 3. Thus, the shell does not know which one to fill in. When you hit
391 tab again, the shell will list the possible choices. 
392
393 Tab completion can also fill in the names of programs. For example,
394 enter `e<tab><tab>`. You will see the name of every program that
395 starts with an `e`. One of those is `echo`. If you enter `ec<tab>` you
396 will see that tab completion works.
397
398 ** Command History**
399
400 You can easily access previous commands.  Hit the up arrow.  
401 Hit it again.  You can step backwards through your command history. 
402 The down arrow takes your forwards in the command history.  
403
404 ^-C will cancel the command you are writing, and give you a fresh prompt.
405
406 ^-R will do a reverse-search through your command history.  This
407 is very useful.
408
409 ## Which program? ##
410
411 Commands like `ls`, `rm`, `echo`, and `cd` are just ordinary programs
412 on the computer. A program is just a file that you can *execute*. The
413 program `which` tells you the location of a particular program. For
414 example:
415
416     which ls
417
418 Will return "/bin/ls". Thus, we can see that `ls` is a program that
419 sits inside of the `/bin` directory. Now enter:
420
421     which find
422
423 You will see that `find` is a program that sits inside of the
424 `/usr/bin` directory.
425
426 So ... when we enter a program name, like `ls`, and hit enter, how
427 does the shell know where to look for that program? How does it know
428 to run `/bin/ls` when we enter `ls`. The answer is that when we enter
429 a program name and hit enter, there are a few standard places that the
430 shell automatically looks. If it can't find the program in any of
431 those places, it will print an error saying "command not found". Enter
432 the command:
433
434     echo $PATH
435
436 This will print out the value of the `PATH` environment variable. More
437 on environment variables later. Notice that a list of directories,
438 separated by colon characters, is listed. These are the places the
439 shell looks for programs to run. If your program is not in this list,
440 then an error is printed. The shell ONLY checks in the places listed
441 in the `PATH` environment variable. 
442
443 Navigate to the `shell` directory and list the contents. You will
444 notice that there is a program (executable file) called `hello` in
445 this directory. Now, try to run the program by entering:
446
447     hello
448
449 You should get an error saying that hello cannot be found. That is
450 because the directory `/home/swc/boot-camps/shell` is not in the
451 `PATH`. You can run the `hello` program by entering:
452
453     ./hello
454
455 Remember that `.` is a shortcut for the current working
456 directory. This tells the shell to run the `hello` program which is
457 located right here. So, you can run any program by entering the path
458 to that program. You can run `hello` equally well by specifying:
459
460     /home/swc/boot-camps/shell/hello
461
462 Or by entering:
463
464     ../shell/hello
465
466 When there are no `/` characters, the shell assumes you want to look
467 in one of the default places for the program.
468
469
470 ## Examining Files
471
472 We now know how to switch directories, run programs, and look at the
473 contents of directories, but how do we look at the contents of files?
474
475 The easiest way to examine a file is to just print out all of the
476 contents using the program `cat`. Enter the following command:
477
478     cat ex_data.txt
479
480 This prints out the contents of the `ex_data.txt` file. If you enter:
481
482     cat ex_data.txt ex_data.txt
483
484 It will print out the contents of `ex_data.txt` twice. `cat` just
485 takes a list of file names and writes them out one after another (this
486 is where the name comes from, `cat` is short for concatenate). 
487
488 * * * *
489 **Short Exercises**
490
491 1.  Print out the contents of the `~/boot-camps/shell/dictionary.txt`
492     file. What does this file contain?
493
494 2.  Without changing directories, (you should still be in `shell`),
495     use one short command to print the contents of all of the files in
496     the `/home/swc/boot-camps/shell/data/THOMAS` directory.
497
498 * * * *
499
500 `cat` is a terrific program, but when the file is really big, it can
501 be annoying to use. The program, `less`, is useful for this
502 case. Enter the following command:
503
504     less ~/boot-camps/shell/dictionary.txt
505
506 `less` opens the file, and lets you navigate through it. The commands
507 are identical to the `man` program. Use "space" to go forward and hit
508 the "b" key to go backwards. The "g" key goes to the beginning of the
509 file and "G" goes to the end. Finally, hit "q" to quit.
510
511 `less` also gives you a way of searching through files. Just hit the
512 "/" key to begin a search. Enter the name of the word you would like
513 to search for and hit enter. It will jump to the next location where
514 that word is found. Try searching the `dictionary.txt` file for the
515 word "cat". If you hit "/" then "enter", `less` will just repeat
516 the previous search. `less` searches from the current location and
517 works its way forward. If you are at the end of the file and search
518 for the word "cat", `less` will not find it. You need to go to the
519 beginning of the file and search.
520
521 Remember, the `man` program uses the same commands, so you can search
522 documentation using "/" as well!
523
524 * * * *
525 **Short Exercise**
526
527 Use the commands we've learned so far to figure out how to search
528 in reverse while using `less`.
529
530 * * * * 
531
532
533 ## Redirection
534
535 Let's turn to the experimental data from the hearing tests that we
536 began with. This data is located in the `~/boot-camps/shell/data`
537 directory. Each subdirectory corresponds to a particular participant
538 in the study. Navigate to the `Bert` subdirectory in `data`.  There
539 are a bunch of text files which contain experimental data
540 results. Lets print them all:
541
542     cat au*
543
544 Now enter the following command:
545
546     cat au* > ../all_data
547
548 This tells the shell to take the output from the `cat au*` command and
549 dump it into a new file called `../all_data`. To verify that this
550 worked, examine the `all_data` file. If `all_data` had already
551 existed, we would overwritten it. So the `>` character tells the shell
552 to take the output from what ever is on the left and dump it into the
553 file on the right. The `>>` characters do almost the same thing,
554 except that they will append the output to the file if it already
555 exists.
556
557 * * * *
558 **Short Exercise**
559
560 Use `>>`, to append the contents of all of the files which contain the
561 number 4 in the directory:
562
563     /home/swc/boot-camps/shell/data/gerdal
564
565 to the existing `all_data` file. Thus, when you are done `all_data`
566 should contain all of the experiment data from Bert and any
567 experimental data file from gerdal that contains the number 4.
568
569 * * * *
570
571
572 ## Creating, moving, copying, and removing
573
574 We've created a file called `all_data` using the redirection operator
575 `>`. This is critical file so we have to make copies so that the data
576 is backed up. Lets copy the file using the `cp` command. The `cp`
577 command backs up the file. Navigate to the `data` directory and enter:
578
579     cp all_data all_data_backup
580
581 Now `all_data_backup` has been created as a copy of `all_data`. We can
582 move files around using the command `mv`. Enter this command:
583
584     mv all_data_backup /tmp/
585
586 This moves `all_data_backup` into the directory `/tmp`. The directory
587 `/tmp` is a special directory that all users can write to. It is a
588 temporary place for storing files. Data stored in `/tmp` is
589 automatically deleted when the computer shuts down.
590
591 The `mv` command is also how you rename files. Since this file is so
592 important, let's rename it:
593
594     mv all_data all_data_IMPORTANT
595
596 Now the file name has been changed to all_data_IMPORTANT. Let's delete
597 the backup file now:
598
599     rm /tmp/all_data_backup
600
601 The `mkdir` command is used to create a directory. Just enter `mkdir`
602 followed by a space, then the directory name. 
603
604 * * * *
605 **Short Exercise**
606
607 Do the following:
608
609 1.  Rename the `all_data_IMPORTANT` file to `all_data`.
610 2.  Create a directory in the `data` directory called `foo`
611 3.  Then, copy the `all_data` file into `foo`
612
613 * * * *
614
615 By default, `rm`, will NOT delete directories. You can tell `rm` to
616 delete a directory using the `-r` option. Enter the following command:
617
618     rm -r foo
619
620
621 ## Count the words
622
623 The `wc` program (word count) counts the number of lines, words, and
624 characters in one or more files. Make sure you are in the `data`
625 directory, then enter the following command:
626
627     wc Bert/* gerdal/*4*
628
629 For each of the files indicated, `wc` has printed a line with three
630 numbers. The first is the number of lines in that file. The second is
631 the number of words. Finally, the total number of characters is
632 indicated. The final line contains this information summed over all of
633 the files. Thus, there were 10445 characters in total. 
634
635 Remember that the `Bert/*` and `gerdal/*4*` files were merged
636 into the `all_data` file. So, we should see that `all_data` contains
637 the same number of characters:
638
639     wc all_data
640
641 Every character in the file takes up one byte of disk space. Thus, the
642 size of the file in bytes should also be 10445. Let's confirm this:
643
644     ls -l all_data
645
646 Remember that `ls -l` prints out detailed information about a file and
647 that the fifth column is the size of the file in bytes.
648
649 * * * *
650 **Short Exercise**
651
652 Figure out how to get `wc` to print the length of the longest line in
653 `all_data`.
654
655 * * * *
656
657 ## The awesome power of the Pipe
658
659 Suppose I wanted to only see the total number of character, words, and
660 lines across the files `Bert/*` and `gerdal/*4*`. I don't want to
661 see the individual counts, just the total. Of course, I could just do:
662
663     wc all_data
664
665 Since this file is a concatenation of the smaller files. Sure, this
666 works, but I had to create the `all_data` file to do this. Thus, I
667 have wasted a precious 7062 bytes of hard disk space. We can do this
668 *without* creating a temporary file, but first I have to show you two
669 more commands: `head` and `tail`. These commands print the first few,
670 or last few, lines of a file, respectively. Try them out on
671 `all_data`:
672
673     head all_data
674     tail all_data
675
676 The `-n` option to either of these commands can be used to print the
677 first or last `n` lines of a file. To print the first/last line of the
678 file use:
679
680     head -n 1 all_data
681     tail -n 1 all_data
682
683 Let's turn back to the problem of printing only the total number of
684 lines in a set of files without creating any temporary files. To do
685 this, we want to tell the shell to take the output of the `wc Bert/*
686 gerdal/*4*` and send it into the `tail -n 1` command. The `|`
687 character (called pipe) is used for this purpose. Enter the following
688 command:
689
690     wc Bert/* gerdal/Data0559 | tail -n 1
691
692 This will print only the total number of lines, characters, and words
693 across all of these files. What is happening here? Well, `tail`, like
694 many command line programs will read from the *standard input* when it
695 is not given any files to operate on. In this case, it will just sit
696 there waiting for input. That input can come from the user's keyboard
697 *or from another program*. Try this:
698
699     tail -n 2
700
701 Notice that your cursor just sits there blinking. Tail is waiting for
702 data to come in. Now type:
703
704     French
705     fries
706     are
707     good
708
709 then CONTROL+d. You should is the lines:
710
711     are
712     good
713
714 printed back at you. The CONTROL+d keyboard shortcut inserts an
715 *end-of-file* character. It is sort of the standard way of telling the
716 program "I'm done entering data". The `|` character is replaces the
717 data from the keyboard with data from another command. You can string
718 all sorts of commands together using the pipe. 
719
720 The philosophy behind these command line programs is that none of them
721 really do anything all that impressive. BUT when you start chaining
722 them together, you can do some really powerful things really
723 efficiently. If you want to be proficient at using the shell, you must
724 learn to become proficient with the pipe and redirection operators:
725 `|`, `>`, `>>`.
726
727
728 **A sorting example**
729
730 Let's create a file with some words to sort for the next example. We
731 want to create a file which contains the following names:
732
733     Bob
734     Alice
735     Diane
736     Charles
737
738 To do this, we need a program which allows us to create text
739 files. There are many such programs, the easiest one which is
740 installed on almost all systems is called `nano`. Navigate to `/tmp`
741 and enter the following command:
742
743     nano toBeSorted
744
745 Now enter the four names as shown above. When you are done, press
746 CONTROL+O to write out the file. Press enter to use the file name
747 `toBeSorted`. Then press CONTROL+x to exit `nano`.
748
749 When you are back to the command line, enter the command:
750
751     sort toBeSorted
752
753 Notice that the names are now printed in alphabetical order.
754
755 * * * *
756 **Short Exercise**
757
758 Use the `echo` command and the append operator, `>>`, to append your
759 name to the file, then sort it and make a new file called Sorted.
760
761 * * * *
762
763 Let's navigate back to `~/boot-camps/shell/data`. Enter the following command:
764
765     wc Bert/* | sort -k 3 -n
766
767 We are already familiar with what the first of these two commands
768 does: it creates a list containing the number of characters, words,
769 and lines in each file in the `Bert` directory. This list is then
770 piped into the `sort` command, so that it can be sorted. Notice there
771 are two options given to sort:
772
773 1.  `-k 3`: Sort based on the third column
774 2.  `-n`: Sort in numerical order as opposed to alphabetical order
775
776 Notice that the files are sorted by the number of characters.
777
778 * * * *
779 **Short Exercise**
780
781 Use the `man` command to find out how to sort the output from `wc` in
782 reverse order.
783
784 * * * *
785
786 * * * * 
787 **Short Exercise**
788
789 Combine the `wc`, `sort`, `head` and `tail` commands so that only the
790 `wc` information for the largest file is listed
791
792 Hint: To print the smallest file, use:
793
794     wc Bert/* | sort -k 3 -n | head -n 1
795
796 * * * * 
797
798 Printing the smallest file seems pretty useful. We don't want to type
799 out that long command often. Let's create a simple script, a simple
800 program, to run this command. The program will look at all of the
801 files in the current directory and print the information about the
802 smallest one. Let's call the script `smallest`. We'll use `nano` to
803 create this file. Navigate to the `data` directory, then:
804
805     nano smallest
806
807 Then enter the following text:
808
809     #!/bin/bash
810     wc * | sort -k 3 -n | head -n 1
811
812 Now, `cd` into the `Bert` directory and enter the command
813 `../smallest`. Notice that it says permission denied. This happens
814 because we haven't told the shell that this is an executable
815 file. If you do `ls -l ../smallest`, it will show you the permissions on 
816 the left of the listing.
817
818 Enter the following commands:
819
820     chmod a+x ../smallest
821     ../smallest
822
823 The `chmod` command is used to modify the permissions of a file. This
824 particular command modifies the file `../smallest` by giving all users
825 (notice the `a`) permission to execute (notice the `x`) the file. If
826 you enter:
827
828     ls -l ../smallest
829
830 You will see that the file name is green and the permissions have changed. 
831 Congratulations, you just created your first shell script!
832
833 # Searching files
834
835 You can search the contents of a file using the command `grep`. The
836 `grep` program is very powerful and useful especially when combined
837 with other commands by using the pipe. Navigate to the `Bert`
838 directory. Every data file in this directory has a line which says
839 "Range". The range represents the smallest frequency range that can be
840 discriminated. Lets list all of the ranges from the tests that Bert
841 conducted:
842
843     grep Range *
844
845 * * * * 
846 **Short Exercise**
847
848 Create an executable script called `smallestrange` in the `data`
849 directory, that is similar to the `smallest` script, but prints the
850 file containing the file with the smallest Range. Use the commands
851 `grep`, `sort`, and `tail` to do this.
852
853 * * * * 
854
855
856 # Finding files
857
858 The `find` program can be used to find files based on arbitrary
859 criteria. Navigate to the `data` directory and enter the following
860 command:
861
862     find . -print
863
864 This prints the name of every file or directory, recursively, starting
865 from the current directory. Let's exclude all of the directories:
866
867     find . -type f -print
868
869 This tells `find` to locate only files. Now try these commands:
870
871     find . -type f -name "*1*"
872     find . -type f -name "*1*" -or -name "*2*" -print
873     find . -type f -name "*1*" -and -name "*2*" -print
874
875 The `find` command can acquire a list of files and perform some
876 operation on each file. Try this command out:
877
878     find . -type f -exec grep Volume {} \;
879
880 This command finds every file starting from `.`. Then it searches each
881 file for a line which contains the word "Volume". The `{}` refers to
882 the name of each file. The trailing `\;` is used to terminate the
883 command.  This command is slow, because it is calling a new instance
884 of `grep` for each item the `find` returns.
885
886 A faster way to do this is to use the `xargs` command:
887
888     find . -type f -print | xargs grep Volume
889
890 `find` generates a list of all the files we are interested in, 
891 then we pipe them to `xargs`.  `xargs` takes the items given to it 
892 and passes them as arguments to `grep`.  `xargs` generally only creates
893 a single instance of `grep` (or whatever program it is running).
894
895 * * * * 
896 **Short Exercise**
897
898 Navigate to the `data` directory. Use one find command to perform each
899 of the operations listed below (except number 2, which does not
900 require a find command):
901
902 1.  Find any file whose name is "NOTES" within `data` and delete it 
903
904 2.  Create a new directory called `cleaneddata`
905
906 3.  Move all of the files within `data` to the `cleaneddata` directory
907
908 4.  Rename all of the files to ensure that they end in `.txt` (note:
909     it is ok for the file name to end in `.txt.txt`
910
911 Hint: If you make a mistake and need to start over just do the
912 following:
913
914 1.  Navigate to the `shell` directory
915
916 2.  Delete the `data` directory
917
918 3.  Enter the command: `git checkout -- data` You should see that the
919     data directory has reappeared in its original state
920
921 **BONUS**
922
923 Redo exercise 4, except rename only the files which do not already end
924 in `.txt`. You will have to use the `man` command to figure out how to
925 search for files which do not match a certain name. 
926
927 * * * * 
928
929
930
931 ## Bonus:
932
933 **backtick, xargs**: Example find all files with certain text
934
935 **alias** -> rm -i
936
937 **variables** -> use a path example
938
939 **.bashrc**
940
941 **du**
942
943 **ln**
944
945 **ssh and scp**
946
947 **Regular Expressions**
948
949 **Permissions**
950
951 **Chaining commands together**