3 **Material by Paul Wilson, Milad Fatenejad, Sasha Wood, and Radhika Khetani**
5 # What is the shell? How do I access the shell?
7 The *shell* is a program that presents a command line interface
8 which allows you to control your computer using commands entered
9 with a keyboard instead of controlling graphical user interfaces
10 (GUIs) with a mouse/keyboard combination.
12 Use a browser to open the tutorial on github, located at:
13 https://github.com/USERNAME/boot-camps/tree/YYYY-MM-PLACE
15 Click on the directory named `shell`.
17 A *terminal* is a program you run that gives you access to the
18 shell. There are many different terminal programs that vary across
21 There are many reasons to learn about the shell. In my opinion, the
22 most important reasons are that:
24 1. It is very common to encounter the shell and
25 command-line-interfaces in scientific computing, so you will
26 probably have to learn it eventually
28 2. The shell is a really powerful way of interacting with your
29 computer. GUIs and the shell are complementary - by knowing both
30 you will greatly expand the range of tasks you can accomplish with
31 your computer. You will also be able to perform many tasks more
34 The shell is just a program and there are many different shell
35 programs that have been developed. The most common shell (and the one
36 we will use) is called the Bourne-Again SHell (bash). Even if bash is
37 not the default shell, it is usually installed on most systems and can be
38 started by typing `bash` in the terminal. Many commands, especially a
39 lot of the basic ones, work across the various shells but many things
40 are different. I recommend sticking with bash and learning it well.
41 ([Here is a link for more information](http://en.wikipedia.org/wiki/Bash_(Unix_shell))
43 To open a terminal, just single click on the "Terminal" icon on the
46 # The Example: Manipulating Experimental Data Files
48 We will spend most of our time learning about the basics of the shell
49 by manipulating some experimental data from a hearing test. To get
50 the data for this test, you will need internet access. Just enter the
53 git clone -b YYYY-MM-PLACE https://github.com/USERNAME/boot-camps.git
55 This command will grab all of the data needed for this workshop from
56 the internet. (We will talk about the `git` command later in the
61 One very basic command is `echo`. This command just prints text to
62 the terminal. Try the command:
66 Then press enter. You should see the text "Hello, World" printed back
67 to you. The echo command is useful for printing from a shell script,
68 for displaying variables, and for generating known values to pass
71 ## Moving around the file system
73 Let's learn how to move around the file system using command line
74 programs. This is really easy to do using a GUI (just click on
75 things). Once you learn the basic commands, you'll see that it is
76 really easy to do in the shell too.
78 First we have to know where we are. The program `pwd` (print working
79 directory) tells you where you are sitting in the directory tree. The
80 command `ls` will list the files in files in the current
81 directory. Directories are often called "folders" because of how they
82 are represented in GUIs. Directories are just listings of files. They
83 can contain other files or directories.
85 Whenever you start up a terminal, you will start in a special
86 directory called the *home* directory. Every user has their own home
87 directory where they have full access to do whatever they want. In
88 this case, the `pwd` command tells us that we are in the `/home/swc`
89 directory. This is the home directory for the `swc` user. That is our
90 user name. You can always find out your user name by entering the
95 When you enter the `ls` command lists the contents of the current
96 directory. There are several items in the home directory, notice that
97 they are all colored blue. This tells us that all of these items are
98 directories as opposed to files.
100 Lets create an empty file using the `touch` command. Enter the
105 Then list the contents of the directory again. You should see that a
106 new entry, called `testfile`, exists. It is colored white meaning that
107 it is a file, as opposed to a directory. The `touch` command just
108 creates an empty file.
110 Some terminals will not color the directory entries in this very
111 convenient way. In those terminals, use `ls -F` instead of `ls`. The
112 `-F` argument modifies the results so that a slash is placed at the
113 end of directories. If the file is *executable* meaning that it can be
114 run like a program, then a star will be placed at the end of of the
117 You can also use the command `ls -l` to see whether items in a
118 directory are files or directories. `ls -l` gives a lot more
119 information too, such as the size of the file and information about
120 the owner. If the entry is a directory, then the first letter will be
121 a "d". The fifth column shows you the size of the entries in
122 bytes. Notice that `testfile` has a size of zero.
124 Now, let's get rid of `testfile`. To remove a file, just enter the
129 The `rm` command can be used to remove files. If you enter `ls` again,
130 you will see that `testfile` is gone.
133 ## Changing Directories
135 Now, let's move to a different directory. The command `cd` (change
136 directory) is used to move around. Let's move into the `boot-camps`
137 directory. Enter the following command:
141 Use the `ls` command to see what is inside this directory. This
142 directory contains all of the material for this boot camp. Now move to
143 the directory containing the data for the shell tutorial:
147 Now use the `ls` command to see what is inside this directory. You
148 will see that there is an entry which is green. This means that this
149 is an executable. If you use `ls -F` you will see that this file ends
152 If you enter the `cd` command by itself, you will return to the home
153 directory. Try this, and then navigate back to the `shell`
158 Most programs take additional arguments that control their exact
159 behavior. For example, `-F` and `-l` are arguments to `ls`. The `ls`
160 program, like many programs, take a lot of arguments. But how do we
161 know what the options are to particular commands?
163 Most commonly used shell programs have a manual. You can access the
164 manual using the `man` program. Try entering:
168 This will open the manual page for `ls`. Use the space key to go
169 forward and b to go backwards. When you are done reading, just hit `q`
172 Programs that are run from the shell can get extremely complicated. To
173 see an example, open up the manual page for the `find` program,
174 which we will use later this session. No one can possibly learn all of
175 these arguments, of course. So you will probably find yourself
176 referring back to the manual page frequently.
181 1. Use the manual page for `ls` to guess what you would expect from
182 using the arguments `-l`, '-t', '-r' at the same time.
183 2. Try the following and see if you can figure out what they do, either by examining the results or consulting the manual page.
184 * `ls -lS` (equivalent to `ls -l -S`)
185 * `ls -lt` (equivalent to `ls -l -t`)
186 * `ls -1` (that's the number one, not a letter 'ell')
191 ## Examining the contents of other directories
193 By default, the `ls` commands lists the contents of the working
194 directory (i.e. the directory you are in). You can always find the
195 directory you are in using the `pwd` command. However, you can also
196 give `ls` the names of other directories to view. Navigate to the
197 home directory if you are not already there. Then enter the
202 This will list the contents of the `boot-camps` directory without
203 you having to navigate there. Now enter:
207 This prints the contents of `shell`. The `cd` command works in a
208 similar way. Try entering:
212 and you will jump directly to `shell` without having to go through
213 the intermediate directory.
215 ## Full vs. Relative Paths
217 The `cd` command takes an argument which is the directory
218 name. Directories can be specified using either a *relative* path a
219 full *path*. The directories on the computer are arranged into a
220 hierarchy. The full path tells you where a directory is in that
221 hierarchy. Navigate to the home directory. Now, enter the `pwd`
222 command and you should see:
226 which is the full name of your home directory. This tells you that you
227 are in a directory called `swc`, which sits inside a directory called
228 `home` which sits inside the very top directory in the hierarchy. The
229 very top of the hierarchy is a directory called `/` which is usually
230 referred to as the *root directory*. So, to summarize: `swc` is a
231 directory in `home` which is a directory in `/`.
233 Now enter the following command:
235 cd /home/swc/boot-camps/shell
237 This jumps to `shell`. Now go back to the home directory. We saw
238 earlier that the command:
242 had the same effect - it took us to the `shell` directory. But,
243 instead of specifying the full path
244 (`/home/swc/boot-camps/shell`), we specified a *relative path*. In
245 other words, we specified the path relative to our current
246 directory. A full path always starts with a `/`. A relative path does
247 not. You can usually use either a full path or a relative path
248 depending on what is most convenient. If we are in the home directory,
249 it is more convenient to just enter the relative path since it
250 involves less typing.
252 Over time, it will become easier for you to keep a mental note of the
253 structure of the directories that you are using hand how to quickly
254 navigate amongst them.
259 Now, list the contents of the /bin directory. Do you see anything
264 ## Saving time with shortcuts, wild cards, and tab completion
268 There are some shortcuts which you should know about. Dealing with the
269 home directory is very common. So, in the shell the tilde character,
270 `~`, is a shortcut for your home directory. Navigate to the `shell`
271 directory, then enter the command:
275 This prints the contents of your home directory, without you having to
276 type the full path. The shortcut `..` always refers to the directory
277 above your current directory. Thus:
281 prints the contents of the `/home/swc/boot-camps`. You can chain
286 prints the contents of `/home/swc` which is your home
287 directory. Finally, the special directory `.` always refers to your
288 current directory. So, `ls`, `ls .`, and `ls ././././.` all do the
289 same thing, they print the contents of the current directory. This may
290 seem like a useless shortcut right now, but we'll see when it is
291 needed in a little while.
293 To summarize, while you are in the `shell` directory, the commands
294 `ls ~`, `ls ~/.`, `ls ../../`, and `ls /home/swc` all do exactly the
295 same thing. These shortcuts are not necessary, they are provided for
298 ### Our data set: Cochlear Implants
300 A cochlear implant is a small electronic device that is surgically
301 implanted in the inner ear to give deaf people a sense of
302 hearing. More than a quarter of a million people have them, but there
303 is still no widely-accepted benchmark to measure their effectiveness.
304 In order to establish a baseline for such a benchmark, our supervisor
305 got teenagers with CIs to listen to audio files on their computer and
308 1. the quietest sound they could hear
309 2. the lowest and highest tones they could hear
310 3. the narrowest range of frequencies they could discriminate
312 To participate, subjects attended our laboratory and one of our lab
313 techs played an audio sample, and recorded their data - when they
314 first heard the sound, or first heard a difference in the sound. Each
315 set of test results were written out to a text file, one set per file.
316 Each participant has a unique subject ID, and a made-up subject name.
317 Each experiment has a unique experiment ID. The experiment has
318 collected 351 files so far.
320 The data is a bit of a mess! There are inconsistent file names, there
321 are extraneous "NOTES" files that we'd like to get rid of, and the
322 data is spread across many directories. We are going to use shell
323 commands to get this data into shape. By the end we would like to:
325 1. Put all of the data into one directory called "alldata"
327 2. Have all of the data files in there, and ensure that every file
328 has a ".txt" extension
330 3. Get rid of the extraneous "NOTES" files
332 If we can get through this example in the available time, we will move
333 onto more advanced shell topics...
337 Navigate to the `~/boot-camps/shell/data/THOMAS` directory. This
338 directory contains our hearing test data for THOMAS. If we type `ls`,
339 we will see that there are a bunch of files which are just four digit
340 numbers. By default, `ls` lists all of the files in a given
341 directory. The `*` character is a shortcut for "everything". Thus, if
342 you enter `ls *`, you will see all of the contents of a given
343 directory. Now try this command:
347 This lists every file that ends with a `1`. This command:
351 Lists every file in `/usr/bin` that ends in the characters `.sh`. And
356 lists every file in the current directory whose name contains the
357 number `4`, and ends with the number `1`. There are four such files:
358 `0241`, `0341`, `0431`, and `0481`.
360 So how does this actually work? Well...when the shell (bash) sees a
361 word that contains the `*` character, it automatically looks for filenames
362 that match the given pattern. In this case, it identified four such
363 files. Then, it replaced the `*4*1` with the list of files, separated
364 by spaces. In other words, the two commands:
367 ls 0241 0341 0431 0481
369 are exactly identical. The `ls` command cannot tell the difference
370 between these two things.
375 Do each of the following using a single `ls` command without
376 navigating to a different directory.
378 1. List all of the files in `/bin` that contain the letter `a`
379 2. List all of the files in `/bin` that contain the letter `a` or the letter `b`
380 3. List all of the files in `/bin` that contain the letter `a` AND the letter `b`
386 Navigate to the home directory. Typing out directory names can waste a
387 lot of time. When you start typing out the name of a directory, then
388 hit the tab key, the shell will try to fill in the rest of the
389 directory name. For example, enter:
393 The shell will fill in the rest of the directory name for
394 `boot-camps`. Now enter:
398 When you hit the first tab, nothing happens. The reason is that there
399 are multiple directories in the home directory which start with
400 `s`. Thus, the shell does not know which one to fill in. When you hit
401 tab again, the shell will list the possible choices.
403 Tab completion can also fill in the names of programs. For example,
404 enter `e<tab><tab>`. You will see the name of every program that
405 starts with an `e`. One of those is `echo`. If you enter `ec<tab>` you
406 will see that tab completion works.
410 You can easily access previous commands. Hit the up arrow.
411 Hit it again. You can step backwards through your command history.
412 The down arrow takes your forwards in the command history.
414 ^-C will cancel the command you are writing, and give you a fresh prompt.
416 ^-R will do a reverse-search through your command history. This
419 You can also review your recent commands with the `history` command. Just enter:
423 to see a numbered list of recent commands, including this just issues
424 `history` command. You can reuse one of these commands directly by
425 referring to the number of that command.
427 If your history looked like this:
433 then you could repeat command #260 by simply entering:
437 (that's an exclamation mark).
442 1. Find the line number in your history for the last exercise (listing
443 files in /bin) and reissue that command.
449 Commands like `ls`, `rm`, `echo`, and `cd` are just ordinary programs
450 on the computer. A program is just a file that you can *execute*. The
451 program `which` tells you the location of a particular program. For
456 Will return "/bin/ls". Thus, we can see that `ls` is a program that
457 sits inside of the `/bin` directory. Now enter:
461 You will see that `find` is a program that sits inside of the
462 `/usr/bin` directory.
464 So ... when we enter a program name, like `ls`, and hit enter, how
465 does the shell know where to look for that program? How does it know
466 to run `/bin/ls` when we enter `ls`. The answer is that when we enter
467 a program name and hit enter, there are a few standard places that the
468 shell automatically looks. If it can't find the program in any of
469 those places, it will print an error saying "command not found". Enter
474 This will print out the value of the `PATH` environment variable. More
475 on environment variables later. Notice that a list of directories,
476 separated by colon characters, is listed. These are the places the
477 shell looks for programs to run. If your program is not in this list,
478 then an error is printed. The shell ONLY checks in the places listed
479 in the `PATH` environment variable.
481 Navigate to the `shell` directory and list the contents. You will
482 notice that there is a program (executable file) called `hello` in
483 this directory. Now, try to run the program by entering:
487 You should get an error saying that hello cannot be found. That is
488 because the directory `/home/swc/boot-camps/shell` is not in the
489 `PATH`. You can run the `hello` program by entering:
493 Remember that `.` is a shortcut for the current working
494 directory. This tells the shell to run the `hello` program which is
495 located right here. So, you can run any program by entering the path
496 to that program. You can run `hello` equally well by specifying:
498 /home/swc/boot-camps/shell/hello
504 When there are no `/` characters, the shell assumes you want to look
505 in one of the default places for the program.
510 We now know how to switch directories, run programs, and look at the
511 contents of directories, but how do we look at the contents of files?
513 The easiest way to examine a file is to just print out all of the
514 contents using the program `cat`. Enter the following command:
518 This prints out the contents of the `ex_data.txt` file. If you enter:
520 cat ex_data.txt ex_data.txt
522 It will print out the contents of `ex_data.txt` twice. `cat` just
523 takes a list of file names and writes them out one after another (this
524 is where the name comes from, `cat` is short for concatenate).
529 1. Print out the contents of the `~/boot-camps/shell/dictionary.txt`
530 file. What does this file contain?
532 2. Without changing directories, (you should still be in `shell`),
533 use one short command to print the contents of all of the files in
534 the `/home/swc/boot-camps/shell/data/THOMAS` directory.
538 `cat` is a terrific program, but when the file is really big, it can
539 be annoying to use. The program, `less`, is useful for this
540 case. Enter the following command:
542 less ~/boot-camps/shell/dictionary.txt
544 `less` opens the file, and lets you navigate through it. The commands
545 are identical to the `man` program.
547 **Some commands in `less`**
550 | ------- | ---------- |
551 | "space" | to go forward |
552 | "b" | to go backwarsd |
553 | "g" | to go to the beginning |
554 | "G" | to go to the end |
557 `less` also gives you a way of searching through files. Just hit the
558 "/" key to begin a search. Enter the name of the word you would like
559 to search for and hit enter. It will jump to the next location where
560 that word is found. Try searching the `dictionary.txt` file for the
561 word "cat". If you hit "/" then "enter", `less` will just repeat
562 the previous search. `less` searches from the current location and
563 works its way forward. If you are at the end of the file and search
564 for the word "cat", `less` will not find it. You need to go to the
565 beginning of the file and search.
567 Remember, the `man` program actually uses `less` internally and
568 therefore uses the same commands, so you can search documentation
574 Use the commands we've learned so far to figure out how to search
575 in reverse while using `less`.
582 Let's turn to the experimental data from the hearing tests that we
583 began with. This data is located in the `~/boot-camps/shell/data`
584 directory. Each subdirectory corresponds to a particular participant
585 in the study. Navigate to the `Bert` subdirectory in `data`. There
586 are a bunch of text files which contain experimental data
587 results. Lets print them all:
591 Now enter the following command:
593 cat au* > ../all_data
595 This tells the shell to take the output from the `cat au*` command and
596 dump it into a new file called `../all_data`. To verify that this
597 worked, examine the `all_data` file. If `all_data` had already
598 existed, we would overwritten it. So the `>` character tells the shell
599 to take the output from what ever is on the left and dump it into the
600 file on the right. The `>>` characters do almost the same thing,
601 except that they will append the output to the file if it already
607 Use `>>`, to append the contents of all of the files whose names
608 contain the number 4 in the directory:
610 /home/swc/boot-camps/shell/data/gerdal
612 to the existing `all_data` file. Thus, when you are done `all_data`
613 should contain all of the experiment data from Bert and any
614 experimental data file from gerdal with filenames that contain the
620 ## Creating, moving, copying, and removing
622 We've created a file called `all_data` using the redirection operator
623 `>`. This file is critical - it's our analysis results - so we want to
624 make copies so that the data is backed up.
625 Lets copy the file using the `cp` command. The `cp`
626 command backs up the file. Navigate to the `data` directory and enter:
628 cp all_data all_data_backup
630 Now `all_data_backup` has been created as a copy of `all_data`. We can
631 move files around using the command `mv`. Enter this command:
633 mv all_data_backup /tmp/
635 This moves `all_data_backup` into the directory `/tmp`. The directory
636 `/tmp` is a special directory that all users can write to. It is a
637 temporary place for storing files. Data stored in `/tmp` is
638 automatically deleted when the computer shuts down.
640 The `mv` command is also how you rename files. Since this file is so
641 important, let's rename it:
643 mv all_data all_data_IMPORTANT
645 Now the file name has been changed to all_data_IMPORTANT. Let's delete
648 rm /tmp/all_data_backup
650 The `mkdir` command is used to make a directory. Just enter `mkdir`
651 followed by a space, then the directory name.
658 1. Rename the `all_data_IMPORTANT` file to `all_data`.
659 2. Create a directory in the `data` directory called `foo`
660 3. Then, copy the `all_data` file into `foo`
664 By default, `rm`, will NOT delete directories. You can tell `rm` to
665 delete a directory using the `-r` option. Enter the following command:
672 The `wc` program (word count) counts the number of lines, words, and
673 characters in one or more files. Make sure you are in the `data`
674 directory, then enter the following command:
678 For each of the files indicated, `wc` has printed a line with three
679 numbers. The first is the number of lines in that file. The second is
680 the number of words. Finally, the total number of characters is
681 indicated. The final line contains this information summed over all of
682 the files. Thus, there were 10445 characters in total.
684 Remember that the `Bert/*` and `gerdal/*4*` files were merged
685 into the `all_data` file. So, we should see that `all_data` contains
686 the same number of characters:
690 Every character in the file takes up one byte of disk space. Thus, the
691 size of the file in bytes should also be 10445. Let's confirm this:
695 Remember that `ls -l` prints out detailed information about a file and
696 that the fifth column is the size of the file in bytes.
701 Figure out how to get `wc` to print the length of the longest line in
706 ## The awesome power of the Pipe
708 Suppose I wanted to only see the total number of character, words, and
709 lines across the files `Bert/*` and `gerdal/*4*`. I don't want to
710 see the individual counts, just the total. Of course, I could just do:
714 Since this file is a concatenation of the smaller files. Sure, this
715 works, but I had to create the `all_data` file to do this. Thus, I
716 have wasted a precious 10445 bytes of hard disk space. We can do this
717 *without* creating a temporary file, but first I have to show you two
718 more commands: `head` and `tail`. These commands print the first few,
719 or last few, lines of a file, respectively. Try them out on
725 The `-n` option to either of these commands can be used to print the
726 first or last `n` lines of a file. To print the first/last line of the
732 Let's turn back to the problem of printing only the total number of
733 lines in a set of files without creating any temporary files. To do
734 this, we want to tell the shell to take the output of the `wc Bert/*
735 gerdal/*4*` and send it into the `tail -n 1` command. The `|`
736 character (called pipe) is used for this purpose. Enter the following
739 wc Bert/* gerdal/Data0559 | tail -n 1
741 This will print only the total number of lines, characters, and words
742 across all of these files. What is happening here? Well, `tail`, like
743 many command line programs will read from the *standard input* when it
744 is not given any files to operate on. In this case, it will just sit
745 there waiting for input. That input can come from the user's keyboard
746 *or from another program*. Try this:
750 Notice that your cursor just sits there blinking. Tail is waiting for
751 data to come in. Now type:
758 then CONTROL+d. You should is the lines:
763 printed back at you. The CONTROL+d keyboard shortcut inserts an
764 *end-of-file* character. It is sort of the standard way of telling the
765 program "I'm done entering data". The `|` character is replaces the
766 data from the keyboard with data from another command. You can string
767 all sorts of commands together using the pipe.
769 The philosophy behind these command line programs is that none of them
770 really do anything all that impressive. BUT when you start chaining
771 them together, you can do some really powerful things really
772 efficiently. If you want to be proficient at using the shell, you must
773 learn to become proficient with the pipe and redirection operators:
777 ### A sorting example
779 Let's create a file with some words to sort for the next example. We
780 want to create a file which contains the following names:
787 To do this, we need a program which allows us to create text
788 files. There are many such programs, the easiest one which is
789 installed on almost all systems is called `nano`. Navigate to `/tmp`
790 and enter the following command:
794 Now enter the four names as shown above. When you are done, press
795 CONTROL+O to write out the file. Press enter to use the file name
796 `toBeSorted`. Then press CONTROL+x to exit `nano`.
798 When you are back to the command line, enter the command:
802 Notice that the names are now printed in alphabetical order.
807 Use the `echo` command and the append operator, `>>`, to append your
808 name to the file, then sort it and make a new file called Sorted.
812 Let's navigate back to `~/boot-camps/shell/data`. Enter the following command:
814 wc Bert/* | sort -k 3 -n
816 We are already familiar with what the first of these two commands
817 does: it creates a list containing the number of characters, words,
818 and lines in each file in the `Bert` directory. This list is then
819 piped into the `sort` command, so that it can be sorted. Notice there
820 are two options given to sort:
822 1. `-k 3`: Sort based on the third column
823 2. `-n`: Sort in numerical order as opposed to alphabetical order
825 Notice that the files are sorted by the number of characters.
830 1. Use the `man` command to find out how to sort the output from `wc` in
833 2. Combine the `wc`, `sort`, `head` and `tail` commands so that only the
834 `wc` information for the largest file is listed
836 Hint: To print the smallest file, use:
838 wc Bert/* | sort -k 3 -n | head -n 1
842 Printing the smallest file seems pretty useful. We don't want to type
843 out that long command often. Let's create a simple script, a simple
844 program, to run this command. The program will look at all of the
845 files in the current directory and print the information about the
846 smallest one. Let's call the script `smallest`. We'll use `nano` to
847 create this file. Navigate to the `data` directory, then:
851 Then enter the following text:
854 wc * | sort -k 3 -n | head -n 1
856 Now, `cd` into the `Bert` directory and enter the command
857 `../smallest`. Notice that it says permission denied. This happens
858 because we haven't told the shell that this is an executable
859 file. If you do `ls -l ../smallest`, it will show you the permissions on
860 the left of the listing.
862 Enter the following commands:
864 chmod a+x ../smallest
867 The `chmod` command is used to modify the permissions of a file. This
868 particular command modifies the file `../smallest` by giving all users
869 (notice the `a`) permission to execute (notice the `x`) the file. If
874 You will see that the file name is green and the permissions have changed.
875 Congratulations, you just created your first shell script!
879 You can search the contents of a file using the command `grep`. The
880 `grep` program is very powerful and useful especially when combined
881 with other commands by using the pipe. Navigate to the `Bert`
882 directory. Every data file in this directory has a line which says
883 "Range". The range represents the smallest frequency range that can be
884 discriminated. Lets list all of the ranges from the tests that Bert
892 Create an executable script called `smallestrange` in the `data`
893 directory, that is similar to the `smallest` script, but prints the
894 file containing the file with the smallest Range. Use the commands
895 `grep`, `sort`, and `tail` to do this.
902 The `find` program can be used to find files based on arbitrary
903 criteria. Navigate to the `data` directory and enter the following
908 This prints the name of every file or directory, recursively, starting
909 from the current directory. Let's exclude all of the directories:
911 find . -type f -print
913 This tells `find` to locate only files. Now try these commands:
915 find . -type f -name "*1*"
916 find . -type f -name "*1*" -or -name "*2*" -print
917 find . -type f -name "*1*" -and -name "*2*" -print
919 The `find` command can acquire a list of files and perform some
920 operation on each file. Try this command out:
922 find . -type f -exec grep Volume {} \;
924 This command finds every file starting from `.`. Then it searches each
925 file for a line which contains the word "Volume". The `{}` refers to
926 the name of each file. The trailing `\;` is used to terminate the
927 command. This command is slow, because it is calling a new instance
928 of `grep` for each item the `find` returns.
930 A faster way to do this is to use the `xargs` command:
932 find . -type f -print | xargs grep Volume
934 `find` generates a list of all the files we are interested in,
935 then we pipe them to `xargs`. `xargs` takes the items given to it
936 and passes them as arguments to `grep`. `xargs` generally only creates
937 a single instance of `grep` (or whatever program it is running).
942 Navigate to the `data` directory. Use one `find` command to perform each
943 of the operations listed below (except number 2, which does not
944 require a `find` command):
946 1. Find any file whose name is "NOTES" within `data` and delete it
948 2. Create a new directory called `cleaneddata`
950 3. Move all of the files within `data` to the `cleaneddata` directory
952 4. Rename all of the files to ensure that they end in `.txt` (note:
953 it is ok for the file name to end in `.txt.txt`
955 Hint: If you make a mistake and need to start over just do the
958 1. Navigate to the `shell` directory
960 2. Delete the `data` directory
962 3. Enter the command: `git checkout -- data` You should see that the
963 data directory has reappeared in its original state
967 Redo exercise 4, except rename only the files which do not already end
968 in `.txt`. You will have to use the `man` command to figure out how to
969 search for files which do not match a certain name.
977 **backtick, xargs**: Example find all files with certain text
981 **variables** -> use a path example
991 **Regular Expressions**
995 **Chaining commands together**