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