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