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