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