7af06a23dff227d38b073ec0cb363a8006fc2b67
[swc-modular-shell-hearing.git] / 1-Shell / Readme.md
1 # The Shell
2
3 [Back To The Menu](http://github.com/JHU-SWC-2012/SWC-bootcamp/)
4 - [Forward to Python Variables](http://github.com/JHU-SWC-2012/SWC-bootcamp/tree/master/2a-PythonVariables/)
5
6 * * * * *
7
8 **Presented By : Sasha Wood**
9
10 **This presentation originally developed by: Milad Fatenejad**
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/JHU-SWC-2012/SWC-bootcamp.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/swc`
124 directory. This is the home directory for the `swc` 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 `SWC-bootcamp`
171 directory. Enter the following command:
172
173     cd SWC-bootcamp
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 SWC-bootcamp
222
223 This will list the contents of the `SWC-bootcamp` directory without
224 you having to navigate there. Now enter:
225
226     ls SWC-bootcamp/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 SWC-bootcamp/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/swc
246
247 which is the full name of your home directory. This tells you that you
248 are in a directory called `swc`, 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: `swc` is a
252 directory in `home` which is a directory in `/`.
253
254 Now enter the following command:
255
256     cd /home/swc/SWC-bootcamp/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 SWC-bootcamp/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/swc/SWC-bootcamp/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/swc/SWC-bootcamp`. You can chain
295 these together, so:
296
297     ls ../../
298
299 prints the contents of `/home/swsc` 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/swc` 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 `~/SWC-bootcamp/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
359 **Tab Completion**
360
361 Navigate to the home directory. Typing out directory names can waste a
362 lot of time. When you start typing out the name of a directory, then
363 hit the tab key, the shell will try to fill in the rest of the
364 directory name. For example, enter:
365
366     ls U<tab>
367
368 The shell will fill in the rest of the directory name for
369 `SWC-bootcamp`. Now enter:
370
371     ls D<tab><tab>
372
373 When you hit the first tab, nothing happens. The reason is that there
374 are multiple directories in the home directory which start with
375 D. Thus, the shell does not know which one to fill in. When you hit
376 tab again, the shell will list the possible choices. 
377
378 Tab completion can also fill in the names of programs. For example,
379 enter `e<tab><tab>`. You will see the name of every program that
380 starts with an `e`. One of those is `echo`. If you enter `ec<tab>` you
381 will see that tab completion works.
382
383 ## Which program? ##
384
385 Commands like `ls`, `rm`, `echo`, and `cd` are just ordinary programs
386 on the computer. A program is just a file that you can *execute*. The
387 program `which` tells you the location of a particular program. For
388 example:
389
390     which ls
391
392 Will return "/bin/ls". Thus, we can see that `ls` is a program that
393 sits inside of the `/bin` directory. Now enter:
394
395     which mplayer
396
397 You will see that `mplayer` is a program that sits inside of the
398 `/usr/bin` directory.
399
400 So ... when we enter a program name, like `ls`, and hit enter, how
401 does the shell know where to look for that program? How does it know
402 to run `/bin/ls` when we enter `ls`. The answer is that when we enter
403 a program name and hit enter, there are a few standard places that the
404 shell automatically looks. If it can't find the program in any of
405 those places, it will print an error saying "command not found". Enter
406 the command:
407
408     echo $PATH
409
410 This will print out the value of the `PATH` environment variable. More
411 on environment variables later. Notice that a list of directories,
412 separated by colon characters, is listed. These are the places the
413 shell looks for programs to run. If your program is not in this list,
414 then an error is printed. The shell ONLY checks in the places listed
415 in the `PATH` environment variable. 
416
417 Navigate to the `1-Shell` directory and list the contents. You will
418 notice that there is a program (executable file) called `hello` in
419 this directory. Now, try to run the program by entering:
420
421     hello
422
423 You should get an error saying that hello cannot be found. That is
424 because the directory `/home/swc/SWC-bootcamp/1-Shell` is not in the
425 `PATH`. You can run the `hello` program by entering:
426
427     ./hello
428
429 Remember that `.` is a shortcut for the current working
430 directory. This tells the shell to run the `hello` program which is
431 located right here. So, you can run any program by entering the path
432 to that program. You can run `hello` equally well by specifying:
433
434     /home/swc/SWC-bootcamp/1-Shell/hello
435
436 Or by entering:
437
438     ../1-Shell/hello
439
440 When there are no `/` characters, the shell assumes you want to look
441 in one of the default places for the program.
442
443
444 ## Examining Files
445
446 We now know how to switch directories, run programs, and look at the
447 contents of directories, but how do we look at the contents of files?
448
449 The easiest way to examine a file is to just print out all of the
450 contents using the program `cat`. Enter the following command:
451
452     cat ex_data.txt
453
454 This prints out the contents of the `ex_data.txt` file. If you enter:
455
456     cat ex_data.txt ex_data.txt
457
458 It will print out the contents of `ex_data.txt` twice. `cat` just
459 takes a list of file names and writes them out one after another (this
460 is where the name comes from, `cat` is short for concatenate). 
461
462 * * * *
463 **Short Exercises**
464
465 1.  Print out the contents of the `/usr/share/dict/american-english`
466     file. What does this file contain?
467
468 2.  Without changing directories, (you should still be in `1-Shell`),
469     use one short command to print the contents of all of the files in
470     the /home/swc/SWC-bootcamp/1-Shell/data/THOMAS directory.
471
472 * * * *
473
474 `cat` is a terrific program, but when the file is really big, it can
475 be annoying to use. The program, `less`, is useful for this
476 case. Enter the following command:
477
478     less /usr/share/dict/american-english
479
480 `less` opens the file, and lets you navigate through it. The commands
481 are identical to the `man` program. Use "space" to go forward and hit
482 the "b" key to go backwards. The "g" key goes to the beginning of the
483 file and "G" goes to the end. Finally, hit "q" to quit.
484
485 `less` also gives you a way of searching through files. Just hit the
486 "/" key to begin a search. Enter the name of the word you would like
487 to search for and hit enter. It will jump to the next location where
488 that word is found. Try searching the `american-english` file for the
489 word "copper". If you hit "/" then "enter", `less` will just repeat
490 the previous search. `less` searches from the current location and
491 works its way forward. If you are at the end of the file and search
492 for the word "copper", `less` will not find it. You need to go to the
493 beginning of the file and search.
494
495 Remember, the `man` program uses the same commands, so you can search
496 documentation using "/" as well!
497
498 * * * *
499 **Short Exercise**
500
501 Use the commands we've learned so far to figure out what the `-fs`
502 argument for the program `mplayer` does. `mplayer` video playing program.
503
504 * * * * 
505
506
507 ## Redirection
508
509 Let's turn to the experimental data from the hearing tests that we
510 began with. This data is located in the `~/SWC-bootcamp/1-Shell/data`
511 directory. Each subdirectory corresponds to a particular participant
512 in the study. Navigate to the `Bert` subdirectory in `data`.  There
513 are a bunch of text files which contain experimental data
514 results. Lets print them all:
515
516     cat au*
517
518 Now enter the following command:
519
520     cat au* > ../all_data
521
522 This tells the shell to take the output from the `cat au*` command and
523 dump it into a new file called `../all_data`. To verify that this
524 worked, examine the `all_data` file. If `all_data` had already
525 existed, we would overwritten it. So the `>` character tells the shell
526 to take the output from what ever is on the left and dump it into the
527 file on the right. The `>>` characters do almost the same thing,
528 except that they will append the output to the file if it already
529 exists.
530
531 * * * *
532 **Short Exercise**
533
534 Use `>>`, to append the contents of all of the files which contain the
535 number 4 in the directory:
536
537     /home/swc/SWC-bootcamp/1-Shell/data/gerdal
538
539 to the existing `all_data` file. Thus, when you are done `all_data`
540 should contain all of the experiment data from Bert and any
541 experimental data file from gerdal that contains the number 4.
542
543 * * * *
544
545
546 ## Creating, moving, copying, and removing
547
548 We've created a file called `all_data` using the redirection operator
549 `>`. This is critical file so we have to make copies so that the data
550 is backed up. Lets copy the file using the `cp` command. The `cp`
551 command backs up the file. Navigate to the `data` directory and enter:
552
553     cp all_data all_data_backup
554
555 Now `all_data_backup` has been created as a copy of `all_data`. We can
556 move files around using the command `mv`. Enter this command:
557
558     mv all_data_backup /tmp/
559
560 This moves `all_data_backup` into the directory `/tmp`. The directory
561 `/tmp` is a special directory that all users can write to. It is a
562 temporary place for storing files. Data stored in `/tmp` is
563 automatically deleted when the computer shuts down.
564
565 The `mv` command is also how you rename files. Since this file is so
566 important, let's rename it:
567
568     mv all_data all_data_IMPORTANT
569
570 Now the file name has been changed to all_data_IMPORTANT. Let's delete
571 the backup file now:
572
573     rm /tmp/all_data_backup
574
575 The `mkdir` command is used to create a directory. Just enter `mkdir`
576 followed by a space, then the directory name. 
577
578 * * * *
579 **Short Exercise**
580
581 Do the following:
582
583 1.  Create a directory in the `data` directory called `foo`
584 2.  Then, copy the `all_data` file into `foo`
585
586 * * * *
587
588 By default, `rm`, will NOT delete directories. You can tell `rm` to
589 delete a directory using the `-r` option. Enter the following command:
590
591     rm -r foo
592
593
594 ## Count the words
595
596 The `wc` program (word count) counts the number of lines, words, and
597 characters in one or more files. Make sure you are in the `data`
598 directory, then enter the following command:
599
600     wc Bert/* gerdal/Data0559
601
602 For each of the files indicated, `wc` has printed a line with three
603 numbers. The first is the number of lines in that file. The second is
604 the number of words. Finally, the total number of characters is
605 indicated. The final line contains this information summed over all of
606 the files. Thus, there were 7062 characters in total. 
607
608 Remember that the `Bert/*` and `gerdal/Data0559` files were merged
609 into the `all_data` file. So, we should see that `all_data` contains
610 the same number of characters:
611
612     wc all_data
613
614 Every character in the file takes up one byte of disk space. Thus, the
615 size of the file in bytes should also be 7062. Let's confirm this:
616
617     ls -l all_data
618
619 Remember that `ls -l` prints out detailed information about a file and
620 that the fifth column is the size of the file in bytes.
621
622 * * * *
623 **Short Exercise**
624
625 Figure out how to get `wc` to print the length of the longest line in
626 `all_data`.
627
628 * * * *
629
630 ## The awesome power of the Pipe
631
632 Suppose I wanted to only see the total number of character, words, and
633 lines across the files `Bert/*` and `gerdal/Data0559`. I don't want to
634 see the individual counts, just the total. Of course, I could just do:
635
636     wc all_data
637
638 Since this file is a concatenation of the smaller files. Sure, this
639 works, but I had to create the `all_data` file to do this. Thus, I
640 have wasted a precious 7062 bytes of hard disk space. We can do this
641 *without* creating a temporary file, but first I have to show you two
642 more commands: `head` and `tail`. These commands print the first few,
643 or last few, lines of a file, respectively. Try them out on
644 `all_data`:
645
646     head all_data
647     tail all_data
648
649 The `-n` option to either of these commands can be used to print the
650 first or last `n` lines of a file. To print the first/last line of the
651 file use:
652
653     head -n 1 all_data
654     tail -n 1 all_data
655
656 Let's turn back to the problem of printing only the total number of
657 lines in a set of files without creating any temporary files. To do
658 this, we want to tell the shell to take the output of the `wc Bert/*
659 gerdal/Data0559` and send it into the `tail -n 1` command. The `|`
660 character (called pipe) is used for this purpose. Enter the following
661 command:
662
663     wc Bert/* gerdal/Data0559 | tail -n 1
664
665 This will print only the total number of lines, characters, and words
666 across all of these files. What is happening here? Well, `tail`, like
667 many command line programs will read from the *standard input* when it
668 is not given any files to operate on. In this case, it will just sit
669 there waiting for input. That input can come from the user's keyboard
670 *or from another program*. Try this:
671
672     tail -n 2
673
674 Notice that your cursor just sits there blinking. Tail is waiting for
675 data to come in. Now type:
676
677     French fries
678     are
679     good
680
681 then CONTROL+d. You should is the lines:
682
683     are
684     good
685
686 printed back at you. The CONTROL+d keyboard shortcut inserts an
687 *end-of-file* character. It is sort of the standard way of telling the
688 program "I'm done entering data". The `|` character is replaces the
689 data from the keyboard with data from another command. You can string
690 all sorts of commands together using the pipe. 
691
692 The philosophy behind these command line programs is that none of them
693 really do anything all that impressive. BUT when you start chaining
694 them together, you can do some really powerful things really
695 efficiently. If you want to be proficient at using the shell, you must
696 learn to become proficient with the pipe and redirection operators:
697 `|`, `>`, `>>`.
698
699
700 **A sorting example**
701
702 Let's create a file with some words to sort for the next example. We
703 want to create a file which contains the following names:
704
705     Bob
706     Alice
707     Diane
708     Charles
709
710 To do this, we need a program which allows us to create text
711 files. There are many such programs, the easiest one which is
712 installed on almost all systems is called `nano`. Navigate to `/tmp`
713 and enter the following command:
714
715     nano toBeSorted
716
717 Now enter the four names as shown above. When you are done, press
718 CONTROL+O to write out the file. Press enter to use the file name
719 `toBeSorted`. Then press CONTROL+x to exit `nano`.
720
721 When you are back to the command line, enter the command:
722
723     sort toBeSorted
724
725 Notice that the names are now printed in alphabetical order.
726
727 * * * *
728 **Short Exercise**
729
730 Use the `echo` command and the append operator, `>>`, to append your
731 name to the file, then sort it.
732
733 * * * *
734
735 Let's navigate back to `~/SWC-bootcamp/1-Shell/data`. You should still
736 have the `all_data` file hanging around here. Enter the following command:
737
738     wc Bert/* | sort -n -k 3
739
740 We are already familiar with what the first of these two commands
741 does: it creates a list containing the number of characters, words,
742 and lines in each file in the `Bert` directory. This list is then
743 piped into the `sort` command, so that it can be sorted. Notice there
744 are two options given to sort:
745
746 1.  `-n`: Sort in numerical order as opposed to alphabetical order
747 2.  `-k 3`: Sort based on the numbers in the third column
748
749 Notice that the files are sorted by the number of characters.
750
751 * * * *
752 **Short Exercise**
753
754 Use the `man` command to find out how to sort the output from `wc` in
755 reverse order.
756
757 * * * *
758
759 * * * * 
760 **Short Exercise**
761
762 Combine the `wc`, `sort`, `head` and `tail` commands so that only the
763 `wc` information for the largest file is listed
764
765 Hint: To print the smallest file, use:
766
767     wc Bert/* | sort -n -k 3 | head -n 1
768
769 * * * * 
770
771 Printing the smallest file seems pretty useful. We don't want to type
772 out that long command often. Let's create a simple script, a simple
773 program, to run this command. The program will look at all of the
774 files in the current directory and print the information about the
775 smallest one. Let's call the script `smallest`. We'll use `nano` to
776 create this file. Navigate to the `data` directory, then:
777
778     nano smallest
779
780 Then enter the following text:
781
782     #!/bin/bash
783     wc * | sort -n -k 3 | head -n 1
784
785 Now, `cd` into the `Bert` directory and enter the command
786 `../smallest`. Notice that it says permission denied. This happens
787 because we haven't told the shell that this is an executable
788 file. Enter the following commands:
789
790     chmod a+x ../smallest
791     ../smallest
792
793 The `chmod` command is used to modify the permissions of a file. This
794 particular command modifies the file `../smallest` by giving all users
795 (notice the `a`) permission to execute (notice the `x`) the file. If
796 you enter:
797
798     ls ../smallest
799
800 You will see that the file name is green. Congratulations, you just
801 created your first shell script!
802
803 # Searching files
804
805 You can search the contents of a file using the command `grep`. The
806 `grep` program is very powerful and useful especially when combined
807 with other commands by using the pipe. Navigate to the `Bert`
808 directory. Every data file in this directory has a line which says
809 "Range". The range represents the smallest frequency range that can be
810 discriminated. Lets list all of the ranges from the tests that Bert
811 conducted:
812
813     grep Range *
814
815 * * * * 
816 **Short Exercise**
817
818 Create an executable script called `smallestrange` in the `data`
819 directory, that is similar to the `smallest` script, but prints the
820 file containing the file with the smallest Range. Use the commands
821 `grep`, `sort`, and `tail` to do this.
822
823 * * * * 
824
825
826 # Finding files
827
828 The `find` program can be used to find files based on arbitrary
829 criteria. Navigate to the `data` directory and enter the following
830 command:
831
832     find .
833
834 This prints the name of every file or directory, recursively, starting
835 from the current directory. Let's exclude all of the directories:
836
837     find . -type f
838
839 This tells `find` to locate only files. Now try these commands:
840
841     find . -type f -name "*1*"
842     find . -type f -name "*1*" -or -name "*2*"
843     find . -type f -name "*1*" -and -name "*2*"
844
845 The `find` command can acquire a list of files and perform some
846 operation on each file. Try this command out:
847
848     find . -type f -exec grep Volume {} \;
849
850 This command finds every file starting from `.`. Then it searches each
851 file for a line which contains the word "Volume". The `{}` refers to
852 the name of each file. The trailing `\;` is used to terminate the
853 command.
854
855 * * * * 
856 **Short Exercise**
857
858 Navigate to the `data` directory. Use one find command to perform each
859 of the operations listed below (except number 2, which does not
860 require a find command):
861
862 1.  Find any file whose name is "NOTES" within `data` and delete it 
863
864 2.  Create a new directory called `cleaneddata`
865
866 3.  Move all of the files within `data` to the `cleaneddata` directory
867
868 4.  Rename all of the files to ensure that they end in `.txt` (note:
869     it is ok for the file name to end in `.txt.txt`
870
871 Hint: If you make a mistake and need to start over just do the
872 following:
873
874 1.  Navigate to the `1-Shell` directory
875
876 2.  Delete the `data` directory
877
878 3.  Enter the command: `git checkout -- data` You should see that the
879     data directory has reappeared in its original state
880
881 **BONUS**
882
883 Redo exercise 4, except rename only the files which do not already end
884 in `.txt`. You will have to use the `man` command to figure out how to
885 search for files which do not match a certain name. 
886
887 * * * * 
888
889
890
891 ## Bonus:
892
893 **backtick, xargs**: Example find all files with certain text
894
895 **alias** -> rm -i
896
897 **variables** -> use a path example
898
899 **.bashrc**
900
901 **du**
902
903 **ln**
904
905 **ssh and scp**
906
907 **Regular Expressions**
908
909 **Permissions**
910
911 **Chaining commands together**